HelloSSM

Spring MVC + Spring + MyBatis 整合

目录结构

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
HelloSSM.
│ .gitignore
│ pom.xml

├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─yuda
│ │ │ ├─dao
│ │ │ │ UserMapper.java
│ │ │ │
│ │ │ ├─dto
│ │ │ ├─entity
│ │ │ │ User.java
│ │ │ │
│ │ │ ├─service
│ │ │ │ │ UserService.java
│ │ │ │ │
│ │ │ │ └─impl
│ │ │ │ UserServiceImpl.java
│ │ │ │
│ │ │ ├─utils
│ │ │ └─web
│ │ │ UserController.java
│ │ │
│ │ ├─resources
│ │ │ │ jdbc.properties
│ │ │ │ log4j.properties
│ │ │ │ mybatis-config.xml
│ │ │ │ spring-dao.xml
│ │ │ │ spring-mvc.xml
│ │ │ │ spring-service.xml
│ │ │ │ spring.xml
│ │ │ │
│ │ │ └─com
│ │ │ └─yuda
│ │ │ └─dao
│ │ │ UserMapper.xml
│ │ │
│ │ └─webapp
│ │ │ hello.html
│ │ │ index.jsp
│ │ │
│ │ ├─resources
│ │ └─WEB-INF
│ │ │ web.xml
│ │ │
│ │ └─templates
│ │ user.html
│ │
│ ├─sql
│ │ User.sql
│ │
│ └─test
│ └─java
│ └─com
│ └─yuda
│ ├─service
│ │ └─impl
│ │ UserServiceImplTest.java
│ │
│ └─web
│ UserControllerTest.java
├────────────────────────────────────────────────────

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
2
3
4
5
6
7
import lombok.Data;
@Data
public class User {
private Integer u_id;
private String username;
private String password;
}

MyBatis相关

  1. MyBatis配置文件 : mybatis-config.xml

    1
    2
    3
    4
    5
    6
    7
    <?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>
  2. 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
    <?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">
    <!--..............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>
  3. UserMapper.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    import com.yuda.entity.User;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    public interface UserMapper {
    int insert(@Param("user") User user);
    int insertSelective(@Param("user") User user);
    int insertList(@Param("users") List<User> users);
    int update(@Param("user") User user);
    }
  4. 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
    <?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.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>
  5. 对应的表

    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层

  1. Spring配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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: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>
  2. UserServiec接口

    1
    2
    3
    4
    5
    6
    7
    8
    import 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);
    }
  3. 接口实现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
    29
    import 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;
    @Service
    @Transactional
    public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public int insert(User user){
    return userMapper.insert(user);
    }
    @Override
    public int insertSelective(User user){
    return userMapper.insertSelective(user);
    }
    @Override
    public int insertList(List<User> users){
    return userMapper.insertList(users);
    }
    @Override
    public int update(User user){
    return userMapper.update(user);
    }
    }

Web层

站点配置文件

web.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
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
<!--version = 3.1 -->
<welcome-file-list>
<welcome-file>hello.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Spring配置文件

spring-mvc.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
41
42
43
44
45
46
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解模式-->
<!--1.自动注册DefaultAnnotationHandlerMapping和AnnotationHandlerAdapter-->
<!--2.提供一系列:数据绑定,数字和日期类format,
@NumberFormat,@DataTimeFormat,xml,json,默认读写支持-->
<mvc:annotation-driven/>
<!--扫描Controller-->
<context:annotation-config/>
<context:component-scan base-package="com.yuda.web">
<!-- 仅仅扫描Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- servletMapping 路径为 "/" -->
<!-- 静态资源默认Servlet配置 -->
<!-- 1. 加入对静态资源的处理,js,png,css-->
<!-- 2. 运行使用'/'做映射 -->
<mvc:default-servlet-handler/>
<!--映射静态资源,和上面那个一样的作用,不过功能好像更加强大,可以映射classpath里面的静态资源-->
<mvc:resources mapping="/resources/**" location="/resources/"/>

<!--thymeleaf模板解析器-->
<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="templateResolver">
<property name="prefix" value="WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="cacheable" value="false"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<!--模板引擎-->
<bean class="org.thymeleaf.spring4.SpringTemplateEngine" id="templateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<!--Thymeleaf视图解析器-->
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver" id="viewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</beans>
  1. 注意Thyemleaf模板的相关配置:有三个bean需要配置;
  2. SpirngMVC仅仅扫描Controller注解;
  3. 两种静态资源配置

整合各层的Spring

spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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">
<context:component-scan base-package="com.yuda">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--导入其他-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
</beans>

扫描排除Controller

前端页面

  1. webapp/WEB-INF/templates/user.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1 th:text="${user.u_id}"></h1>
<h1 th:text="${user.username}"></h1>
<h1 th:text="${user.password}"></h1>
</body>
</html>

这里使用了thymeleaf模板的标签

  1. 外部的静态页面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!DOCTYPE html>
    <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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import com.yuda.entity.User;
import com.yuda.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/hello")
public class UserController {
private final Logger loggger = LoggerFactory.getLogger(this.getClass());
@Autowired
private UserService userService;
@RequestMapping(value = "/mvc",method = RequestMethod.POST)
public String test1(User user, Model model) {
loggger.debug("正在保存 : " + user);
userService.insertSelective(user);
model.addAttribute(user);
return "user";
}
}

其他配置

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.com.yuda=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

注意事项

  1. IDEA工具下,使用Maven Helper插件检查jar包依赖树,使用Lombok Plugin来配合Lombok来简化Entity类的编写;
  2. Thymeleaf作为Spring推荐的模板框架需要去学习;
  3. MyBatis注重SQL语句的编写;
  4. 本HelloSSM并不全,有许多地方都要优化或者缺失没写,例如异常的处理,自定义转换器的编写,DTO领域模型的创建等等;
  5. IDEA中的Resources目录下创建文件夹使用com/yuda/dao,而Java目录下创建文件夹用com.yuda.dao ;
  6. 注意MyBatis的UserMapper.java和UserMapper.xml,保证它们经过编译后在同一个文件夹下;
  7. 配置文件与Java代码需要分开到Resources目录和Java目录下;

项目已经上传到GitHub