SpringBoot整合MyBatis
-
在pom文件中添加以下坐标,增加mybatis依赖,和数据库驱动依赖
<!--导入Mybatis依赖坐标--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!--导入mysql依赖,如果mysql版本是5.7,则应使用mysql:mysql-connector-java--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency>
-
删除resources目录下的application.properties,并新建application.yml对数据库连接进行配置
# 配置数据库连接 spring: datasource: url: jdbc:mysql:///springboot_demo username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
-
配置Mybatis
mybatis: # 配置实体类别名 type-aliases-package: com.starry.demo.entity # 配置mapper.xml位置 mapper-locations: classpath:mapper/*.xml # 下划线转驼峰 configuration: map-underscore-to-camel-case: true
-
application.yml
# 配置数据库连接 spring: datasource: url: jdbc:mysql:/// username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: # 配置实体类别名 type-aliases-package: com.starry.demo.entity # 配置mapper.xml位置 mapper-locations: classpath:mapper/*.xml
-
在启动类上加上@MapperScan注解
package com.starry.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 配置mybatis的扫描路径 @MapperScan("com.starry.demo.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
-
执行以下sql脚本
CREATE TABLE `sys_user`( user_id bigint not null primary key auto_increment, username VARCHAR(50) not null unique, `password` varchar(64) not null, `email` varchar(50) null, `phone` varchar(50) null, create_time datetime, update_time datetime, create_by varchar(50), update_by varchar(50) );
-
新建entity包并新增实体类
package com.starry.demo.entity; import lombok.Data; import java.util.Date; @Data public class SysUser { private Long userId; private String username; private String password; private String email; private String phone; private Date createTime; private Date updateTime; private String createBy; private String updateBy; }
-
新建dao包并编写SysUserDao接口
package com.starry.demo.dao; import com.starry.demo.entity.SysUser; import org.springframework.stereotype.Repository; @Repository // 这个注解表示它是一个数据访问层 public interface SysUserDao { SysUser selectByUsername(String username); int insert(SysUser sysUser); }
-
在resouces目录下新建mapper目录并在此编写SysUserMapper.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.starry.demo.dao.SysUserDao"> <select id="selectByUsername" resultType="sysUser" parameterType="string"> select * from sys_user where username = #{username} </select> <insert id="insert" parameterType="sysUser"> insert into sys_user(`username`, `password`, `email`, `phone`, `create_time`, `create_by`) values (#{username}, #{password}, #{email}, #{phone}, now(), #{createBy}) </insert> </mapper>
-
新建service和service.impl包并编写SysUserService和SysUserServiceImpl的代码
package com.starry.demo.service;
import com.starry.demo.entity.SysUser;
public interface SysUserService {
SysUser getByUsername(String username);
boolean saveOrUpdate(SysUser sysUser);
}
package com.starry.demo.service.impl;
import com.starry.demo.dao.SysUserDao;
import com.starry.demo.entity.SysUser;
import com.starry.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SysUserServiceImpl implements SysUserService {
private final SysUserDao sysUserDao;
public SysUserServiceImpl(SysUserDao sysUserDao) {
this.sysUserDao = sysUserDao;
}
@Override
public SysUser getByUsername(String username) {
return sysUserDao.selectByUsername(username);
}
@Override
public boolean saveOrUpdate(SysUser sysUser) {
// 判断sysUser的主键是否为null或为0
if (sysUser.getUserId() == null || sysUser.getUserId() == 0) {
// 执行新增操作
return sysUserDao.insert(sysUser) > 0;
}
// 执行修改操作
return false;
}
}
-
新建controller并创建SysUserController
package com.starry.demo.controller; import com.starry.demo.entity.SysUser; import com.starry.demo.service.SysUserService; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/sys/user") public class SysUserController { private final SysUserService sysUserService; public SysUserController(SysUserService sysUserService) { this.sysUserService = sysUserService; } @GetMapping public SysUser getByUsername(String username) { return sysUserService.getByUsername(username); } @PostMapping public boolean add(@RequestBody SysUser sysUser) { return sysUserService.saveOrUpdate(sysUser); } }