整合Spring-SpringMVC-MyBatis实现简单前后端交互
(1)环境要求
- IDEA
- MySQL
- Tomcat
- Maven
(2)数据库环境
id name pwd
1 Hunter 12
2 Nexa 34
3 Kennys 56
4 Niko 78
(3)SSM基本环境搭建
1.新建Maven项目,项目添加Web app支持
2.在pom.xml中导入相关依赖
<dependencies>
<!--Junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--数据库驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--数据库连接池-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet-JSP-->
<!-- 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/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--Spring-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
3.Maven资源过滤问题设置
<build>
<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>
4.搭建基本结构和框架配置
org.hac.controller
org.hac.pojo
org.hac.dao
org.hac.service
applicationContext.xml(Spring核心配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
(4)Mybatis层编写
1.数据库配置文件 database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
#jdbc.serverTimezone=UTC
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=hac0207
2.IDEA关联数据库
3.编写mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!--mybatis核心配置文件-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="org.hac.pojo"/>
</typeAliases>
<mappers>
<mapper class="org.hac.dao.UserMapper"/>
</mappers>
</configuration>
4.编写具体的接口、类、Mapper配置文件(分Dao层和Servcie层)
1)编写数据库对应的实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
2)定义dao层的Mapper接口
public interface UserMapper {
void addUser(User user);
void deleteUser(int id);
void updateUser(User user);
List<User> selectUser();
User selectUserById(int id);
}
3)编写接口对应的Mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?><!--Mapper配置文件-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hac.dao.UserMapper">
<insert id="addUser" parameterType="User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
<update id="updateUser" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<select id="selectUser" resultType="User">
select * from user
</select>
<select id="selectUserById" resultType="User" parameterType="int">
select * from user where id=#{id};
</select>
</mapper>
4)编写Service层的接口和实现类
public interface UserService {
void addUser(User user);
void deleteUser(int id);
void updateUser(User user);
List<User> selectUser();
User selectUserById(int id);
}
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public void addUser(User user) {
userMapper.addUser(user);
}
public void deleteUser(int id) {
userMapper.deleteUser(id);
}
public void updateUser(User user) {
userMapper.updateUser(user);
}
public List<User> selectUser() {
return userMapper.selectUser();
}
public User selectUserById(int id) {
return userMapper.selectUserById(id);
}
}
(5)Spring层编写
1.配置Spring整合Mybatis,数据源使用c3p0连接池;
<!--连接池-->
<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>
2.编写Spring整合Mybatis的相关的配置文件:Spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?><!--spring-dao层的配置文件-->
<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">
<!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2.连接池-->
<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>
<!--3.SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4.配置dao接口扫描包,动态实现dao接口可以注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.hac.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
3.Spring整合Service层
<?xml version="1.0" encoding="UTF-8"?><!--spring-service层的配置文件-->
<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:jdbc="http://www.springframework.org/schema/jdbc"
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 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!--扫描service相关的bean-->
<context:component-scan base-package="org.hac.service"/>
<!--将UserServiceImpl注入IOC容器-->
<bean id="userServiceImpl" class="org.hac.service.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
(6)SpringMVC层编写
Spring配置中<context:component-scan/>说明
在XML中配置了这个标签后,spring可以自动扫描base-package下面或者子包下面的java文件,如果扫描有@Component @Service @Controller等这些注解的类,则把这些类注册为bean。
1.web.xml配置文件
<?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_4_0.xsd"
version="4.0">
<!--DispatcherServlet-->
<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:applicationContext.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>
<!--endcodingFilter-->
<filter>
<filter-name>endcodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>endcoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>endcodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session过期问题-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
2.spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><!--配置SpringMVC-->
<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 https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.开启SpringMVC注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源默认Servlet配置-->
<mvc:default-servlet-handler/>
<!--3.配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描web相关的bean-->
<context:component-scan base-package="org.hac.controller"/>
</beans>
3.Spring配置整合文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><!--Spring核心配置文件-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-mvc.xml"/>
<import resource="spring-service.xml"/>
</beans>
(7)Controller编写
1.UserController编写
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
@Qualifier("userServiceImpl")
private UserServiceImpl userService;
@RequestMapping("/selectUser")
public String selectUser(Model model){
List<User> users=userService.selectUser();
model.addAttribute("msg",users);
return "user";
}
}
(8)起始页和跳转页编写
1.编写首页index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
这是首页
</body>
</html>
2.检索成员的跳转页user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User</title>
</head>
<body>
${msg}
</body>
</html>
(9)配置Tomcat测试
(10)问题排查
1.资源过滤问题
<build>
<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>
2.需要检查target目录中是否缺少配置文件,必要时手动添加
3.运行Tomcat前检查项目设置中的Artifacts中是否导入了相关的jar包
4.MySQL8.0以上版本中,配置文件database.properties中设置应多"jc"和注意时区设置
jdbc.driver=com.mysql.cj.jdbc.Driver
5.spring-dao配置文件中关联数据库配置文件中路径设置要加上classpath:
<!--关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
(11)SSM搭建思维导图
以上;
懂了吗,那就去用SpringBoot吧doge~~