SpringBoot项目Mybatis的XML文件建议放在resource目录下,新建一个mapper文件夹,里面放你要写的SQL.xml文件。html页面放在templates目录下面:
如图目录结构:
1.项目需要加的依赖包:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--mySql链接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--访问template下面html页面,也是加载thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.项目application.yml配置:
每台电脑自的配置不一样,不放代码复制了。这里需要注意:
a.mysql8.0的驱动是:com.mysql.cj.jdbc.Driver,不是com.mysql.jdbc.Driver;
b.mysql5.7以上,数据源地址配置需要拼上参数:serverTimezone=GMT,有时候还得加上useSSL=true
c.扫面XML里classpath“:”后面不要有空格哦。
3.启动类上加上注解:MapperScan("")
否则启动就会报错,找不到*Mapper文件。
4.写一个实体类User
package com.example.demo.registry.entity;
public class User {
private String username;
private String password;
private String idcard;
private int sex;
private String realname;
private Long mobile;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public Long getMobile() {
return mobile;
}
public void setMobile(Long mobile) {
this.mobile = mobile;
}
@Override
public String toString() {
return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", idcard='" + idcard + '\'' + ", sex=" + sex + ", realname='" + realname + '\'' + ", mobile=" + mobile + '}';
}
}
5.写UserMapper接口
package com.example.demo.registry.mapper;
import com.example.demo.registry.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
void insertUser(User user);
User findUserByUser(User user);
}
6.写UserMapper的mybatis.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="com.example.demo.registry.mapper.UserMapper">
<select id="findUserByUser" parameterType ="com.example.demo.registry.entity.User" resultType="com.example.demo.registry.entity.User">
SELECT U.USERNAME,U.IDCARD,U.MOBILE,U.REALNAME,U.SEX
FROM USER U
WHERE 1=1
<if test="username !=null and username != ''">
AND U.USERNAME =#{username}
</if>
<if test="password !=null and password != ''">
AND U.password =#{password}
</if>
<if test="idcard !=null and idcard != ''">
AND U.idcard =#{idcard}
</if>
<if test="mobile !=null and mobile != ''">
AND U.mobile =#{mobile}
</if>
<if test="realname !=null and realname != ''">
AND U.realname =#{realname}
</if>
</select>
</mapper>
这里要注意:namesapce是对应mapper的接口地址。不能写错,而且文件名必须和接口相同。
7.写Service和ServiceImp类:
package com.example.demo.registry.service;
import com.example.demo.registry.entity.User;
public interface UserService {
User findUser(User user);
}
package com.example.demo.registry.service;
import com.example.demo.registry.entity.User;
import com.example.demo.registry.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findUser(User user) {
return userMapper.findUserByUser(user);
}
}
8.写Controller
package com.example.demo.registry.controller;
import com.example.demo.registry.entity.User;
import com.example.demo.registry.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RegistryUser {
@Autowired
private UserService userService;
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public String findUser(User user) {
try {
user= userService.findUser(user);
return user.toString();
} catch(Exception e) {
return user.toString();
}
}
}
9.index.hrml页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎登录</title>
</head>
<body>
<div id="app">
<form action="/login" method="post" id="user">
<table>
<tr>
<td class="text_right">用户名:</td>
<td class="text_left"><input placeholder="输入用户名" name="username"
autocomplete="off"/></td>
</tr>
<tr>
<td class="text_right">密码:</td>
<td class="text_left"><input placeholder="请输入密码密码" type="password"
name="password" autocomplete="off" maxlength="10"/></td>
</tr>
<tr>
<td class="text_right"> 身份证:</td>
<td class="text_left"><input maxlength="18" placeholder="请输入身份证号码" type="text"
name="idcard" autocomplete="off"/></td>
</tr>
<tr>
<td class="text_right">真实姓名:</td>
<td class="text_left"><input placeholder="请输入真实姓名" type="text"
autocomplete="off" name="realname"/></td>
</tr>
<tr>
<td class="text_right">手机号码:</td>
<td class="text_left"><input maxlength="11" placeholder="请输入手机号码" type="number"
autocomplete="off" name="mobile"/></td>
</tr>
<tr>
<td class="text_right"><input type="submit" value="登录"/></td>
<td class="text_left"><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</div>
</body>
<script>
</script>
<style>
#app {
text-align: center;
margin: 100px 500px;
}
.text_right {
text-align: right;
}
.text_left {
text-align: left;
}
</style>
</html>
10.点击登录:
查到数据信息了。表示我们成功了。