hibernate3.jarlib\rquired\*.jarlib\optional\encache-1.2.3.jar (二级缓存)
lib\test\slf4j-log4j12.jar (hibernate注解安装包)
dist\spring.jardist\modules\spring-webmvc-struts.jarlib\jakarta-commons\commons-loggng.jarlib\jakarta-commons\commons-dbcp.jarlib\jakarta-commons\commons-pool.jarlib\cglib\cglib-nodep-2.1_3.jarlib\j2ee\common-annotations.jarlib\log4j-1.2.15.jar
gallery/image/112471.html">http://www.cnblogs.com/hongten/gallery/image/112471.html
com.b510.domain; 实体类 Person java.io.Serializable { serialVersionUID = -47270870639923184L; Integer id; String name; Integer age; String sex; Person() { } Person(String name) { .name = name; } Person(String name,Integer age,String sex) { .name = name; .age = age; .sex = sex; } Integer getId() { .id; } setId(Integer id) { .id = id; } String getName() { .name; } setName(String name) { .name = name; } Integer getAge() { .age; } setAge(Integer age) { .age = age; } String getSex() { .sex; } setSex(String sex) { .sex = sex; } }
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> < name="com.b510.domain.Person" table="person" catalog="spring"> java.lang.Integer"> ="increment" /> java.lang.String"> ="true" /> java.lang.Integer"> java.lang.String"> >
/spring+hibernate/src/com/b510/service/PersonService.java
com.b510.service; java.util.List; com.b510.domain.Person; PersonService { aram save(Person person); aram update(Person person); 获取Person aram Person getPerson(Integer id); 获取所有Person List getPerson(); 删除指定id的Person aram delete(Integer id); }
/
com.b510.service.impl; java.util.List; javax.annotation.Resource; org.hibernate.SessionFactory; org.springframework.transaction.annotation.Propagation; org.springframework.transaction.annotation.Transactional; com.b510.domain.Person; com.b510.service.PersonService; @Transactional PersonServiceBean PersonService { 配置文件按名称sessionFactory注入属性sessionFactory, @Resource SessionFactory sessionFactory; @Override delete(Integer id) { sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().load(Person.,id)); } 查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率 @Override @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = ) Person getPerson(Integer id) { (Person) sessionFactory.getCurrentSession() .get(Person.,id); } 查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率 @Override @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = ) @SuppressWarnings("unchecked") List getPerson() { sessionFactory.getCurrentSession().createquery("from Person") .list(); } @Override save(Person person) { sessionFactory.getCurrentSession().persist(person); } @Override update(Person person) { sessionFactory.getCurrentSession().merge(person); } }
com.b510.test; org.junit.BeforeClass; org.junit.Test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.b510.domain.Person; com.b510.service.PersonService; PersonServiceBeanTest { PersonService personService; @BeforeClass setUpBeforeClass() Exception { { ApplicationContext act = ClasspathXmlApplicationContext( "bean.xml"); personService = (PersonService) act.getBean("personService"); } (Exception e) { e.printstacktrace(); } } @Test testSave() { personService.save( Person("hongten",21,"男")); } @Test testUpdate() { Person person =personService.getPerson(2); person.setName("hanyuan"); person.setAge(21); person.setSex("男"); personService.update(person); } @Test testGetPersonInteger() { Person person = personService.getPerson(1); System.out.println(person.getId() + " " + person.getName() + " " + person.getAge() + " " + person.getSex()); } @Test testGetPerson() { java.util.List list = personService.getPerson(); System.out.println("*******************"); (Person person : list) { System.out.println(person.getId() + " " + person.getName() + " " + person.getAge() + " " + person.getSex()); } } @Test testDelete() { personService.delete(1); } }
配置器:
/spring+hibernate/src/bean.xml的另外一种配置方式是:要知道,程序执行是完全相同的。
<div class="cnblogs_code">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http:spring-beans-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/context/spring-context-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> ="org.springframework.orm.hibernate3.LocalSessionfactorybean"> com/b510/domain/Person.hbm.xml
hibernate.dialect=org.hibernate.dialect.MysqL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql= hibernate.format_sql= hibernate.cache.use_second_level_cache= hibernate.cache.use_query_cache= hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider ="org.springframework.orm.hibernate3.HibernateTransactionManager"> ="com.b510.service.impl.PersonServiceBean">