微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring开发_spring+hibernate

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); } }

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">

配置器:

/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"> 

driverClassName=org.gjt.mm.MysqL.Driver url=jdbc\:MysqL\:aracterEncoding\=UTF-8 username=root password=root initialSize=1 maxActive=300 maxIdle=2 minIdle=1

log4j:WARN No appenders Could be found logger (org.springframework.context.support.ClasspathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. Hibernate: select max(id) from person Hibernate: insert into spring.person (name,age,sex,id) values (?,?,?) Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.age as age0_0_, person0_.sex as sex0_0_ from spring.person person0_ where person0_.id=? Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.age as age0_0_, person0_.sex as sex0_0_ from spring.person person0_ where person0_.id=? Hibernate: select person0_.id as id0_, person0_.name as name0_, person0_.age as age0_, person0_.sex as sex0_ from spring.person person0_ ******************* 2 hanyuan 21 男 3 hongten 21 男 Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.age as age0_0_, person0_.sex as sex0_0_ from spring.person person0_ where person0_.id=?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