首页 > 代码库 > SSM整合

SSM整合

第一步:添加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=http://www.mamicode.com/"${jdbc.driver}" />>

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=http://www.mamicode.com/"false">>
struts.xml

<?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=http://www.mamicode.com/"simple">>
log4j.properties

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-config.properties

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");
	}




SSM整合