实战----整合项目
2016-05-11 17:33
519 查看
设计代码分3层:
view-->service-->Dao
Struts2+hibernate+spring+JBPM+junit+jquery
一. 建数据库:
查看:
二. Myeclipse工程
添加junit的jar
添加框架环境, Struts2
1. 添加Struts2的jar (commons-fileupload, commons-io, freemarker, ognl, struts2-core, xwork-core), Struts.xml, web.xml
struts.xml:
alt+/可以提示, 但是不能自动.设置自动提示:
1. Preferences对话框中,选择“XML Catalog” ,
2. add目录:D:\Java\jar\struts-2.3.24.1\src\core\src\main\resources --> URI.
3. 设置xml关联编辑器: Preferences->file asso...->找到.xml, 设置myeclipse xml editor为默认.
2.配置 web.xml, 添加struts2的filter:
添加框架环境, hibernate: jar包, hibernate.cfg.xml, *.hbm.xml
jar包: 核心包, 必须包, jpa, c3p0, jdbc
hibernate.cfg.xml:
添加框架环境, spring: jar包, applicationContext.xml/beans.xml
applicationContext.xml:
整合spring和struts2:
新建class TestAction, 包cn.itcast.oa.test:
创建一个jsp:test.jsp, 有编码就改成utf-8
配置struts.xml:
test.jsp:
运行成功.
整合前测试spring:
1. 用于声明bean
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
2. 配置bean的生命周期: prototype, 不是单例.
详细步骤:
1. 新建类SpringTest.java:
2. 在testAction类前加入@Controller
3. 在SpringTest上运行 junit test , 成功!
还有一种方法: XML, 只需要删除testAction类前的@Controller 更改ApplicationContext.xml:
整合开始: 1. 在web.xml里配置spring的监听器 2. 加入整合jar包
4. 配置web.xml里的sping监听器:
5. 加入struts2-sping-plugin的jar包
6. 更改struts.xml:, 把action的class属性改成bean名称
整合成功测试test.jsp:
运行: http://localhost:8080/Itcastoa/test.action, 整合成功!!!
spring与struts2整合总结:
1. 加入整合jar包, struts2-sping-plugin的jar包,
在web.xml里配置spring的监听器
2. 在action上面加入@Controller:
3. 配置struts.xml文件, 就可以在配置action的class的时候直接使用 action名字
所以spring整合struts2的目的就是为了管理action , 如果action里想使用某个service文件的话, 直接注入@Resource就可以了.
hibernate+spring整合
整合目的:
1. spring管理sessionFactory实例(只需要一个)
2. 声明式事务管理
改配置文件:
1. 考虑到连接池的问题, 将hibernate配置文件里的jdbc配置注释掉:
2. 新增jdbc.properties, 加入mysql配置:
jdbcUrl=jdbc:mysql://localhost/itcastoa0720 driverClass=com.mysql.jdbc.Driver user=root password=linda0213
3. applicationContext.xml配置: 别忘了要导入外部properties文件
4. springtest.java里测试sessionFactory:
测试成功后进行声明式事务管理:
1. 配置applicationContext.xml
2. 新建user.java, 包: cn.itcast.oa.domain
3. User.hbm.xml:
4. 配置hibernate.cfg.xml里的mapping内容:
5. 开始写测试文件, 先添加一个TestService.java:
写测试文件:
测试文件test.jsp,
布置工程时出错, 导入slf-nop包解决!!
http://localhost:8080/Itcastoa/test.action后,
创建了新表, 把service里的异常删掉, 再执行, 保存进去两个数据. 并且从2,3开始计数id
到此整合完成!!!
spring和hibernate整合总结:
1. 新建jdbc.properties, 把数据库连接信息填入:
2. 配置hibernate.cfg.xml:
3. applicationContext.xml配置内容: 导入属性文件, sessionFactory, dataSource, transaction
4. 编写业务逻辑service类: 此时就可以注入容器里的sessionFactory了, 同时声明事务管理
5. 测试文件可以直接获取testService了:
到此 spring和hibernate整合完成,
再验证struts整合:
action调用testService测试:
浏览器输入http://localhost:8080/itcastoa/test.action验证成功!!!
view-->service-->Dao
Struts2+hibernate+spring+JBPM+junit+jquery
一. 建数据库:
create database itcastoa0720 default character set utf8;
查看:
show create database itcastoa0720
二. Myeclipse工程
1. 新建web工程, 工程右键属性, 编码设为utf-8 2. 添加框架环境, Struts2: jar, struts.xml, web.xml hibernate: jar, hibernate.cfg.xml, *.hbm.xml spring:jar, applicationContext junit: jar 3. 整合SSH Struts2+spring整合: 目的, spring负责主要IOC(管理对象), AOP(事务管理), 就为了让spring容器来管理struts的action hibernate+spring整合: 目的, sessionFactory是创建session的, factory就需要一个, 管理一个工厂的话,用spring来管理一个单例工厂, 最重要的是, 声明式事务管理 4. 资源分类 5. 配置日志
添加junit的jar
添加框架环境, Struts2
1. 添加Struts2的jar (commons-fileupload, commons-io, freemarker, ognl, struts2-core, xwork-core), Struts.xml, web.xml
struts.xml:
alt+/可以提示, 但是不能自动.设置自动提示:
1. Preferences对话框中,选择“XML Catalog” ,
2. add目录:D:\Java\jar\struts-2.3.24.1\src\core\src\main\resources --> URI.
3. 设置xml关联编辑器: Preferences->file asso...->找到.xml, 设置myeclipse xml editor为默认.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置扩展名为action --> <constant name="struts.action.extension" value="action" /> <!--把主题配置为simple, --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> </package> <!-- Add packages here --> </struts>
2.配置 web.xml, 添加struts2的filter:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
添加框架环境, hibernate: jar包, hibernate.cfg.xml, *.hbm.xml
jar包: 核心包, 必须包, jpa, c3p0, jdbc
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">bjsxt</property> <!-- 2. other configuration --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="connection.pool_size">1</property> <!--3. mapping--> <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/> <mapping class="com.bjsxt.hibernate.Teacher"/> </session-factory> </hibernate-configuration>
添加框架环境, spring: jar包, applicationContext.xml/beans.xml
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="cn.itcast.oa" /> </beans>
整合spring和struts2:
新建class TestAction, 包cn.itcast.oa.test:
package cn.itcast.oa.test; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{ @Override public String execute() throws Exception { System.out.println("===>TestAction.execute()"); return "success"; } }
创建一个jsp:test.jsp, 有编码就改成utf-8
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> Struts2添加成功. <br> </body> </html>
配置struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置扩展名为action --> <constant name="struts.action.extension" value="action" /> <!--把主题配置为simple, --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!-- 配置 测试用的action, 还没有和spring整合, class属性写全 名--> <action name="test" class="cn.itcast.oa.test.TestAction"> <result name="success">/test.jsp</result> </action> </package> <!-- Add packages here --> </struts>
test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> Struts2添加成功. <br> </body> </html>
运行成功.
整合前测试spring:
1. 用于声明bean
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
2. 配置bean的生命周期: prototype, 不是单例.
详细步骤:
1. 新建类SpringTest.java:
package cn.itcast.oa.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } }
2. 在testAction类前加入@Controller
package cn.itcast.oa.test; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") public class TestAction extends ActionSupport{ @Override public String execute() throws Exception { System.out.println("===>TestAction.execute()"); return "success"; } }
3. 在SpringTest上运行 junit test , 成功!
package cn.itcast.oa.test; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } }
还有一种方法: XML, 只需要删除testAction类前的@Controller 更改ApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa.test" /> <bean id="testAction" class = "cn.itcast.oa.test.TestAction" scope="prototype"></bean> </beans>
整合开始: 1. 在web.xml里配置spring的监听器 2. 加入整合jar包
4. 配置web.xml里的sping监听器:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 配置spring用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 配置struts2的核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5. 加入struts2-sping-plugin的jar包
6. 更改struts.xml:, 把action的class属性改成bean名称
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置扩展名为action --> <constant name="struts.action.extension" value="action" /> <!--把主题配置为simple, --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!-- 配置 测试用的action, 还没有和spring整合, class属性写全 名--> <!-- 当struts2余spring整合后,class属性可以写bean的名称 --> <!--<action name="test" class="cn.itcast.oa.test.TestAction"> --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> </package> <!-- Add packages here --> </struts>
整合成功测试test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> Struts2添加成功. <br> Struts2与spring整合成功. <br> </body> </html>
运行: http://localhost:8080/Itcastoa/test.action, 整合成功!!!
spring与struts2整合总结:
1. 加入整合jar包, struts2-sping-plugin的jar包,
在web.xml里配置spring的监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param>
2. 在action上面加入@Controller:
package cn.itcast.oa.test; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") public class TestAction extends ActionSupport{ @Override public String execute() throws Exception { System.out.println("===>TestAction.execute()"); return "success"; } }
3. 配置struts.xml文件, 就可以在配置action的class的时候直接使用 action名字
<action name="test" class="testAction"> <result name="success">/test.jsp</result> </action>
所以spring整合struts2的目的就是为了管理action , 如果action里想使用某个service文件的话, 直接注入@Resource就可以了.
hibernate+spring整合
整合目的:
1. spring管理sessionFactory实例(只需要一个)
2. 声明式事务管理
改配置文件:
1. 考虑到连接池的问题, 将hibernate配置文件里的jdbc配置注释掉:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">bjsxt</property> --> <!-- 2. other configuration --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="connection.pool_size">1</property> <!--3. mapping <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/> <mapping class="com.bjsxt.hibernate.Teacher"/>--> </session-factory> </hibernate-configuration>
2. 新增jdbc.properties, 加入mysql配置:
jdbcUrl=jdbc:mysql://localhost/itcastoa0720 driverClass=com.mysql.jdbc.Driver user=root password=linda0213
3. applicationContext.xml配置: 别忘了要导入外部properties文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa" /> <!-- <bean id="testAction" class = "cn.itcast.oa.test.TestAction"></bean> --> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定 hibernate 配置文件路径 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!-- 初始化时获取3个连接, 取值应该在minPoolSize与maxPoolSize之间, default:3 --> <property name="initialPoolSize" value="3"></property> <!-- 连接池中保留的最小连接数, default:3 --> <property name="minPoolSize" value="3"></property> <!-- 连接池中保留的最大连接数, default:15 --> <property name="maxPoolSize" value="5"></property> <!-- 当连接池中的连接耗尽的时候, c3p0一次同时获取的连接数, default:3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量, 如果maxStatements与maxStatementsPerConnection均为0, 则缓存被关闭, default:0 --> <property name="maxStatements" value="8"></property> <!-- maxStatements与maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数, default:0 --> <property name="maxStatementsPerConnection" value="5"></property> <!-- 最大空闲时间, 1800秒内未使用则连接被丢弃,若为0则永不丢弃,default:0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> </bean> </beans>
4. springtest.java里测试sessionFactory:
package cn.itcast.oa.test; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } @Test public void testSessionFactory() throws Exception{ SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory"); System.out.println(sessionFactory); } }
测试成功后进行声明式事务管理:
1. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa" /> <!-- <bean id="testAction" class = "cn.itcast.oa.test.TestAction"></bean> --> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定 hibernate 配置文件路径 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!-- 初始化时获取3个连接, 取值应该在minPoolSize与maxPoolSize之间, default:3 --> <property name="initialPoolSize" value="3"></property> <!-- 连接池中保留的最小连接数, default:3 --> <property name="minPoolSize" value="3"></property> <!-- 连接池中保留的最大连接数, default:15 --> <property name="maxPoolSize" value="5"></property> <!-- 当连接池中的连接耗尽的时候, c3p0一次同时获取的连接数, default:3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量, 如果maxStatements与maxStatementsPerConnection均为0, 则缓存被关闭, default:0 --> <property name="maxStatements" value="8"></property> <!-- maxStatements与maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数, default:0 --> <property name="maxStatementsPerConnection" value="5"></property> <!-- 最大空闲时间, 1800秒内未使用则连接被丢弃,若为0则永不丢弃,default:0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> </bean> <!-- 配置声明式事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
2. 新建user.java, 包: cn.itcast.oa.domain
package cn.itcast.oa.domain; public class User { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3. User.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"> <class name="User" table="itcast_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> </class> </hibernate-mapping>
4. 配置hibernate.cfg.xml里的mapping内容:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">bjsxt</property> --> <!-- 2. other configuration --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="connection.pool_size">1</property> <!--3. mapping --> <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
5. 开始写测试文件, 先添加一个TestService.java:
package cn.itcast.oa.test; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.oa.domain.User; @Service public class TestService { @Resource private SessionFactory sessionFactory; @Transactional public void saveTwoUsers(){ Session session = sessionFactory.getCurrentSession(); session.save(new User()); int a = 1/0; //exception session.save(new User()); } }
写测试文件:
package cn.itcast.oa.test; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } @Test public void testSessionFactory() throws Exception{ SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory"); System.out.println(sessionFactory); } @Test public void testTransaction() throws Exception { TestService testService = (TestService) ac.getBean("testService"); testService.saveTwoUsers(); } }
测试文件test.jsp,
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> Struts2添加成功. <br> Struts2与spring整合成功. <br> Struts2与spring与hibernate整合成功. <br> </body> </html>
布置工程时出错, 导入slf-nop包解决!!
http://localhost:8080/Itcastoa/test.action后,
创建了新表, 把service里的异常删掉, 再执行, 保存进去两个数据. 并且从2,3开始计数id
到此整合完成!!!
spring和hibernate整合总结:
1. 新建jdbc.properties, 把数据库连接信息填入:
jdbcUrl=jdbc:mysql://localhost/itcastoa0720 driverClass=com.mysql.jdbc.Driver user=root password=linda0213
2. 配置hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 2. other configuration --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="connection.pool_size">1</property> <!--3. mapping --> <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
3. applicationContext.xml配置内容: 导入属性文件, sessionFactory, dataSource, transaction
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa" /> <!-- <bean id="testAction" class = "cn.itcast.oa.test.TestAction"></bean> --> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定 hibernate 配置文件路径 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!-- 初始化时获取3个连接, 取值应该在minPoolSize与maxPoolSize之间, default:3 --> <property name="initialPoolSize" value="3"></property> <!-- 连接池中保留的最小连接数, default:3 --> <property name="minPoolSize" value="3"></property> <!-- 连接池中保留的最大连接数, default:15 --> <property name="maxPoolSize" value="5"></property> <!-- 当连接池中的连接耗尽的时候, c3p0一次同时获取的连接数, default:3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量, 如果maxStatements与maxStatementsPerConnection均为0, 则缓存被关闭, default:0 --> <property name="maxStatements" value="8"></property> <!-- maxStatements与maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数, default:0 --> <property name="maxStatementsPerConnection" value="5"></property> <!-- 最大空闲时间, 1800秒内未使用则连接被丢弃,若为0则永不丢弃,default:0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> </bean> <!-- 配置声明式事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
4. 编写业务逻辑service类: 此时就可以注入容器里的sessionFactory了, 同时声明事务管理
@Service public class TestService { @Resource private SessionFactory sessionFactory; @Transactional public void saveTwoUsers(){ Session session = sessionFactory.getCurrentSession(); session.save(new User()); //int a = 1/0; //exception session.save(new User()); } }
5. 测试文件可以直接获取testService了:
public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testTransaction() throws Exception { TestService testService = (TestService) ac.getBean("testService"); testService.saveTwoUsers(); } }
到此 spring和hibernate整合完成,
再验证struts整合:
action调用testService测试:
@Controller @Scope("prototype") public class TestAction extends ActionSupport{ @Resource private TestService testService; @Override public String execute() throws Exception { System.out.println("===>TestAction.execute()"); testService.saveTwoUsers(); return "success"; } }
浏览器输入http://localhost:8080/itcastoa/test.action验证成功!!!
Struts2添加成功. Struts2与spring整合成功. Struts2与spring与hibernate整合成功.
相关文章推荐
- configs for postgresql restart and postgresql reload
- ios关于用xib创建的cell 自动返回cell的高度问题!
- C# SQL语句参数化,通过包含like关键字的子句查询无结果问题
- 让人感觉点击到的圆角按钮
- Redhat 7.2 中文显示及中文输入法设置
- 树莓派3利用create_ap软件变身无线AP
- Hibeinate: Could not determine type for: java.util.List, at table: address_inf, for columns: [org.hi
- 程序员必读的书,StackOverflow 创始人推荐Recommended Reading for Developers
- 本地ie版本大于7.0,MsHtml中get_userAgent获取UA为MSIE7
- ThinkPHP查询数据库表中包含在某一结果集中的所有内容
- [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [一] 初衷与架构设计
- java内存溢出和内存泄露
- 盖得化工--采集所有公司详细信息
- fragment实现手势识别
- Android 发送广播传送数据
- 使用JQuery进行cookie操作
- 《极简欧洲史》 读后思考
- 数据结构实验之链表四:有序链表的归并
- 盖得化工--采集二级网址的公司详细信息
- nginx模块开发(18)—日志分析