spring开发_spring+hibernate
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">
/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
/ 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
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
/spring+hibernate/src/bean.xml的另外一种配置方式是:要知道,程序执行是完全相同的。 <div class="cnblogs_code">
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=?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |