SpringBoot 快速整合 Mybatis&MybatisPlus
Date: 2021-07-23 10:19:25
*/
SET FOREIGN_KEY_CHECKS=0;
-- Table structure for user
DROP TABLE IF EXISTS user
;
CREATE TABLE user
(
id
int(11) NOT NULL AUTO_INCREMENT,
nickname
varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '用户名',
password
varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '密码',
age
int(11) DEFAULT NULL COMMENT '年龄',
male
int(11) DEFAULT NULL COMMENT '性别',
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
2.2.4 新建实体
package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data // getter/setter
@ToString // toString
@AllArgsConstructor // 有参构造函数
@NoArgsConstructor // 无参构造函数
@TableName("user")
@Accessors(chain = true)
@ApiModel(description = "用户实体")
public class User {
// 用户编号
@TableId(type = IdType.AUTO)
@ApiModelProperty(value = "用户编号", required = true)
private Integer id;
// 用户昵称
@ApiModelProperty(value = "用户昵称", required = true)
private String nickname;
// 用户密码
@ApiModelProperty(value = "用户密码", required = true)
private String password;
// 用户年龄
@ApiModelProperty(value = "用户年龄", required = true)
private Integer age;
// 用户性别 0 女 1 男 2 保密
@ApiModelProperty(value = "用户性别", required = true)
private Integer male;
}
2.2.5 新建 mapper
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:16
@Description:
*/
public interface UserMapper extends BaseMapper<User> {
}
2.2.6 mybatis 和 spring 融合
在启动类上增加@MapperScan("com.kuangstudy.mapper")
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mapper")
public class StudyBootsMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(StudyBootsMybatisApplication.class, args);
}
}
2.2.7 定义户 Service 接口和实现类
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.User;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:22
@Description:
*/
public interface UserService extends IService<User> {
}
package com.example.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:23
@Description:
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
2.2.8 测试用例
package com.example;
import com.example.entity.User;
import com.example.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StudyBootsMybatisApplicationTests {
@Autowired
private UserService userService;
@Test
public void saveUser(){
User user = new User();
user.setId(1);
user.setNickname("yykkxxxx");
user.setPassword("1121455");
user.setAge(34);
user.setMale(1);
userService.saveOrUpdate(user);
}
}
3、基于 xml 的
原生态的 mybatis 的支持
3.1 在 application.yml 增加支持 xml 的方式
##mybatis 的原生态支持
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
type-aliases-package: com.kuangstudy.entity
3.2 在 UserMapper 定义方法
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Param;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:16
@Description:
*/
public interface UserMapper extends BaseMapper<User> {
User getByUserId(@Param("id") Integer id);
}
3.3 新建 mapper.xml 文件
在 resources/mapper/UserMapper.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.kuangstudy.mapper.UserMapper">
<select id="getByUserId" resultType="com.kuangstudy.entity.User">
SELECT id,
nickname,
password,
age,
male
FROM kss_user
WHERE id = #{id}
</select>
</mapper>
3.4 UserService 接口新建自定义的方法
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.User;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:22
@Description:
*/
public interface UserService extends IService<User> {
/**
根据用户 id 查询用户信息
@param id
@return
*/
User getByUserId(Integer id);
}
package com.example.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;
/**
@Auther: 长颈鹿
@Date: 2021/07/23/10:23
@Description:
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public User getByUserId(Integer id) {
return this.baseMapper.getByUserId(id);
}
}
this.baseMapper:是 UserMapper,可以不用注入了
3.5 测试
@Test
评论