第一步:添加jar包
第二步:配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssm_demo1</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> </web-app>
application-context.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 容易受到环境影响的配置 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 数据库配置 --> <value>classpath:jdbc-config.properties</value> </list> </property> </bean> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <!--连接池中保留的最小连接数。 --> <property name="minPoolSize" value="${jdbc.minPoolSize}" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <!--最大空闲时间,120秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> <property name="maxStatements" value="0" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="${jdbc.acquireIncrement}" /> <property name="breakAfterAcquireFailure" value="false" /> <!--超时检测 --> <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" /> <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" /> <!-- <property name="preferredTestQuery"> <value>SELECT 1 from tb_test</value> </property>--> <property name="testConnectionOnCheckin" value="true" /> <property name="automaticTestTable" value="C3P0TestTable" /> </bean> <!-- 扫描,装配 --> <context:component-scan base-package="cs.ssm" use-default-filters="false"> <!-- 扫描符合@Controller @Repository的类--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- MyBatis与Spring整合的配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"> <value>classpath:mybatis-config.xml</value> </property> <!-- 自动扫描MyBatis映射配置文件 --> <property name="mapperLocations"> <list> <value>classpath:cs/ssm/mappers/*Mapper.xml</value> </list> </property> <!-- 自动为实体类取别名 --> <property name="typeAliasesPackage" value="cs.ssm.model" /> </bean> <!--事务管理 : DataSourceTransactionManager --> <bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用声明式事务 --> <!--<tx:annotation-driven transaction-manager="manager" />--> <!-- 事务传播规则 --> <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="find*">readOnly</prop> <prop key="save*">readOnly</prop> <prop key="add*">readOnly</prop> <prop key="insert*">readOnly</prop> <prop key="remove*">readOnly</prop> <prop key="show*">readOnly</prop> <prop key="select*">readOnly</prop> <prop key="update*">readOnly</prop> <prop key="delete*">readOnly</prop> <prop key="get*">readOnly</prop> <prop key="select*">readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- spring自动扫描指定包下的dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cs.ssm.dao" /> <property name="markerInterface" value="cs.ssm.dao.BaseDao" /> </bean> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="lazyLoadingEnabled" value="false"></setting> <setting name="aggressiveLazyLoading" value="false"></setting> <setting name="autoMappingBehavior" value="FULL"></setting> </settings> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.devMode" value="true"></constant> <constant name="struts.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default"> <!-- 岗位管理 --> <action name="role_*" class="roleAction" method="{1}"> <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result> <result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result> <result name="toList" type="redirectAction">role_list</result> </action> </package> </struts>
log4j.rootLogger=debug,Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%-5p %c %x - %m%n log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.com.opensymphony.xwork2=ERROR log4j.logger.org.springframework=ERROR log4j.logger.org.apache.struts2=ERROR log4j.logger.com.mchange.v2=ERROR
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost/demo1?characterEncoding\=utf-8 #jdbc.url=jdbc:informix-sqli://192.168.11.108:9088/rqzrbx:INFORMIXSERVER=ol_ids_1150_1;NEWLOCALE=zh_CN,zh_CN;NEWCODESET=GB18030,8859-1,819;IFX_USE_STRENC=ture; jdbc.user=root jdbc.password=1234 #jdbc.password=ssinformix jdbc.initialPoolSize=3 jdbc.minPoolSize=1 jdbc.maxPoolSize=150 jdbc.maxIdleTime=120 jdbc.maxStatements=0 jdbc.acquireIncrement=5 jdbc.idleConnectionTestPeriod=2000 jdbc.checkoutTimeout=8000
第三步测试:
private ApplicationContext ac = new ClassPathXmlApplicationContext("application-context.xml"); //测试dao @Test public void testDao() { RoleMapper roleMapper = (RoleMapper) ac.getBean("roleMapper"); Role role = roleMapper.selectByPrimaryKey(1); System.out.println(role.getDescription()); int num = roleMapper.insert(new Role("abc","哈哈")); System.out.println(num); } //测试service和事务 @Test public void testService() { RoleService roleService = (RoleService) ac.getBean("roleServiceImpl"); roleService.save(new Role("abc","哈哈")); // List<Role> list = roleService.getByIds(new int [] {1,2,3,4,5}); // for(Role role : list) { // System.out.println(role.getName() + " " + role.getDescription()); // } } //测试action @Test public void testAction() { RoleAction roleAction = (RoleAction) ac.getBean("roleAction"); }
原文地址:http://blog.csdn.net/cs_12/article/details/42638607