1)通过Maven的pom.xml引用hibernate的jar包
1 <dependency> 2 <groupId>org.hibernate</groupId> 3 <artifactId>hibernate-core</artifactId> 4 <!-- <version>5.1.3.Final</version> --> 5 <version>4.3.9.Final</version> 6 </dependency>
hibernate-core-
2)hibernate.cfg.xml文件(官网可以copy到)
1 <?xml version='1.0' encoding='utf-8'?> 2 <!-- 3 ~ Hibernate, Relational Persistence for Idiomatic Java 4 ~ 5 ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 6 ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 7 --> 8 <!DOCTYPE hibernate-configuration PUBLIC 9 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 10 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 11 12 <hibernate-configuration> 13 14 <session-factory> 15 16 <!-- Database connection settings --> 17 <!-- 所连接的数据库信息 --> 18 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 19 <property name="connection.url">url?useUnicode=true&characterEncoding=utf-8</property> 20 <property name="connection.username">username</property> 21 <property name="connection.password">password</property> 22 23 <!-- JDBC connection pool (use the built-in) 24 链接池的大小--> 25 <property name="connection.pool_size">5</property> 26 27 <!-- SQL dialect --> 28 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 29 30 <!-- Disable the second-level cache --> 31 <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 32 33 <!-- Echo all executed SQL to stdout 标准输出--> 34 <property name="show_sql">true</property> 35 36 <!-- Drop and re-create the database schema on startup 37 是否自动生成表 38 <property name="hbm2ddl.auto">create</property>--> 39 40 <!-- 通过包路径来寻找文件,xml文件使用 --> 41 <mapping resource="。。。/hibernate/entity/User.hbm.xml"/> 42 <!-- 通过类路径来寻找文件,注解使用 --> 43 <mapping class="。。。.hibernate.entity.AnnoUser"/> 44 45 </session-factory> 46 47 </hibernate-configuration>
3)实体类(entity)
*不使用注解的实体类User.java
1 //配置信息都在resouces下的同一目录下的User.hbm.xml 2 public class User{ 3 private BigInteger id; 4 private String name; 5 private String account; 6 private String password; 7 private BigInteger organizationId;
User.hm.xml
1 <?xml version="1.0"?> 2 3 <!-- 4 ~ Hibernate, Relational Persistence for Idiomatic Java 5 ~ 6 ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 7 ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 8 --> 9 <!DOCTYPE hibernate-mapping PUBLIC 10 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 11 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 12 <!-- 通过包路径扫描实体类 --> 13 <hibernate-mapping package="。。。.hibernate.entity"> 14 15 <class name="User" table="user"> 16 <id name="id" column="id"> 17 <generator class="assigned"/> 18 </id> 19 <property name="name"/> 20 <property name="account"/> 21 <property name="password"/> 22 23 <!-- 属性名和字段名一致用下列代码表示 24 <property name="title"/> 25 属性名和字段名不一致用下列代码表示 26 <property name="title" column="数据库字段名"/> 27 --> 28 </class> 29 30 </hibernate-mapping> 31
*使用注解的实体类AnnoUser
1 //表示为实体类 2 @Entity 3 //表示连接的数据库名称 4 @Table(name="user") 5 public class AnnoUser{ 6 //表示为主键 7 @Id 8 private BigInteger id; 9 //表示连接的字段名称 10 @Column(name="name") 11 private String name; 12 13 @Column(name="account") 14 private String account; 15 16 @Column(name="password") 17 private String password; 18 //不和数据库进行映射 19 @Transient 20 private BigInteger organizationId;
4) 单元测试(实现对数据库简单的增删改查)
1 public class Hibernate4Annotest { 2 3 private SessionFactory sessionFactory; 4 5 @Before 6 public void before(){ 7 //(1)加载配置(默认CLASSPATH下的hibernate.cfg.xml) 8 Configuration conf = new Configuration().configure(); 9 10 sessionFactory = conf.buildSessionFactory( 11 new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build()); 12 13 14 /*//(2)通过Configuration创建StandardServiceRegistryBuilder实例 15 //由StandardServiceRegistryBuilder构建StandardServiceRegistry实例 16 StandardServiceRegistryBuilder serviceRegistryBuilder = 17 new StandardServiceRegistryBuilder().applySettings(conf.getProperties()); 18 StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.build(); 19 20 //(3)通过StandardServiceRegistry实例构建Session工厂 21 sessionFactory = conf.buildSessionFactory(serviceRegistry);*/ 22 23 } 24 25 @After 26 public void after(){ 27 sessionFactory.close(); 28 } 29 30 @Test 31 @Ignore 32 //查 33 public void select(){ 34 Session session = sessionFactory.openSession(); 35 Object user = session.get(User.class,new BigInteger("100000")); 36 System.out.println(user); 37 } 38 39 @Test 40 @Ignore 41 //改1 42 //修改有两种方式,一种是查到后,对源数据进行覆盖 43 //一种是根据id寻找源数据进行修改 44 public void update(){ 45 Session session = sessionFactory.openSession(); 46 //开启一个事务 47 Transaction tr = session.beginTransaction(); 48 User user = new User(); 49 user.setId(new BigInteger("100001")); 50 user.setAccount("hiber4"); 51 user.setContactPhone("11111111111"); 52 user.setCreateTime(new Date()); 53 user.setName("hibernate"); 54 user.setPassword("nate"); 55 user.setState(1); 56 57 session.update(user); 58 tr.commit(); 59 } 60 61 @Test 62 @Ignore 63 //改2 64 public void update2(){ 65 Session session = sessionFactory.openSession(); 66 //开启一个事务 67 Transaction tr = session.beginTransaction(); 68 User user = (User)session.get(User.class,new BigInteger("100001")); 69 user.setAccount("hiber3"); 70 user.setContactPhone("22222222222"); 71 user.setCreateTime(new Date()); 72 user.setName("hibernate"); 73 user.setPassword("nate"); 74 user.setState(1); 75 76 //session.flush(); 77 tr.commit(); 78 } 79 80 @Test 81 @Ignore 82 //删 83 public void delete(){ 84 Session session = sessionFactory.openSession(); 85 //开启一个事务 86 Transaction tr = session.beginTransaction(); 87 User user = new User(); 88 user.setId(new BigInteger("100000")); 89 90 session.delete(user); 91 tr.commit(); 92 } 93 94 @Test 95 @Ignore 96 //增 97 public void add(){ 98 Session session = sessionFactory.openSession(); 99 //开启一个事务 100 Transaction tr = session.beginTransaction(); 101 try { 102 User user = new User(); 103 user.setId(new BigInteger("100002")); 104 user.setAccount("hiber2"); 105 user.setContactPhone("11111111111"); 106 user.setCreateTime(new Date()); 107 user.setName("hibernate"); 108 user.setPassword("nate"); 109 user.setState(1); 110 //保存对象 111 session.save(user); 112 } catch (Exception e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 tr.rollback(); 116 } 117 //事务提交 118 tr.commit(); 119 //session关闭 120 session.close(); 121 } 122 123 }