Hibernate之Hibernate环境搭建
一、Hibernate环境搭建的步骤
1.添加Hibernate && SQLServer 的Jar
antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar sqljdbc4.jar2.创建JavaBean类:User.java
1 package cn.com.zfc.hibernate.entities; 2 3 /** 4 * 5 * @title User 6 * @describe Hibernate 的 POJO 7 * @author 张富昌 8 * @date 2017年4月7日下午10:47:27 9 */10 public class User {11 private Integer id;12 private String userName;13 private String userDesc;14 15 public User() {16 super();17 }18 19 public User(String userName, String userDesc) {20 super();21 this.userName = userName;22 this.userDesc = userDesc;23 }24 25 public Integer getId() {26 return id;27 }28 29 public void setId(Integer id) {30 this.id = id;31 }32 33 public String getUserName() {34 return userName;35 }36 37 public void setUserName(String userName) {38 this.userName = userName;39 }40 41 public String getUserDesc() {42 return userDesc;43 }44 45 public void setUserDesc(String userDesc) {46 this.userDesc = userDesc;47 }48 49 @Override50 public String toString() {51 return "User [id=" + id + ", userName=" + userName + ", userDesc=" + userDesc + "]";52 }53 }
3.使用Xxx.hbm.xml 映射文件映射JavaBean属性到数据库表之间的映射关系:User.hbm.xml
1 2 4 5 67 8 9 2310 14 1511 12 13 16 18 1917 20 2221
4.在hibernate.cfg.xml Hibernate配置文件中配置Hibernate 数据源及相关属性:在hibernate.cfg.xml
1 2 5 67 8 9 10 11 12 32sa 13 14123456 15 16com.microsoft.sqlserver.jdbc.SQLServerDriver 17 18jdbc:sqlserver://localhost:1433;DatabaseName=hibernate-01 19 20 21true 22 23true 24 25update 26 27org.hibernate.dialect.SQLServerDialect 28 29 3031
5.使用Hibernate ORM框架完成对JavaBean对象的操作
①创建Configuration对象
Configuration configuration = new Configuration().configure(); ②创建SessionFactory 工厂类 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); ③创建Session对象 Session session = sessionFactory.openSession(); ④开启事物 Transaction(查询不需要事务) Transaction transaction = session.beginTransaction(); ⑤执行业务****** CRUD操作 ⑥提交事物 transaction.commit(); ⑦关闭会话 session.close(); ⑧关闭 SessionFactory sessionFactory.close();6.数据库:hibernate-01
数据表:users
二、JUNIT 单元测试初识
1.单元测试的基本概念
2.使用Junit 搭建Hibernate 测试环境
3.@Test @Before @After
4.Hibernate 环境的测试:TestHibernate.java
1 package cn.com.zfc.hibernate.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 7 import org.hibernate.cfg.Configuration; 8 import org.hibernate.service.ServiceRegistry; 9 import org.junit.After;10 import org.junit.Before;11 import org.junit.Test;12 13 import cn.com.zfc.hibernate.entities.User;14 15 /**16 * 17 * @title TestHibernate18 * @describe 测试 Hibernate 的环境19 * @author 张富昌20 * @date 2017年4月7日下午10:47:0521 */22 public class TestHibernate {23 24 private Configuration configuration = null;25 private SessionFactory sessionFactory = null;26 private Session session = null;27 private Transaction transaction;28 29 @Before30 public void init() {31 configuration = new Configuration().configure();32 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();33 sessionFactory = configuration.buildSessionFactory(serviceRegistry);34 session = sessionFactory.openSession();35 transaction = session.beginTransaction();36 }37 38 @After39 public void destory() {40 transaction.commit();41 session.close();42 sessionFactory.close();43 }44 45 // 添加46 @Test47 public void testSave() {48 User user = new User("UU-1", "爱生活,爱编程");49 session.save(user);50 }51 52 // 立即加载53 @Test54 public void testGet() {55 User user = (User) session.get(User.class, 1);56 System.out.println("user:" + user);57 }58 59 // 延迟加载:当数据真正使用时在开始加载60 @Test61 public void testLoad() {62 User user = (User) session.load(User.class, 1);63 System.out.println("user:" + user);64 }65 66 // 删除67 @Test68 public void testDelete() {69 User user = (User) session.load(User.class, 4);70 session.delete(user);71 }72 73 // 修改74 @Test75 public void testUpdate() {76 User user = (User) session.load(User.class, 1);77 user.setUserDesc("我是修改过的东西");78 session.update(user);79 }80 81 @Test82 public void dataSource() {83 }84 85 }