ssm整合
先创建spring框架
通过spring整合spring mvc
通过spring整合mybatis
创建项目结构
1.在pom.xml文件中加入坐标
1 <!--分页插件--> 2 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> 3 <dependency> 4 <groupId>com.github.pagehelper</groupId> 5 <artifactId>pagehelper</artifactId> 6 <version>5.1.2</version> 7 </dependency> 8 9 <!--逆向工程--> 10 <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> 11 <dependency> 12 <groupId>org.mybatis.generator</groupId> 13 <artifactId>mybatis-generator-core</artifactId> 14 <version>1.3.7</version> 15 </dependency> 16 17 <!--servlet api--> 18 <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --> 19 <dependency> 20 <groupId>javax.servlet</groupId> 21 <artifactId>servlet-api</artifactId> 22 <version>2.5</version> 23 <scope>provided</scope> 24 </dependency> 25 26 <!--jstl--> 27 <!-- https://mvnrepository.com/artifact/jstl/jstl --> 28 <dependency> 29 <groupId>jstl</groupId> 30 <artifactId>jstl</artifactId> 31 <version>1.2</version> 32 </dependency> 33 34 <!--mysql --> 35 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 36 <dependency> 37 <groupId>mysql</groupId> 38 <artifactId>mysql-connector-java</artifactId> 39 <version>5.1.40</version> 40 </dependency> 41 42 <!--c3p0--> 43 <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> 44 <dependency> 45 <groupId>c3p0</groupId> 46 <artifactId>c3p0</artifactId> 47 <version>0.9.1.2</version> 48 </dependency> 49 50 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-aspects</artifactId> 54 <version>4.3.7.RELEASE</version> 55 </dependency> 56 57 <!--myabtis -spring--> 58 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 59 <dependency> 60 <groupId>org.mybatis</groupId> 61 <artifactId>mybatis-spring</artifactId> 62 <version>1.3.2</version> 63 </dependency> 64 65 <!--myabtis--> 66 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 67 <dependency> 68 <groupId>org.mybatis</groupId> 69 <artifactId>mybatis</artifactId> 70 <version>3.4.2</version> 71 </dependency> 72 73 <!--spring--> 74 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-jdbc</artifactId> 78 <version>4.3.7.RELEASE</version> 79 </dependency> 80 81 <!--springmvc--> 82 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-webmvc</artifactId> 86 <version>4.3.7.RELEASE</version> 87 </dependency> 88 89 <!--支持ajax向页面传输数据的依赖--> 90 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 91 <dependency> 92 <groupId>com.fasterxml.jackson.core</groupId> 93 <artifactId>jackson-databind</artifactId> 94 <version>2.9.9.3</version> 95 </dependency> 96 97 <dependency> 98 <groupId>log4j</groupId> 99 <artifactId>log4j</artifactId> 100 <version>1.2.17</version> 101 </dependency> 102 103 <dependency> 104 <groupId>junit</groupId> 105 <artifactId>junit</artifactId> 106 <version>4.11</version> 107 <scope>test</scope> 108 </dependency>
2.web.xml
1 <web-app> 2 <display-name>Archetype Created Web Application</display-name> 3 <!--1.拦截字节编码的位置--> 4 <filter> 5 <filter-name>CharacterEncodingFilter</filter-name> 6 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 <init-param> 8 <param-name>encoding</param-name> 9 <param-value>utf-8</param-value> 10 </init-param> 11 <init-param> 12 <param-name>forceRequestEncoding</param-name> 13 <param-value>true</param-value> 14 </init-param> 15 <init-param> 16 <param-name>forceResponseEncoding</param-name> 17 <param-value>true</param-value> 18 </init-param> 19 </filter> 20 <filter-mapping> 21 <filter-name>CharacterEncodingFilter</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 <!--2.支持servlet运行的配置--> 26 <servlet> 27 <servlet-name>springDispatcherServlet</servlet-name> 28 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 29 </servlet> 30 <servlet-mapping> 31 <servlet-name>springDispatcherServlet</servlet-name> 32 <url-pattern>/</url-pattern> 33 </servlet-mapping> 34 35 <!--3.可以转换DELETE和PUT的配置--> 36 <filter> 37 <filter-name>HiddenHttpMethodFilter</filter-name> 38 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 39 </filter> 40 <filter-mapping> 41 <filter-name>HiddenHttpMethodFilter</filter-name> 42 <url-pattern>/*</url-pattern> 43 </filter-mapping> 44 45 <!--4.将内容加入到ioc容器中--> 46 <context-param> 47 <param-name>contextConfigLocation</param-name> 48 <param-value>classpath:applicationContext.xml</param-value> 49 </context-param> 50 <listener> 51 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 52 </listener> 53 </web-app>
3.db.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///test jdbc.user=root jdc.password=123456
4.mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!--设置别名--> 7 <typeAliases> 8 <package name="cn.kgc.entity"></package> 9 </typeAliases> 10 11 <!--分页插件--> 12 <plugins> 13 <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> 14 </plugins> 15 16 </configuration>
5.springDispatcherServlet-servlet.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!--设置别名--> 7 <typeAliases> 8 <package name="cn.kgc.entity"></package> 9 </typeAliases> 10 11 <!--分页插件--> 12 <plugins> 13 <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> 14 </plugins> 15 16 </configuration>
6.applicationContext.xml
1 <!--1:配置扫描不包含控制层的包--> 2 <context:component-scan base-package="cn.kgc"> 3 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> 4 </context:component-scan> 5 6 <!--2:导入外部文件,配置数据源--> 7 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> 8 9 <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> 10 <property name="driverClass" value="${jdbc.driverClass}"></property> 11 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 12 <property name="user" value="${jdbc.user}"></property> 13 <property name="password" value="${jdbc.password}"></property> 14 </bean> 15 16 <!--3:配置sqlSessionFactory--> 17 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 18 <property name="dataSource" ref="dataSource"></property> 19 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 20 <property name="mapperLocations" value="classpath:static/*.xml"></property> 21 </bean> 22 23 <!--4:配置sql的映射文件--> 24 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 25 <property name="basePackage" value="cn.kgc.mapper"></property> 26 </bean> 27 28 <!--5:配置事务管理--> 29 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 30 <property name="dataSource" ref="dataSource"></property> 31 </bean> 32 33 <!--6:配置aop--> 34 <aop:config> 35 <!-- // cn.kgc.service.PersonMapper.query(String,String);--> 36 <aop:pointcut id="txPointcut" expression="execution(* cn.kgc.service..*(..))"></aop:pointcut> 37 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor> 38 </aop:config> 39 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 40 <tx:attributes> 41 <tx:method name="*"/> 42 <tx:method name="get*" read-only="true"></tx:method> 43 </tx:attributes> 44 </tx:advice>