略
Spring MVC + Spring + MyBatis 整合
目录结构
1 | HelloSSM. |
pom.xml
测试相关
1
2
3
4
5
6<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>日志相关
1
2
3
4
5<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>数据库相关
1
2
3
4
5
6
7
8
9
10
11<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>DAO层相关
1
2
3
4
5
6
7
8
9
10<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>Servlet相关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>Spring相关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50<!-- 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<!-- web相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<!-- 测试相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>实体类简化
1
2
3
4
5
6<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>工具包
1
2
3
4
5<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>Thymeleaf
1
2
3
4
5<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>插件
1
2
3
4
5
6
7
8
9
10
11
12
13<build>
<finalName>HelloSSM</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8088</port>
</configuration>
</plugin>
</plugins>
</build>
DAO层
实体类
1 | import lombok.Data; |
MyBatis相关
MyBatis配置文件 : mybatis-config.xml
1
2
3
4
5
6
7
<configuration>
<!--可以是空的,但必须要-->
</configuration>Spring配置文件 : Spring-dao.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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">
<!--..............jdbc.properties...................-->
<!--mysql.driver=com.mysql.jdbc.Driver-->
<!--mysql.url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8-->
<!--mysql.username=root-->
<!--mysql.password=123456-->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!--DataSource-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mysql.driver}"/>
<property name="jdbcUrl" value="${mysql.url}"/>
<property name="user" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<!--配置连接池属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="1000"/>
<!--重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 用spring来创建sqlsessionfactory-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--扫描,配置DAO接口包,生成相应的代理,然后注入到容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yuda.dao"/>
</bean>
</beans>UserMapper.java
1
2
3
4
5
6
7
8
9import com.yuda.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
int insert(; User user)
int insertSelective(; User user)
int insertList(; List<User> users)
int update(; User user)
}UserMapper.xml(与UserMapper.java同包下)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<mapper namespace="com.yuda.dao.UserMapper">
<!--auto generated Code-->
<resultMap id="BaseResultMap" type="com.yuda.entity.User">
<id column="t_id" property="u_id" jdbcType="INTEGER"/>
<result column="t_username" property="username" jdbcType="VARCHAR"/>
<result column="t_password" property="password" jdbcType="VARCHAR"/>
</resultMap>
<!--auto generated Code-->
<sql id="Base_Column_List">
`t_id`,
`t_username`,
`t_password`
</sql>
<!--auto generated Code-->
<insert id="insert" useGeneratedKeys="true" keyProperty="user.u_id">
INSERT INTO user2 (
t_id,
t_username,
t_password
) VALUES (
#{user.u_id,jdbcType=INTEGER},
#{user.username,jdbcType=VARCHAR},
#{user.password,jdbcType=VARCHAR}
)
</insert>
<!--auto generated Code-->
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="user.u_id">
INSERT INTO user2
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="user.u_id!=null"> t_id,</if>
<if test="user.username!=null"> t_username,</if>
<if test="user.password!=null"> t_password,</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="user.u_id!=null">#{user.u_id,jdbcType=INTEGER},
</if>
<if test="user.username!=null">#{user.username,jdbcType=VARCHAR},
</if>
<if test="user.password!=null">#{user.password,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!--auto generated Code-->
<insert id="insertList">
INSERT INTO user2 (
<include refid="Base_Column_List"/>
)VALUES
<foreach collection="users" item="user" index="index" separator=",">
(
#{user.u_id,jdbcType=INTEGER},
#{user.username,jdbcType=VARCHAR},
#{user.password,jdbcType=VARCHAR}
)
</foreach>
</insert>
<!--auto generated Code-->
<update id="update">
UPDATE user2
<set>
<if test="user.u_id != null"> t_id= #{user.u_id,jdbcType=INTEGER},</if>
<if test="user.username != null"> t_username= #{user.username,jdbcType=VARCHAR},</if>
<if test="user.password != null"> t_password= #{user.password,jdbcType=VARCHAR}</if>
</set>
WHERE t_id = #{user.u_id,jdbcType=INTEGER}
</update>
</mapper>对应的表
1
2
3
4
5
6
7
8-- auto Generated on 2017-11-12 22:27:16
-- DROP TABLE IF EXISTS user2;
CREATE TABLE user2(
t_id INT (11) NOT NULL AUTO_INCREMENT COMMENT 'u_id',
t_username VARCHAR (50) NOT NULL DEFAULT '' COMMENT 'username',
t_password VARCHAR (50) NOT NULL DEFAULT '' COMMENT 'password',
PRIMARY KEY (t_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'user2';
Service层
Spring配置文件
1
2
3
4
5
6
7
8
9
10
11
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--启动注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>UserServiec接口
1
2
3
4
5
6
7
8import java.util.List;
import com.yuda.entity.User;
public interface UserService{
int insert(User user);
int insertSelective(User user);
int insertList(List<User> users);
int update(User user);
}接口实现UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29import com.yuda.entity.User;
import com.yuda.dao.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.yuda.service.UserService;
import org.springframework.transaction.annotation.Transactional;
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
public int insert(User user){
return userMapper.insert(user);
}
public int insertSelective(User user){
return userMapper.insertSelective(user);
}
public int insertList(List<User> users){
return userMapper.insertList(users);
}
public int update(User user){
return userMapper.update(user);
}
}
Web层
站点配置文件
web.xml
1 |
|
Spring配置文件
spring-mvc.xml
1 |
|
- 注意Thyemleaf模板的相关配置:有三个bean需要配置;
- SpirngMVC仅仅扫描Controller注解;
- 两种静态资源配置
整合各层的Spring
spring.xml
1 |
|
扫描排除Controller
前端页面
- webapp/WEB-INF/templates/user.html
1 |
|
这里使用了thymeleaf模板的标签
外部的静态页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>静态页面</h1>
<form action="/helloSSM/hello/mvc" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
控制器
UserController.java
1 | import com.yuda.entity.User; |
其他配置
log4j.properties
1 | # Global logging configuration |
注意事项
- IDEA工具下,使用Maven Helper插件检查jar包依赖树,使用Lombok Plugin来配合Lombok来简化Entity类的编写;
- Thymeleaf作为Spring推荐的模板框架需要去学习;
- MyBatis注重SQL语句的编写;
- 本HelloSSM并不全,有许多地方都要优化或者缺失没写,例如异常的处理,自定义转换器的编写,DTO领域模型的创建等等;
- IDEA中的Resources目录下创建文件夹使用
com/yuda/dao
,而Java目录下创建文件夹用com.yuda.dao
; - 注意MyBatis的UserMapper.java和UserMapper.xml,保证它们经过编译后在同一个文件夹下;
- 配置文件与Java代码需要分开到Resources目录和Java目录下;