Spring与Hibernate结合使用
1、使用Spring的IOC容器,将对象之间的依赖关系交给Spring,可以降低组件之间的耦合性,可以更专注于应用逻辑。同时可以提供众多服务,数据库事务管理,WS等。
2、Hibernate是对JDBC的轻量级的对象封装,是一个独立的对象持久化对象。
1)对象/关系数据库映射(ORM)
它使用时只需要操纵对象,使开发更对象化,抛弃了数据库中心的思想,完全的面向对象思想。
2) 事务Transaction(org.hibernate.Transaction)
应用程序用来指定原子操作单元范围的对象,它是单线程的,生命周期很短。它通过抽象将应用从底层具体的JDBC、JTA以及CORBA事务隔离开。某些情况下,一个Session之内可能包含多个Transaction对象。尽管是否使用该对象是可选的,但无论是使用底层的API还是使用Transaction对象,事务边界的开启与关闭是必不可少的。
3)简洁的HQL编程。
将上述两个框架结合起来实现对数据库的操作将继承两个框架的优点。
3、Spring与Hibernate框架整合
1)创建一个bean与与之对应的bean.hbm.xml文件,使用Hibernate的ORM对数据库表进行设置。
bean:
package com.ruijie.bean;public class Person {private int id;private String name;private String password;public int getId() {return this.id;}public void setId(int id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}}
Person.hbm.xml:
2)面对接口编程。创建一个接口操作数据库,并实现。
PersonServer:
public interface PersonServer {public abstract void save(Person person);public abstract void update(Person person);public abstract Person getPerson(int id);public abstract List getAllPerson();public abstract void delete(int id);} 实现类:
@Transactional
public class PersonServerImpl implements PersonServer {private SessionFactory sessionFactory;public void save(Person person){sessionFactory.getCurrentSession().persist(person);}public void update(Person person){sessionFactory.getCurrentSession().merge(person);}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)public Person getPerson(int personid){return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);}public void delete(int personid){sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personid));}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)@SuppressWarnings("unchecked")public List getAllPerson(){ return sessionFactory.getCurrentSession().createQuery("from Person").list();}public SessionFactory getSessionFactory() {return this.sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}}
3)配置beans.xml文件,配置bean和数据库源等。
com/ruijie/bean/Person.hbm.xml
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
4)编写测试用例测试。
public class PersonServerTest {private Logger logger = Logger.getLogger(this.getClass().getName());private static PersonServer personServer;@BeforeClasspublic static void setupBean(){ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");personServer = (PersonServer) context.getBean("personService");}@Testpublic void testSave() {logger.debug("开始插入数据");Person person = new Person();person.setName("ruijie");person.setPassword("awgagwg");personServer.save(person);logger.debug("数据插入成功");}@Testpublic void testUpdate(){Person person = new Person();person.setId(7);person.setName("李四");person.setPassword("wgeage");personServer.update(person );}@Testpublic void testDelete(){personServer.delete(7);}@Testpublic void testGetPerson(){Person person = personServer.getPerson(6);System.out.println("姓名:"+person.getName()+",密码:"+person.getPassword());}@Testpublic void testGetAllPerson(){List persons = personServer.getAllPerson();for (Person person : persons) {System.out.println("姓名:"+person.getName()+",密码:"+person.getPassword());}}
}
一个小小的demo: SpringHibernateDemo
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
