加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring开发_spring+hibernate

发布时间:2020-12-15 01:52:25 所属栏目:大数据 来源:网络整理
导读:hibernate3.jarlibrquired*.jarliboptionalencache-1.2.3.jar??? (二级缓存) libtestslf4j-log4j12.jar??? (hibernate注解安装包) distspring.jardistmodulesspring-webmvc-struts.jarlibjakarta-commonscommons-loggng.jarlibjakarta-commonsc

hibernate3.jarlibrquired*.jarliboptionalencache-1.2.3.jar??? (二级缓存)

libtestslf4j-log4j12.jar??? (hibernate注解安装包)

distspring.jardistmodulesspring-webmvc-struts.jarlibjakarta-commonscommons-loggng.jarlibjakarta-commonscommons-dbcp.jarlibjakarta-commonscommons-pool.jarlibcglibcglib-nodep-2.1_3.jarlibj2eecommon-annotations.jarliblog4j-1.2.15.jar

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"> ="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:            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=?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读