Spring cloud 引入MyBatis-Plus
MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。
一、引入MyBatis-Plus依赖
在build.gradle文件中的dependencies的对象中引入依赖
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.2'
引入MyBatis-Plus之后请不要再次引入MyBatis以及MyBatis-Spring,以避免因版本差异导致的问题。
二、application.xml配置mybatis-plus
mybatis-plus:
# mybatis的sql xml映射文件,配置在resources文件夹下
mapper-locations: classpath:mybatis/*Mapper.xml
# 数据库表字段实体
typeAliasesPackage: com.pd.shop.model.entity
三、配置Application的mapper扫描路径
@SpringBootApplication
@MapperScan(basePackages = "com.pd.shop.mapper")
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}
四、封装基类MyBatis-Plus
建议一下封装基类,后续进行扩展方便,当然也可以不封装
对于继承的BaseMapper接口、IService接口、继承ServiceImpl实现类
(1) 继承MyBatis-Plus的BaseMapper
package com.pd.shop.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface IShopMapper<T> extends BaseMapper<T> {
}
(2) 继承MyBatis-Plus的IService
package com.pd.shop.service;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IShopService<T> extends IService<T> {
}
(3) 继承MyBatis-Plus的ServiceImpl<M, T>
package com.pd.shop.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pd.shop.mapper.IShopMapper;
import com.pd.shop.service.IShopService;
public class ShopImpl<M extends IShopMapper<T>, T> extends ServiceImpl<M, T> implements IShopService<T> {
}
五、新建实体类
对应步骤二中, mybatis-plus.typeAliasesPackage配置
package com.pd.shop.model.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user_info")
public class UserInfo {
private static final long serialVersionUID = 1L;
private Long id;
/**
* 用户昵称
*/
private String nickname;
/**
* 1男 0女 -1未知
*/
private Integer sex;
}
六、新建mybatis的sql xml映射文件
新建UserInfoMapper.xml resources/mybatis文件夹下,对应步骤二中, mybatis-plus.mapper-locations 配置
<?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.pd.shop.mapper.UserInfoMapper">
<select id="queryList" resultType="com.pd.shop.model.entity.UserInfo">
SELECT
id,
nickname,
sex
FROM
user_info
</select>
</mapper>
推荐MybatisX快速开发插件。可以进行跳转,提示等辅助开发功能
七、继承基类
(1) Mapper继承新的IShopMapper,
package com.pd.shop.mapper;
import com.pd.shop.model.entity.UserInfo;
import java.util.List;
public interface UserInfoMapper extends IShopMapper<UserInfo> {
List<UserInfo> queryList();
}
(2) Service继承新的IShopService,
package com.pd.shop.service;
import com.pd.shop.model.entity.UserInfo;
import java.util.List;
public interface UserInfoService extends IShopService<UserInfo> {
List<UserInfo> queryList();
}
(3) Impl继承新的ShopImpl
package com.pd.shop.service.impl;
import com.pd.shop.mapper.UserInfoMapper;
import com.pd.shop.model.entity.UserInfo;
import com.pd.shop.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserInfoImpl extends ShopServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
@Override
public List<UserInfo> queryList(){
UserInfo userInfo= baseMapper.selectById("1336601236750163969");
//.... baseMapper.
//.... this.
//.... 可以调用MyBatis-Plus的方法
return baseMapper.queryList();
}
}