hibernate 基本配置详解,hibernate入门配置

朴灿烈づ我的快乐病毒、 2022-05-22 04:12 376阅读 0赞

这篇博客,主要是对hibernate常用的标签一个说明,如果你还不懂怎么配置基本的hibernate可以去看这篇博客

https://blog.csdn.net/tomwildboar/article/details/80694269

orm元数据(实体配置文件)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <!-- 配置表与实体对象的关系 -->
  6. <!-- package属性:填写一个包名 -->
  7. <hibernate-mapping package="hibernate.domain">
  8. <!--
  9. class 元素:配置实体与表的对应关系的
  10. name:完整类名
  11. table:数据库表名
  12. -->
  13. <class name="hibernate.domain.Customer" table="cst_customer">
  14. <!--
  15. id:配置主键映射的属性
  16. name:填写主键对应属性名
  17. column:填写表中的主键列名
  18. -->
  19. <id name="cust_id" column="cust_id">
  20. <!-- generator :主键生成策略 -->
  21. <generator class="native"></generator>
  22. </id>
  23. <!--
  24. property 元素:除id之外的普通属性映射
  25. name:填写对应属性名
  26. column(可选):填写表中的列名,默认值:列名会默认使用name的值
  27. type(可选):填写该列(属性)的类型。hibernate会自动检测实体的属性类型
  28. 每个类型有三种填法:java类型
  29. hibernate类型
  30. 数据库类型
  31. not-null(可选): 配置是否为空,默认值:false
  32. length(可选):配置数据库中列的长度,默认值:自动取当前数据库最大长度
  33. -->
  34. <!--
  35. java类型
  36. <property name="cust_name" column="cust_name" type="java.lang.String"></property>
  37. -->
  38. <!--
  39. hibernate类型
  40. <property name="cust_name" column="cust_name" type="string"></property>
  41. -->
  42. <!--
  43. 数据库类型
  44. <property name="cust_name" column="cust_name">
  45. <column name="cust_name" sql-type="varchar"></column>
  46. </property>
  47. -->
  48. <property name="cust_name" column="cust_name"></property>
  49. <property name="cust_source" column="cust_source"></property>
  50. <property name="cust_industry" column="cust_industry"></property>
  51. <property name="cust_level" column="cust_level"></property>
  52. <property name="cust_linkman" column="cust_linkman"></property>
  53. <property name="cust_phone" column="cust_phone"></property>
  54. <property name="cust_mobile" column="cust_mobile"></property>
  55. </class>
  56. </hibernate-mapping>

