本章讲解SpringBoot整合MyBatis的相关知识
1.概念
之前我们通过整合SpringMVC已经发现了SpringBoot的强大之处。
那么,它和其他持久层框架的整合也是十分完美的,比如我们之前讲过的MyBatis。
本次将使用SpringBoot+SpringMVC+MyBatis+Thymeleaf完成登录验证
2.使用SpringBoot整合SpringMVC和MyBatis的操作步骤
1)数据库准备
我们需要一张users表来进行操作,本次使用的是MySQL 5.5
2)编写User实体类
package cn.edu.ccut.bo;
public class Users {
private String id;
private String username;
private String password;
public Users() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3)除了要引入SpringBoot的web启动器和Thymeleaf启动器以外,还需引入Mybatis启动器和JDBC相关驱动包
这里需要注意的是:mybatis的启动器版本目前最新支持到了2.1.0,所以我们规定了它的版本。
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
mysql
mysql-connector-java
5.1.47
4)配置Mapper
package cn.edu.ccut.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import cn.edu.ccut.bo.Users;
@Component
public interface UserMapper {
@Select({"select * from users where username=#{username} and password=#{password}"})
public Users doLogin(@Param("username") String username, @Param("password") String password);
}
5)编写service接口及实现类
package cn.edu.ccut.service;
import cn.edu.ccut.bo.Users;
public interface UserQueryService {
public Users login(String username, String password);
}
package cn.edu.ccut.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.edu.ccut.bo.Users;
import cn.edu.ccut.mapper.UserMapper;
import cn.edu.ccut.service.UserQueryService;
@Service("userQueryService")
@Transactional
public class UserQueryServiceImpl implements UserQueryService{
@Autowired
private UserMapper userMapper;
@Override
public Users login(String username, String password) {
return userMapper.doLogin(username,password);
}
}
5)编写controller
package cn.edu.ccut.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.edu.ccut.bo.Users;
import cn.edu.ccut.service.UserQueryService;
@Controller
public class UserController {
@Autowired
private UserQueryService userQueryService;
@RequestMapping("/login")
public String login(String username, String password){
Users user = userQueryService.login(username, password);
if (user != null) {
return "success";
}
return "login";
}
}
6)编写相关的页面login.html和success.html
login.html:
用户登录
success.html:
登录成功
Login Success!
7)配置数据库连接信息和MyBatis相关配置
修改SpringBoot的全局配置文件application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jwang
spring.datasource.username=root
spring.datasource.password=root
mybatis.type-aliases-package=cn.edu.ccut.bo
8)编写SpringBoot启动类
package cn.edu.ccut;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.edu.ccut.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.启动程序验证效果
1)访问http://localhost:8080/login,输入正确的用户名和密码
2) 观察跳转之后的效果如下