spring开发_spring+hibernate

spring开发_spring+hibernate,第1张

概述java 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

href="http://www.cnblogs.com/hongten/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"> ="increment" /> ="true" /> >

/spring+hibernate/src/com/b510/service/PersonService.java

com.b510.service; java.util.List; com.b510.domain.Person; PersonService { save(Person person); update(Person person); Person getPerson(Integer ID); List getPerson(); 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 { @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: http: http: http: ="org.apache.commons.dbcp.BasicdataSource" destroy-method="close"> @H_404_622@ ="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 >

       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:            http:            http:            http:                          ="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\: 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=? 总结

以上是内存溢出为你收集整理的spring开发_spring+hibernate全部内容,希望文章能够帮你解决spring开发_spring+hibernate所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270037.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存