主配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!--
  8. #hibernate.dialect org.hibernate.dialect.MySQLDialect
  9. #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
  10. #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
  11. #hibernate.connection.driver_class com.mysql.jdbc.Driver
  12. #hibernate.connection.url jdbc:mysql:///test
  13. #hibernate.connection.username gavin
  14. #hibernate.connection.password
  15. -->
  16. <!-- 数据库驱动 -->
  17. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  18. <!-- 三个/// 表示链接本机 数据了url -->
  19. <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
  20. <!-- 数据库链接名 -->
  21. <property name="hibernate.connection.username">root</property>
  22. <!-- 数据库链接密码 -->
  23. <property name="hibernate.connection.password">123</property>
  24. <!-- 数据库方言
  25. 不同的数据中,sql语法有区别,指定方言可以放hibernate框架在生成sql语句时,针对数据库方言生成
  26. sql99标准: DDL 定义语言 库、表的增删改查
  27. DML 控制语言 事物、权限
  28. DML 操纵语言 增删改查
  29. 注意:mysql在选择方言的时候,请选择最短的方言
  30. -->
  31. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  32. <!--
  33. #hibernate.show_sql true
  34. hibernate.format_sql true
  35. -->
  36. <!-- 将hibernate生成的sql语句打印到控制台 -->
  37. <property name="hibernate.show_sql">true</property>
  38. <!-- 将hibernate生成的sql语句格式化 -->
  39. package hibernate.test;
  40. import org.hibernate.Session;
  41. import org.hibernate.SessionFactory;
  42. import org.hibernate.Transaction;
  43. import org.hibernate.cfg.Configuration;
  44. import org.junit.Test;
  45. import hibernate.domain.Customer;
  46. //学习Session对象
  47. //session对象功能:表达hibernate框架与数据库之间的连接(会话)
  48. //session 类似于JDBC的connection对象 还可以完成对数据库中数据的增删改查
  49. //session 是hibernate操作数据库的核心对象
  50. public class api_session {
  51. @Test
  52. public void fun1(){
  53. //1、创建,调用空参构造
  54. Configuration conf = new Configuration().configure();
  55. //2、根据配置信息,创建SessionFactory对象
  56. SessionFactory sf = conf.buildSessionFactory();
  57. //3、获取sessio
  58. //打开一个新的session对象
  59. Session session = sf.openSession();
  60. //4、session获得操作事物的Transaction对象
  61. //获得操作事物的对象
  62. // Transaction tx = session.getTransaction();
  63. //获得操作事物的对象 并开启事务
  64. Transaction tx2 = session.beginTransaction();
  65. //-----------------------------
  66. // 相对应的增删改查
  67. //-----------------------------
  68. tx2.commit(); //提交
  69. tx2.rollback(); //回滚
  70. session.close();
  71. sf.close();
  72. }
  73. //增
  74. @Test
  75. public void fun2(){
  76. //1、创建,调用空参构造
  77. Configuration conf = new Configuration().configure();
  78. //2、根据配置信息,创建SessionFactory对象
  79. SessionFactory sf = conf.buildSessionFactory();
  80. //3、获取sessio
  81. //打开一个新的session对象
  82. Session session = sf.openSession();
  83. //4、session获得操作事物的Transaction对象
  84. //获得操作事物的对象
  85. // Transaction tx = session.getTransaction();
  86. //获得操作事物的对象 并开启事务
  87. Transaction tx2 = session.beginTransaction();
  88. //-----------------------------
  89. Customer c = new Customer();
  90. c.setCust_name("123456");
  91. session.save(c);
  92. //-----------------------------
  93. tx2.commit(); //提交
  94. // tx2.rollback(); //回滚
  95. session.close();
  96. sf.close();
  97. }
  98. //查询 id = 1
  99. @Test
  100. public void fun3(){
  101. //1、创建,调用空参构造
  102. Configuration conf = new Configuration().configure();
  103. //2、根据配置信息,创建SessionFactory对象
  104. SessionFactory sf = conf.buildSessionFactory();
  105. //3、获取sessio
  106. //打开一个新的session对象
  107. Session session = sf.openSession();
  108. //4、session获得操作事物的Transaction对象
  109. //获得操作事物的对象
  110. // Transaction tx = session.getTransaction();
  111. //获得操作事物的对象 并开启事务
  112. Transaction tx2 = session.beginTransaction();
  113. //-----------------------------
  114. Customer customer = session.get(Customer.class, 1l);
  115. System.out.println(customer.getCust_name());
  116. //-----------------------------
  117. tx2.commit(); //提交
  118. // tx2.rollback(); //回滚
  119. session.close();
  120. sf.close();
  121. }
  122. //修改
  123. @Test
  124. public void fun4(){
  125. //1、创建,调用空参构造
  126. Configuration conf = new Configuration().configure();
  127. //2、根据配置信息,创建SessionFactory对象
  128. SessionFactory sf = conf.buildSessionFactory();
  129. //3、获取sessio
  130. //打开一个新的session对象
  131. Session session = sf.openSession();
  132. //4、session获得操作事物的Transaction对象
  133. //获得操作事物的对象
  134. // Transaction tx = session.getTransaction();
  135. //获得操作事物的对象 并开启事务
  136. Transaction tx2 = session.beginTransaction();
  137. //-----------------------------
  138. //1、获得要修改的对象
  139. Customer c = session.get(Customer.class, 1l);
  140. //2、修改
  141. c.setCust_name("222222");
  142. session.update(c);
  143. //-----------------------------
  144. tx2.commit(); //提交
  145. // tx2.rollback(); //回滚
  146. session.close();
  147. sf.close();
  148. }
  149. //删除
  150. @Test
  151. public void fun5(){
  152. //1、创建,调用空参构造
  153. Configuration conf = new Configuration().configure();
  154. //2、根据配置信息,创建SessionFactory对象
  155. SessionFactory sf = conf.buildSessionFactory();
  156. //3、获取sessio
  157. //打开一个新的session对象
  158. Session session = sf.openSession();
  159. //4、session获得操作事物的Transaction对象
  160. //获得操作事物的对象
  161. // Transaction tx = session.getTransaction();
  162. //获得操作事物的对象 并开启事务
  163. Transaction tx2 = session.beginTransaction();
  164. //-----------------------------
  165. //1、获得要删除的对象
  166. Customer c = session.get(Customer.class, 1l);
  167. //2、删除
  168. session.delete(c);
  169. //-----------------------------
  170. tx2.commit(); //提交
  171. // tx2.rollback(); //回滚
  172. session.close();
  173. sf.close();
  174. }
  175. }

trueupdate

核心api and 增删改查

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读