1.熟悉Spring Boot整合MyBatis
二. 实验内容1、 整合MyBatis,实现登录应用:
根据实验指导,完成程序。
三. 实验过程数据表结构图
实验项目框架图
UserController源代码
package com.example.liu.controller;
import com.example.liu.entity.User;
import com.example.liu.service.UserService;
import com.example.liu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//查询所以,方法名需要和UserMapper.xml中的id对应
@RequestMapping("/findAll")
public List findAll(){
return userService.findAll();
};
//根据姓名查询
@RequestMapping("/findByName")
public ListfindByName(String name){
return userService.findByName(name);
};
//根据用户名模糊查询
@RequestMapping("/findByUsernameLike")
public ListfindByUsernameLike(String username){
return userService.findByUsernameLike(username);
};
//根据id查询
@RequestMapping("/findById")
public User findById(Integer id){
return userService.findById(id);
};
//多条件动态查询
@RequestMapping("/find")
public Listfind(User user){
return userService.find(user);
};
//分页查询
@RequestMapping("/findByPageAll")
public ListfindByPageAll(User user){
return userService.findByPageAll(user);
};
//新增
@RequestMapping("/addSave")
public String addSave(){
User user=new User();
user.setName("lzy");
user.setUsername("lzy");
user.setPassword("lzy");
userService.addSave(user);
user.setName("qaz");
user.setUsername("qaz");
user.setPassword("qaz");
userService.addSave(user);
user.setName("qwe");
user.setUsername("qwe");
user.setPassword("qwe");
userService.addSave(user);
return "数据保存成功!";
};
//修改
@RequestMapping("/update")
public String update(){
User user=userService.findById(1);
user.setName("诸葛亮");
userService.update(user);
return "修改成功!";
};
}
MyEverpage源代码
package com.example.liu.entity;
public class MyEverpage {
private int limit;
private int offset;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
}
User源代码
package com.example.liu.entity;
public class User extends MyEverpage{
private Integer id;
private String name;
private String username;
private String password;
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;
}
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;
}
}
UserMapper源代码
package com.example.liu.mapper;
import com.example.liu.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
//查询所有,方法名需要和UserMapper.xml中的id对应
public List findAll();
//根据姓名查询
public ListfindByName(String name);
//根据用户名模糊查询
public ListfindByUsernameLike(String username);
//根据id查询
public User findById(Integer id);
//多条件动态查询
public Listfind(User user);
//分页查询
public ListfindByPageAll(User user);
//新增
public void addSave(User user);
//修改
public void update(User user);
}
UserService源代码
package com.example.liu.service;
import com.example.liu.entity.User;
import com.example.liu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
//查询所以,方法名需要和UserMapper.xml中的id对应
public List findAll(){
return userMapper.findAll();
};
//根据姓名查询
public ListfindByName(String name){
return userMapper.findByName(name);
};
//根据用户名模糊查询
public ListfindByUsernameLike(String username){
return userMapper.findByUsernameLike(username);
};
//根据id查询
public User findById(Integer id){
return userMapper.findById(id);
};
//多条件动态查询
public Listfind(User user){
return userMapper.find(user);
};
//分页查询
public ListfindByPageAll(User user){
return userMapper.findByPageAll(user);
};
//新增
public void addSave(User user){
userMapper.addSave(user);
};
//修改
public void update(User user){
userMapper.update(user);
};
}
UserMapper.xml文件源代码
porm.xml配置文件源代码
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.0 com.example liu 0.0.1-SNAPSHOT liu Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com.alibaba druid 1.0.9 org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin



