SSM框架整合
1.0新建Maven项目,添加依赖
< jsp-api.version> 2.0</ jsp-api.version>
< jackson.version> 2.9.6</ jackson.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.6.11</ version>
</ dependency> < dependency>
< groupId> org.json</ groupId>
< artifactId> json</ artifactId>
< version> 20140107</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-beans</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jms</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context-support</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> ${mybatis.version}</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> ${mybatis.spring.version}</ version>
</ dependency>
< dependency>
< groupId> com.github.miemiedev</ groupId>
< artifactId> mybatis-paginator</ artifactId>
< version> ${mybatis.paginator.version}</ version>
</ dependency>
< dependency>
< groupId> com.github.pagehelper</ groupId>
< artifactId> pagehelper</ artifactId>
< version> ${pagehelper.version}</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> ${mysql.version}</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> ${druid.version}</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> jstl</ groupId>
< artifactId> jstl</ artifactId>
< version> ${jstl.version}</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.0.1</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jsp-api</ artifactId>
< scope> provided</ scope>
< version> ${jsp-api.version}</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> ${jackson.version}</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> fastjson</ artifactId>
< version> 1.2.28</ version>
</ dependency>
< dependency>
< groupId> commons-io</ groupId>
< artifactId> commons-io</ artifactId>
< version> 2.4</ version>
</ dependency>
< dependency>
< groupId> commons-fileupload</ groupId>
< artifactId> commons-fileupload</ artifactId>
< version> 1.3.1</ version>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-compiler-plugin</ artifactId>
< configuration>
< source> 1.8</ source>
< target> 1.8</ target>
< encoding> UTF-8</ encoding>
</ configuration>
</ plugin>
</ plugins>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
</ build>
1.1拷贝所有的配置文件到config,开发每个配置文件
< context: property-placeholder location = " classpath:jdbc.properties" > </ context: property-placeholder>
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.driver}" > </ property>
< property name = " url" value = " ${jdbc.url}" > </ property>
< property name = " username" value = " ${jdbc.username}" > </ property>
< property name = " password" value = " ${jdbc.password}" > </ property>
</ bean>
< bean class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" > </ property>
< property name = " configLocation" value = " classpath:SqlMapConfig.xml" > </ property>
< property name = " typeAliasesPackage" value = " com.bjpowernode.pojo" > </ property>
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.bjpowernode.mapper" > </ property>
</ bean>
1.1.1 applicationContext-service.xml
< context: component-scan base-package = " com.bjpowernode.service" > </ context: component-scan>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< tx: advice id = " myadvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *select*" read-only = " true" />
< tx: method name = " *find*" read-only = " true" />
< tx: method name = " *get*" read-only = " true" />
< tx: method name = " *search*" read-only = " true" />
< tx: method name = " *insert*" propagation = " REQUIRED" />
< tx: method name = " *save*" propagation = " REQUIRED" />
< tx: method name = " *add*" propagation = " REQUIRED" />
< tx: method name = " *delete*" propagation = " REQUIRED" />
< tx: method name = " *remove*" propagation = " REQUIRED" />
< tx: method name = " *clear*" propagation = " REQUIRED" />
< tx: method name = " *update*" propagation = " REQUIRED" />
< tx: method name = " *modify*" propagation = " REQUIRED" />
< tx: method name = " *change*" propagation = " REQUIRED" />
< tx: method name = " *set*" propagation = " REQUIRED" />
< tx: method name = " *" propagation = " SUPPORTS" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " mypointcut" expression = " execution(* com.bjpowernode.service.*.*(..))" />
< aop: advisor advice-ref = " myadvice" pointcut-ref = " mypointcut" > </ aop: advisor>
</ aop: config>
1.1.2 Springmvc.xml
< context: component-scan base-package = " com.bjpowernode.controller" > </ context: component-scan>
< bean id = " viewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /admin/" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
</ bean>
< mvc: annotation-driven> </ mvc: annotation-driven>
< mvc: cors>
< mvc: mapping path = " /**"
allowed-origins = " *"
allowed-methods = " POST, GET, OPTIONS, DELETE, PUT"
allowed-headers = " Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
allow-credentials = " true" />
</ mvc: cors>
1.1.3 applicationContext_mapper
<?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"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " classpath:jdbc.properties" > </ context: property-placeholder>
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.driverClassName}" > </ property>
< property name = " url" value = " ${jdbc.url}" > </ property>
< property name = " username" value = " ${jdbc.username}" > </ property>
< property name = " password" value = " ${jdbc.password}" > </ property>
</ bean>
< bean class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" > </ property>
< property name = " configLocation" value = " classpath:SqlMapConfig.xml" > </ property>
< property name = " typeAliasesPackage" value = " com.bjpowernode.pojo" > </ property>
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.bjpowernode.mapper" > </ property>
</ bean>
</ beans>
1.1.4 jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
1.1.5 SqlMapConfig.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 = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
</ configuration>
1.2在web.xml文件中完成springmvc,spring两个框架的注册
< filter>
< filter-name> encode</ filter-name> < filter-class> org.springframework.web.filter.CharacterEncodingFilter</ filter-class>
< init-param>
< param-name> encoding</ param-name>
< param-value> UTF-8</ param-value>
</ init-param>
< init-param>
< param-name> forceRequestEncoding</ param-name>
< param-value> true</ param-value>
</ init-param>
< init-param>
< param-name> forceResponseEncoding</ param-name>
< param-value> true</ param-value>
</ init-param>
</ filter>
< filter-mapping>
< filter-name> encode</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
< servlet>
< servlet-name> springmvc</ servlet-name> < servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:springmvc.xml</ param-value>
</ init-param>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< 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>
1.4创建UserMapper接口和实现功能的UserMapper.xml
1.4.1 UserMapper
package com. bjpowernode. mapper ;
import com. bjpowernode. pojo. User ;
import org. apache. ibatis. annotations. Param ;
import java. util. List ;
public interface UserMapper {
List < User > selectUserPage (
@Param ( "userName" )
String userName,
@Param ( "userSex" )
String userSex,
@Param ( "startRow" )
int startRow) ;
int createUser ( User user) ;
int deleteUserById ( String userId) ;
int getRowCount (
@Param ( "userName" )
String userName,
@Param ( "userSex" )
String userSex) ;
}
1.4.2 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.bjpowernode.mapper.UserMapper" >
< resultMap id = " usermap" type = " user" >
< id property = " userId" column = " user_id" > </ id>
< result property = " cardType" column = " card_type" > </ result>
< result property = " cardNo" column = " card_no" > </ result>
< result property = " userName" column = " user_name" > </ result>
< result property = " userSex" column = " user_sex" > </ result>
< result property = " userAge" column = " user_age" > </ result>
< result property = " userRole" column = " user_role" > </ result>
</ resultMap>
< sql id = " allColumns" >
user_id,card_type,card_no,user_name,user_sex,user_age,user_role
</ sql>
< select id = " selectUserPage" resultMap = " usermap" >
select < include refid = " allColumns" > </ include>
from user
< where>
< if test = " userName != null and userName != ' ' " >
and user_name like concat('%',#{userName},'%')
</ if>
< if test = " userSex != null and userSex != ' ' " >
and user_sex = #{userSex}
</ if>
</ where>
limit #{startRow},5
</ select>
< insert id = " createUser" parameterType = " user" >
insert into user values(#{userId},#{cardType},#{cardNo},#{userName},#{userSex},#{userAge},#{userRole})
</ insert>
< delete id = " deleteUserById" parameterType = " string" >
delete from user where user_id = #{userId}
</ delete>
< select id = " getRowCount" resultType = " int" >
select count(*)
from user
< where>
< if test = " userName != null and userName != ' ' " >
and user_name like concat('%',#{userName},'%')
</ if>
< if test = " userSex != null and userSex != ' ' " >
and user_sex = #{userSex}
</ if>
</ where>
</ select>
</ mapper>
1.5创建业务逻辑层UserService接口和实现类
1.5.1 UserService
package com. bjpowernode. service ;
import com. bjpowernode. pojo. User ;
import java. util. List ;
public interface UserService {
List < User > selectUserPage ( String userName, String userSex, int startRow) ;
int createUser ( User user) ;
int deleteUserById ( String userId) ;
int getRowCount ( String userName, String userSex) ;
}
1.5.2 UserServiceImpl
package com. bjpowernode. service. impl ;
import com. bjpowernode. mapper. UserMapper ;
import com. bjpowernode. pojo. User ;
import com. bjpowernode. service. UserService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List < User > selectUserPage ( String userName, String userSex, int startRow) {
return userMapper. selectUserPage ( userName, userSex, startRow) ;
}
@Override
public int createUser ( User user) {
return userMapper. createUser ( user) ;
}
@Override
public int deleteUserById ( String userId) {
return userMapper. deleteUserById ( userId) ;
}
@Override
public int getRowCount ( String userName, String userSex) {
return userMapper. getRowCount ( userName, userSex) ;
}
}
1.6创建测试类进行功能测试
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( locations = { "classpath:applicationContext_dao.xml" , "classpath:applicationContext_service.xml" } )
public class MyTest {
@Autowired
UserService userService;
@Test
public void testQueryUserPage ( ) {
List < User > list = userService. selectUserPage ( null , null , 1 ) ;
list. forEach ( user -> System . out. println ( user) ) ;
}
@Test
public void testGetRowconunt ( ) {
Integer num = userService. getRowCount ( "" , "" ) ;
System . out. println ( num) ;
}
@Test
public void testDelete ( ) {
Integer num = userService. deleteUserById ( "15968162087363060" ) ;
System . out. println ( num) ;
}
}
1.7创建action进行分页显示,查询用户个数,删除处理
@CrossOrigin
@RestController
@RequestMapping ( "/user" )
public class UserController {
@Autowired
private UserService userService;
@RequestMapping ( "/queryUserPage" )
public List < User > queryUserPage ( Integer page) {
int pageNow = page == null ? 1 : page;
int pageSize = 5 ;
int startRows = pageSize* ( pageNow- 1 ) ;
return userService. queryUserPage ( startRows) ;
}
@RequestMapping ( "/selectUserPage" )
public List < User > selectUserPage ( String userName, String userSex, Integer page) {
int pageNow = page == null ? 1 : page;
int pageSize = 5 ;
int startRows = pageSize* ( pageNow- 1 ) ;
return userService. selectUserPage ( userName, userSex, startRows) ;
}
@RequestMapping ( "/getRowCount" )
public Integer getRowCount ( String userName, String userSex) {
return userService. getRowCount ( userName, userSex) ;
}
@RequestMapping ( "/createUser" )
public Integer createUser ( User user) {
Random random = new Random ( ) ;
Integer number = random. nextInt ( 9000 ) + 1000 ;
user. setUserId ( System . currentTimeMillis ( ) + String . valueOf ( number) ) ;
return userService. createUser ( user) ;
}
@RequestMapping ( "/deleteUserById" )
public Integer deleteUserById ( String userId) {
return userService. deleteUserById ( userId) ;
}
@RequestMapping ( value = "/deleteUserByIdList" )
public Integer deleteUserByIdList ( String userIdList) {
String userIdListSub = userIdList. substring ( 0 , userIdList. length ( ) - 1 ) ;
List userIds = new ArrayList ( ) ;
for ( String userIdStr: userIdListSub. split ( "," ) ) {
userIds. add ( userIdStr. trim ( ) ) ;
}
return userService. deleteUserByIdList ( userIds) ;
}
@RequestMapping ( "/updateUserById" )
public Integer updateUserById ( User user, Date date) {
return userService. updateUserById ( user) ;
}
}
1.8总览图
1.9SSM的整合模板
1.9.1 SqlMapConfig.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>
</ configuration>
1.9.2 springmvc.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: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
</ beans>
1.9.3 jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
1.9.4 applicationContext-service.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"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
</ beans>
1.9.5 applicationContext-dao.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"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
</ beans>