代码地址:https://gitee.com/jkangle/springboot-exercise.git
1.构建SpringBoot项目
- 注意版本不能太高
- 版本太高会出现问题
- 整体的项目结构如图
2.导入Mybatis依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
3.写domain层
这一层主要是建立和数据库的映射
package com.jkk.springboot_mybatis.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
/**
* @TableName users
* @author jkk
*/
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class Users implements Serializable {
private String username;
private String password;
private String idNumber;
private String phoneNumber;
private String gender;
private String dataStatus;
private static final long serialVersionUID = 1L;
}
4.写mapper层
mapper层主要是对数据库的操作接口
package com.jkk.springboot_mybatis.mapper;
import com.jkk.springboot_mybatis.domain.Users;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author jkk
*/
@Repository
public interface UserMapper {
/**
* 增加用户
* @param users
* @return
*/
int adduser(Users users);
/**
* 删除用户
* @param username
* @return
*/
int deleteUser(String username);
/**
* 更新用户
* @param users
* @return
*/
int updateUser(Users users);
/**
* 查询单个用户
* @param username
* @return
*/
Users getByUserName(String username);
/**
* 查询所有的用户
* @return
*/
List<Users> getAllUser();
}
mapper层的具体的实现有两种的方式来书写,通过 注解配置,或者通过xml文件配置
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jkk.springboot_mybatis.mapper.UserMapper">
<select id="getByUserName" resultType="Users">
select * from users where username = #{username}
</select>
<select id="getAllUser" resultType="Users">
select * from users
</select>
<insert id="adduser" parameterType="Users">
insert into users(username,password,id_number,phone_number,gender,data_status)values (#{username},#{password},#{idNumber},#{phoneNumber},#{gender},#{dataStatus})
</insert>
<update id="deleteUser" parameterType="String">
delete
from users
where username = #{username};
</update>
<update id="updateUser" parameterType="Users">
update users set password = #{password} where username=#{username}
</update>
</mapper>
5.写service层
具体的业务层
//接口
package com.jkk.springboot_mybatis.service;
import com.github.pagehelper.PageInfo;
import com.jkk.springboot_mybatis.domain.Users;
import java.util.List;
/**
* @author jkk
*/
public interface UserService {
/**
* 增加用户
* @param users
* @return
*/
int adduser(Users users);
/**
* 删除用户
* @param username
* @return
*/
int deleteUser(String username);
/**
* 更新用户
* @param users
* @return
*/
int updateUser(Users users);
/**
* 查询单个用户
* @param username
* @return
*/
Users getByUserName(String username);
/**
* 查询所有的用户
* @return
*/
List<Users> getAllUser();
/**
* 分页查询
* @param pageNum
* @param pageSize
* @return
*/
public PageInfo<Users> findAll(int pageNum,int pageSize);
}
//具体的实现类
package com.jkk.springboot_mybatis.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.jkk.springboot_mybatis.domain.Users;
import com.jkk.springboot_mybatis.mapper.UserMapper;
import com.jkk.springboot_mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author jkk
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int adduser(Users users) {
return userMapper.adduser(users);
}
@Override
public int deleteUser(String username) {
return userMapper.deleteUser(username);
}
@Override
public int updateUser(Users users) {
return userMapper.updateUser(users);
}
@Override
public Users getByUserName(String username) {
return userMapper.getByUserName(username);
}
@Override
public List<Users> getAllUser() {
return userMapper.getAllUser();
}
@Override
public PageInfo<Users> findAll(int pageNum, int pageSize) {
PageMethod.startPage(pageNum, pageSize);
List<Users> allUser = userMapper.getAllUser();
return new PageInfo<>(allUser);
}
}
6.写controller层
package com.jkk.springboot_mybatis.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkk.springboot_mybatis.domain.Users;
import com.jkk.springboot_mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author jkk
*/
@RestController
@RequestMapping("/user")
public class MybatisTestController{
@Autowired
private UserService userService;
/**
* 插入功能
* @param users
* @return
*/
@PostMapping("")
public int add(@RequestBody Users users){
return userService.adduser(users);
}
/**
* 查询功能
* @return
*/
@GetMapping("/list")
public List<Users> getAll(){
return userService.getAllUser();
}
@GetMapping("/list1/{pageNum}/{pageSize}")
public PageInfo getAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
return userService.findAll(pageNum, pageSize);
}
@DeleteMapping("/{username}")
public int deleteUser(@PathVariable("username") String username){
return userService.deleteUser(username);
}
}
当然还需要配置yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/worktest?characterEncoding=UTF-8
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.jkk.springboot_mybatis.domain
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
# 设置日志级别
logging:
level:
root: DEBUG
com.jkk: debug
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
以上就是Springboot整合mybatis的全部过程,可以通过postman或者浏览器测试(只对分页做测试)