TIP

已经连续练了好几天这个,但是一些细节还是会忘,加深一下印象。

# 环境说明

种类 版本
电脑 win10
IDEA商业版 1.45.0
项目 maven

如果要是没有商业版,可以申请教育邮箱,或者用激活码激活。破解jar包下载,里面带有教程点我 (opens new window)

# 整合的思路

创建maven项目并配置相关环境
创建maven项目并配置相关环境
搭建spring
搭建spring
搭建springmvc
搭建springmvc
spring整合springmvc
spring整合springmvc
搭建mybatis
搭建mybatis
spring整合mybatis
spring整合mybatis
事务配置
事务配置
Viewer does not support full SVG 1.1

# 环境搭建

# 创建maven项目

# 在pom.xml中导入相关依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-version>5.2.5.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.4</version>
</dependency>
</dependencies>

# 创建好项目的整体结构

这里就是整个ssm框架基本的结构了。可以根据下面的思路走,看到哪个文件开始编写时,就可以在这里找到文件该到哪里创建。controller是表示层,dao是持久层,service是业务层,domain是实体类。

.
├── .idea
├── src
│   └── main
|       ├── java
|       |   └── org
|       |       └── example
|       |           ├── controller
|       |           |   └── UserController.java
|       |           ├── dao
|       |           |   └── UserMapper.java
|       |           ├── domain
|       |           |   └── User.java
|       |           ├── service
|       |           |   ├── UserService.java
|       |           |   └── Impl
|       |           |       └── UserServiceImpl.java
|       |           └── test
|       |               ├── SpringTest.java
|       |               └── MybatisTest.java
|       ├── resources
|       |   ├── org
|       |   |   └── example
|       |   |       └──dao
|       |   |          └── UserMapper.xml
|       |   ├── applicationContext.xml
|       |   ├── db.properties
|       |   ├── mybatis-config.xml
|       |   └── springmvc.xml
│       └── webapp
│           ├── WEB-INF
|           |   ├── Pages
|           |   |   └── success.jsp
│           │   └── web.xml
│           └── index.jsp 
├── target
├── pom.xml
└── ssm3.iml 

# 创建数据库

create database srcrs;
use srcrs;
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

# 编写javabean实体类

即编写User.java,这个是根据数据库的内容来编写的,数据库表的内容不同,javabean就要做出相应改变。

package org.example.domain;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

# 编写service接口和实现类。

即编写UserService.javaUserServiceImpl.java

package org.example.service;
import org.example.domain.User;
import java.util.List;
public interface UserService {
    List<User> findAll();
    void addUser(User user);
}
package org.example.service.Impl;
import org.example.domain.User;
import org.example.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Override
    public List<User> findAll() {
        System.out.println("Service业务层,查询所有用户");
        return null;
    }
    @Override
    public void addUser(User user) {
        System.out.println("Service业务层,保存用户");
    }
}

# spring框架代码编写

# 编写applicationContext.xml

配置spring扫描注解,将相应的类交给Spring管理。排除Controller注解。

<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       ">
    <!-- 注解扫描,除了Controller不扫描 -->
    <context:component-scan base-package="org.example">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

# 编写SpringTest.java进行测试

package org.example.test;
import org.example.service.UserService;
import org.example.service.Impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserService userServiceImpl = ac.getBean("userServiceImpl", UserServiceImpl.class);
        userServiceImpl.findAll();
    }
}

输出以下内容,则证明spring搭建好了。

Service业务层,查询所有用户

# springmvc代码代码编写

# 在web.xml中配置前端控制器

<!-- 配置前端控制器 -->
<servlet>
    <servlet-name>dispatcherServlet</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>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

# 在web.xml中配置中文乱码过滤器

如果加上这个有报错信息,则是说明位置加的不对,没啥影响,如果看不顺眼可以换个位置就好了。

<!-- 解决中文乱码问题 -->
<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>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

# 编写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: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
       ">
    <!-- 开启注解扫描,只扫描Controller注解 -->
    <context:component-scan base-package="org.example">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 配置视图解析器对象 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/Pages/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 过滤静态资源 -->
    <mvc:resources mapping="/css" location="/css/**"/>
    <mvc:resources mapping="/js" location="/js/**"/>
    <mvc:resources mapping="/images" location="/images/**"/>
    <!-- 开启SpringMVC注解的支持 -->
    <mvc:annotation-driven/>
</beans>

# 编写index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <a href="user/findAll">查询所有用户</a>
</body>
</html>

# 编写UserController.java

package org.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("Controller表现层,查询所有学生");
        return "success";
    }
}

# 编写success.jsp

这个页面没有做过多操作,如果能够成功跳转到这个页面则证明代码没有问题。反之如果在这个页面中有500等类似错误,就需要好好看看哪错了。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <h1>响应成功</h1>
</body>
</html>

# IDEA配置tomcat

这个就不用多说了。

# 测试运行

如果点击页面中的查询所有用户按钮,可以跳转到响应成功的页面,并且控制台输出

Controller表现层,查询所有学生

则说明springmvc搭建成功。

# spring整合springmvc

主要的一个问题就是当web项目启动时,spring.xml如何运行起来。所谓的整合也就是说,可以相互间调用其对象。

# 在web.xml中配置ContextLoaderListener监听器

<!-- 配置Sping的监听器,加载spring配置文件 -->
<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>

# controller中注入service对象

完善UserController.java

package org.example.controller;
import org.example.domain.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("Controller表现层,查询所有用户");
        //此时这里只能进行调用不能输出,因为List是null
        List<User> all = userService.findAll();
        return "success";
    }
}

# 测试整合的结果

运行web项目,点击查询所有用户,如果页面可以跳转到响应成功页面,并且在控制台输出以下内容则证明整合成功。

Controller表现层,查询所有用户
Service业务层,查询所有用户

# mabatis代码编写

有很多都是采用注解的方式,以后假如说遇到复杂的查询还是需要使用配置文件的,我想的就是长痛不如短痛,一步到位。

# 编写持久层接口UserMapper.java

即编写UserMapper.java。这个接口主要的作用制定操作数据库的规范,例如查询用户,增加用户,实现其方法就行,但是在mybatis中不需要自己实现,通过注解或者配置xml即可。

package org.example.dao;
import org.example.domain.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
    //查找所有的用户
    List<User> findAll();
    //增加一个用户
    void addUser(User user);
}

# 编写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>
    <!-- 加载外部配置文件 -->
    <properties resource="db.properties"/>
    <!-- 为实体类设置别名 -->
    <typeAliases>
        <package name="org.example.domain"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 注册*Mapper.xml文件 -->
    <mappers>
        <package name="org.example.dao"/>
    </mappers>
</configuration>

# 编写UserMapper.xml

如果要使用扫包的方式注册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="org.example.dao.UserMapper">
    <!-- 开启二级缓存 -->
    <cache/>
    <select id="findAll" resultType="user">
      select * from user
    </select>
</mapper>

# 编写db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/srcrs?useSSL=false&amp;serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

# 编写MybatisTest.java

package org.example.test;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.dao.UserMapper;
import org.example.domain.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //这里传入一个true开启自动提交事务
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }
}

运行,如果可以查询到数据库中的数据,则证明,可以mybatis搭建成功。增加用户就不用测了。

# spring整合mybatis

# 在applicationContext.xml中配置连接池

<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

# 在applicationContext.xml中加载外部文件db.properties

<!-- 添加数据库外部文件 -->
<context:property-placeholder location="classpath*:db.properties"/>

# 在applicationContext.xml中配置SqlSessionFactory工厂

<!-- 配置SqlSessionFactory工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 扫描domain包 使用别名 -->
    <property name="typeAliasesPackage" value="org.example.domain"/>
    <!-- 扫描dao包,注册*Mpper.xml文件 -->
    <property name="mapperLocations" value="classpath:org/example/dao/*.xml"/>
    <!-- 日志 -->
    <property name="configuration" ref="configuration"></property>
</bean>

# 在applicationContext.xml配置简单日志

需要和上面日志工厂配合使用。

<!-- 配置日志 -->
<bean id="configuration" class="org.apache.ibatis.session.Configuration">
    <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"></property>
</bean>

# 在applicationContext.xml中配置dao接口

<!-- 配置dao接口所在包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFacetory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <!-- 给出需要扫描dao接口包 -->
    <property name="basePackage" value="org.example.dao"/>
</bean>

# 完善UserServiceImpl.java

package org.example.service.Impl;
import org.example.dao.UserMapper;
import org.example.domain.User;
import org.example.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
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        System.out.println("Service业务层,查询所有用户");
        return userMapper.findAll();
    }
    @Override
    public void addUser(User user) {
        System.out.println("Service业务层,保存用户");
        userMapper.addUser(user);
    }
}

# 完善UserController.java

package org.example.controller;
import org.example.domain.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("Controller表现层,查询所有用户");
        List<User> all = userService.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        return "success";
    }
    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println("Controller表现层,增加用户");
        userService.addUser(user);
        return "success";
    }
}

# 完善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="org.example.dao.UserMapper">
    <!-- 开启二级缓存 -->
    <cache/>
    <select id="findAll" resultType="user">
      select * from user
    </select>
    <insert id="addUser" parameterType="user">
        insert into User (id,name) values (
        #{id},#{name}
        );
    </insert>
</mapper>

# 完善index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <a href="user/findAll">查询所有学生</a>
  <form action="user/addUser" method="post">
      <input type="text" name="id"><br/>
      <input type="text" name="name"><br/>
      <input type="submit" name="注册">
  </form>
</body>
</html>

# 测试运行

运行web项目,点击查询所有用户,如果在控制台中输出所有的学生,则证明整合成功。也可以输入idname,添加一个用户。可能添加用户会失败,因为增删改需要提交事务。接下来进行事务配置。

# 事务配置

# 在applicationContext中配置事务管理

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find*" read-only="true"/>
        <tx:method name="*" isolation="DEFAULT"/>
    </tx:attributes>
</tx:advice>
<!-- 配置AOP增强 -->
<aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* org.example.service.Impl.*ServiceImp.*(..))"/>
</aop:config>

至此,一个优雅的框架搭建好了,如果需要别的功能可以在添加。可以删除不必要的文件,org.example.test做测试用的,可以删除,mybatis-config.xml也可以删除了。

# 完整代码

下载地址:ssm框架源码 (opens new window)

# 注意

有一点需要注意,当你的web应用部署到服务器,关于连接数据库,或者执行SQL语言报错的时候,应该是你的数据库驱动和数据库版本不一致,假如你的数据库是5.7版本,就数据库驱动也要是5版本,数据库是8版本,驱动也要调成8版本。还有一种错误就是Linux默认对于数据库大小写敏感,需要自己改变一下。请参考我的web服务环境配置 (opens new window)

# 参考

史上最详细的IDEA优雅整合maven+ssm框架 (opens new window)

maven中央仓库 (opens new window)

mybatis官网 (opens new window)

Spring整合mybatis的xml文件配置 (opens new window)