上文写过用springMVC实现简单的登陆注册功能,不过数据库的连接使用的是JDBC,我们都知道JDBC连接数据库存在很多弊端,最明显的就是硬编码不好维护问题,以及参数传递不方便,而Mybatis很好地解决了这一问题,接下来基于上述代码的基础上,修改项目为使用Mybatis进死数据库交互。
一.删除与数据库连接有关的代码
1.删除spring-mvc.xml中的连接数据库代码:
2.删除UserDao类,因为后面我们需要一个接口,所以该类用不到:
二.添加Mybatis依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMVCPratice2</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVCPratice2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.25</version> <!-- 可以根据需要调整版本 -->
</dependency>
<!-- Druid 依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version> <!-- 可以根据需要调整版本 -->
</dependency>
<!-- MySQL 连接器依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version> <!-- 可以根据需要调整版本 -->
</dependency>
<!-- Spring Context 依赖(通常与 Spring MVC 一起使用) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.25</version> <!-- 可以根据需要调整版本 -->
</dependency>
<!-- Spring Core 依赖(通常与 Spring MVC 一起使用) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.25</version> <!-- 可以根据需要调整版本 -->
</dependency>
<!-- Spring Transaction -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.23</version>
</dependency>
<!-- Jackson JSON Processor -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP (If using JSP for views) -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.25</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- MySQL 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCPratice2</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
三.添加相应配置文件:
1.创建db.properties用于保存数据库连接信息:
![](https://i-blog.csdnimg.cn/direct/41b2e67fbcdf4b7f953009b0e048f6bf.png)
username=root
password=123456
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/b409_db1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
2.创建MybatisConfig.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">
<!-- 定义MyBatis配置文件的开始 -->
<configuration>
<!-- 加载属性文件资源,其中定义了数据库的连接信息 -->
<properties resource="db.properties" />
<!-- 设置MyBatis运行时的参数,这里设置将下划线命名规则映射为驼峰命名规则 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- 定义类型别名,这里加载指定包下的类作为类型别名 -->
<typeAliases>
<package name="com.buka.pojo" />
</typeAliases>
<!-- 定义多个不同的环境,这里是设置开发用的环境 -->
<environments default="dev">
<environment id="dev">
<!-- 设置事务管理器类型 -->
<transactionManager type="JDBC" />
<!-- 设置数据源,这里采用了连接池的实现 -->
<dataSource type="POOLED">
<!-- 指定数据库连接的用户名 -->
<property name="username" value="${username}" />
<!-- 指定数据库连接的密码 -->
<property name="password" value="${password}" />
<!-- 指定数据库驱动 -->
<property name="driver" value="${driver}" />
<!-- 指定数据库的URL -->
<property name="url" value="${url}" />
</dataSource>
</environment>
</environments>
<!-- 加载映射文件,这里只是加载了一个UserMapper的映射文件 -->
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
</configuration>
3.创建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">
四.创建UserDao接口,编写UserMapper.xml中的sql语句:
1.创建UserDao接口:
2.编写sql语句
<?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.buka.dao.UserDao">
<select id="validateUser" resultType="boolean">
SELECT COUNT(*) FROM t_user WHERE userName = #{username} AND passWord = #{password}
</select>
<insert id="registerUser" parameterType="user">
insert into t_user values(#{id},#{userName},#{passWord})
</insert>
</mapper>
注意Mybatis中参数的填充方法,这里采用#{}的方式,防止使用${}可能会出现sql注入问题
3.修改UserService:
拿到映射对象,调用相应方法
package com.buka.service;
import com.buka.dao.UserDao;
import com.buka.pojo.User;
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.springframework.stereotype.Service;
import java.io.IOException;
import java.io.Reader;
@Service
public class UserService {
public String resource = "MybatisConfig.xml";
public Reader reader = Resources.getResourceAsReader(resource);
public SqlSessionFactory builder = new SqlSessionFactoryBuilder().build(reader);
public SqlSession session = builder.openSession(true);
private final UserDao userDao = session.getMapper(UserDao.class);
public UserService() throws IOException {
}
public boolean validateUser(String username, String password) {
return userDao.validateUser(username, password);
}
public boolean registerUser(User user) {
return userDao.registerUser(user);
}
}
五.测试:
这里我已经亲自测试过,效果同上一篇文章一样,只是更改了数据库连接方式