国外的服务器网站长丰住房和建设局网站
web/
2025/9/29 20:43:23/
文章来源:
国外的服务器网站,长丰住房和建设局网站,临沂网站建设那家好,seo有哪些经典的案例一、为什么要整合 1#xff0c;管理SessionFactory实例#xff08;只需要一个#xff09; 2#xff0c;声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二、整合步骤 1.整合sessionFactory 在applicationContext.xml添加 1 !-- 导入外部的properties文件 …一、为什么要整合 1管理SessionFactory实例只需要一个 2声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二、整合步骤 1.整合sessionFactory 在applicationContext.xml添加 1 !-- 导入外部的properties文件 --2 context:property-placeholder locationclasspath:jdbc.properties/3 4 !-- 配置SessionFactory --5 !-- bean默认是单例的 --6 bean idsessionFactory classorg.springframework.orm.hibernate3.LocalSessionFactoryBean7 !-- 指定hibernate的配置文件位置 --8 property nameconfigLocation valueclasspath:hibernate.cfg.xml/property9 !-- 配置c3p0数据库连接池 --
10 property namedataSource
11 !-- 因为连接信息只有连接池用所以配置在匿名的bean中 --
12 bean classcom.mchange.v2.c3p0.ComboPooledDataSource
13 !-- 数据连接信息 --
14 property namejdbcUrl value${jdbcUrl}/property
15 property namedriverClass value${driverClass}/property
16 property nameuser value${user}/property
17 property namepassword value${password}/property
18 !-- 其他配置 --
19 !--初始化时获取三个连接取值应在minPoolSize与maxPoolSize之间。Default: 3 --
20 property nameinitialPoolSize value3/property
21 !--连接池中保留的最小连接数。Default: 3 --
22 property nameminPoolSize value3/property
23 !--连接池中保留的最大连接数。Default: 15 --
24 property namemaxPoolSize value5/property
25 !--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --
26 property nameacquireIncrement value3/property
27 !-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0则缓存被关闭。Default: 0 --
28 property namemaxStatements value8/property
29 !--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --
30 property namemaxStatementsPerConnection value5/property
31 !--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --
32 property namemaxIdleTime value1800/property
33 /bean
34 /property
35 /bean 2.配置声明式事务管理 在applicationContext.xml添加 1 !-- 配置声明式事务管理采用注解的方式,方便--
2 bean idtxManager classorg.springframework.orm.hibernate3.HibernateTransactionManager
3 property namesessionFactory refsessionFactory/property
4 /bean
5 tx:annotation-driven transaction-managertxManager/ 3.测试 TestService.java 1 Service(testService)2 public class TestService {3 4 Resource5 private SessionFactory sessionFactory;6 7 Transactional8 public void saveTwoUsers() {9 Session session sessionFactory.getCurrentSession();
10
11 session.save(new User(李白));
12 //int a 1 / 0; // 这行会抛异常
13 session.save(new User(杜甫));
14 }
15 } TestAction.java 1 //Component(testAction)2 //Service3 //Repository4 Controller(testAction)5 Scope(prototype)6 public class TestAction extends ActionSupport {7 8 Resource9 private TestService testService;
10
11 Override
12 public String execute() throws Exception {
13 System.out.println(--- TestAction.execute());
14 testService.saveTwoUsers();
15 return success;
16 }
17 } 3.SpringTest.java 1 public class SpringTest {2 3 private ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);4 5 Test6 public void testBean() throws Exception {7 TestAction testAction (TestAction) ac.getBean(testAction);8 System.out.println(testAction);9 }
10
11 // 测试SessionFactory
12 Test
13 public void testSessionFactory() throws Exception {
14 SessionFactory sessionFactory (SessionFactory) ac.getBean(sessionFactory);
15 System.out.println(sessionFactory);
16 }
17
18 // 测试事务
19 Test
20 public void testTransaction() throws Exception {
21 TestService testService (TestService) ac.getBean(testService);
22 testService.saveTwoUsers();
23 }
24 } 4.User.hbm.xml 1 ?xml version1.0?2 !DOCTYPE hibernate-mapping PUBLIC3 -//Hibernate/Hibernate Mapping DTD 3.0//EN4 http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd5 6 hibernate-mapping packagecn.itcast.oa.domain7 8 class nameUser tableitcast_user9 id nameid
10 generator classnative/
11 /id
12 property namename /
13 /class
14
15 /hibernate-mapping 转载于:https://www.cnblogs.com/shamgod/p/5225454.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84083.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!