SpringBoot是一个快速开发框架,shiBatis是一个常用的持久层框架,在现价段的开发中,这两个框架经常会搭配使用,本文就细说一下在SpringBoot中如何整合MyBatis。
开发使用工具:IDEA
开发使用框架:Springboot+mybatis
首先,创建一个Springboot项目,本文通过IDEA创建,本文详细过程见下图,当然,创建f方式也有其他的,比如通过maven项目构建或者通过https://start.spring.io/进行创建后下载再导入到IDEA中.
DevTools用于热部署,Lombok用于添加一些注解,省略代码量.
项目结构:
接下来,在pom.xml文件中添加mybatis和mysql的依赖,这个依赖我们可以去maven仓库中去搜索,
maven仓库地址:https://mvnrepository.com/.
pom.xml文件内容:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.3
com.example
mybatis
0.0.1-SNAPSHOT
mybatis
Demo project for Spring Boot
1.8
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
8.0.22
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
pom文件配置后,不要忘记更近maven引入依赖啊.
maven更新后准备数据,在mybatis中创建相关数据库 表和字段:
CREATE TABLE `sys_role` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`role_name` varchar(50) DEFAULT NULL COMMENT '角色名称',
`role_info` varchar(100) DEFAULT NULL COMMENT '角色相关信息',
`status` char(1) NOT NULL COMMENT '使用状态,0:正常使用,1:停止使用',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色信息表';
INSERT INTO `mybatis_test`.`sys_user`(`id`, `user_name`, `user_code`, `user_info`, `status`) VALUES (1, '刘备', 'liubei', NULL, '0');
INSERT INTO `mybatis_test`.`sys_user`(`id`, `user_name`, `user_code`, `user_info`, `status`) VALUES (2, '关羽', 'guanyu', NULL, '0');
INSERT INTO `mybatis_test`.`sys_user`(`id`, `user_name`, `user_code`, `user_info`, `status`) VALUES (3, '张飞', 'zhangfei', NULL, '0');
INSERT INTO `mybatis_test`.`sys_user`(`id`, `user_name`, `user_code`, `user_info`, `status`) VALUES (4, '诸葛亮', 'zhugeliang', NULL, '0');
数据准备完成,接下来进行项目的配置和代码编写,直接看代码:
application.yml:
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.2.12:3306/mybatis_test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true
username: root
# 填写自己数据库密码
password: xxx
mybatis:
type-aliases-package: com.example.mybatis.model
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
com.example.mybatis.mapper: debug
file:
name: log.txt
SysUserMapper.xml代码:
id, user_name, user_code, user_info, status
insert into sys_user( ) values (#{id}, #{userName}, #{userCode}, #{userInfo}, #{status})
update sys_user set user_name = #{userName}, user_code = #{userCode}, user_info = #{userInfo}, status = #{status} where id = #{id}
delete from sys_user where id = #{id}
SysUserMapper.java代码:
package com.example.mybatis.mapper;
import com.example.mybatis.model.SysUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @Classname SysUserMapper
* @Description 用户信息持久层
* @Author chengjunyu
* @Date 2022/1/24 11:01
*/
@Mapper
public interface SysUserMapper {
/**
* @Description 根据用户ID查询用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:06
* @Param id
* @Return com.example.mybatis.model.SysUser
* @Exception
*/
SysUser selectSysUserById(int id);
/**
* @Description 查询全部用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:07
* @Param * @param
* @Return java.util.List
* @Exception
*/
List selectSysUserList();
/**
* @Description 新增用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:07
* @Param sysUser
* @Return int
* @Exception
*/
int insertSysUser(SysUser sysUser);
/**
* @Description 修改用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:07
* @Param sysUser
* @Return int
* @Exception
*/
int updateSysUser(SysUser sysUser);
/**
* @Description 删除用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:07
* @Param
* @Return int
* @Exception
*/
int deleteSysUserById(int id);
}
SysUserService代码:
package com.example.mybatis.service;
import com.example.mybatis.model.SysUser;
import java.util.List;
/**
* @Classname SysUserService
* @Description 用户信息Service层接口
* @Author chengjunyu
* @Date 2022/1/24 11:06
*/
public interface SysUserService {
/**
* @Description 根据用户ID查询用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:11
* @Param id
* @Return com.example.mybatis.model.SysUser
* @Exception
*/
SysUser selectSysUserById(int id);
/**
* @Description 查询全部用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:11
* @Param * @param
* @Return java.util.List
* @Exception
*/
List selectSysUserList();
/**
* @Description 新增用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param sysUser
* @Return int
* @Exception
*/
int insertSysUser(SysUser sysUser);
/**
* @Description 修改用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param sysUser
* @Return int
* @Exception
*/
int updateSysUser(SysUser sysUser);
/**
* @Description 删除用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param id
* @Return int
* @Exception
*/
int deleteSysUserById(int id);
}
SysUserServiceImpl代码,本段代码省去了处理逻辑,实际工作中处理逻辑会比较多:
package com.example.mybatis.service.impl;
import com.example.mybatis.mapper.SysUserMapper;
import com.example.mybatis.model.SysUser;
import com.example.mybatis.service.SysUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName SysUserServiceImpl
* @Description 用户信息Service层接口实现类
* @Author chengjunyu
* @Date 2022/1/24 11:13
* @Version 1.0
*/
@Service
public class SysUserServiceImpl implements SysUserService {
@Resource
public SysUserMapper sysUserMapper;
/**
* @param id
* @Description 根据用户ID查询用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:11
* @Param id
* @Return com.example.mybatis.model.SysUser
* @Exception
*/
@Override
public SysUser selectSysUserById(int id) {
return sysUserMapper.selectSysUserById(id);
}
/**
* @Description 查询全部用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:11
* @Param * @param
* @Return java.util.List
* @Exception
*/
@Override
public List selectSysUserList() {
return sysUserMapper.selectSysUserList();
}
/**
* @param sysUser
* @Description 新增用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param sysUser
* @Return int
* @Exception
*/
@Override
public int insertSysUser(SysUser sysUser) {
return sysUserMapper.insertSysUser(sysUser);
}
/**
* @param sysUser
* @Description 修改用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param sysUser
* @Return int
* @Exception
*/
@Override
public int updateSysUser(SysUser sysUser) {
return sysUserMapper.updateSysUser(sysUser);
}
/**
* @param id
* @Description 删除用户信息
* @Author chengjunyu
* @Date 2022/1/24 11:12
* @Param id
* @Return int
* @Exception
*/
@Override
public int deleteSysUserById(int id) {
return sysUserMapper.deleteSysUserById(id);
}
}
SysUserController代码:
package com.example.mybatis.controller;
import com.example.mybatis.model.SysUser;
import com.example.mybatis.service.SysUserService;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName SysUserController
* @Description 用户信息Controller层
* @Author chengjunyu
* @Date 2022/1/24 11:18
* @Version 1.0
*/
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
@Resource
public SysUserService sysUserService;
@GetMapping("/selectSysUser")
public SysUser selectSysUser(int id) {
return sysUserService.selectSysUserById(id);
}
@GetMapping("/selectSysUserList")
public List selectSysList() {
return sysUserService.selectSysUserList();
}
@PostMapping("/insertSysUser")
public String insertSysUser(SysUser sysUser) {
int count = sysUserService.insertSysUser(sysUser);
if(count > 0) {
return "新增人员信息成功。。。";
}
return "新增人员信息失败。。。";
}
@PostMapping("/updateSysUser")
public String updateSysUser(SysUser sysUser) {
int count = sysUserService.updateSysUser(sysUser);
if(count > 0) {
return "修改人员信息成功。。。";
}
return "修改人员信息失败。。。";
}
@PostMapping("/deleteSysUser")
public String deleteSysUser(int id) {
int count = sysUserService.deleteSysUserById(id);
if(count > 0) {
return "删除人员信息成功。。。";
}
return "删除人员信息失败。。。";
}
}
PostMan执行结果显示:
根据用户ID查询用户信息:
查询全部用户信息:
新增用户信息:
修改刘备的人员信息为username=刘皇叔:
删除ID=2的用户:
再次查询全部用户信息:
至此,所有的mysql的增删改查操作全部成功,SpringBoot整合MyBatis操作完成.