(三)mybatisPlus自定义Sql语句

mybatisPlus自定义Sql语句

? Java学习路线:搬砖工的Java学习路线
? 作者:程序员小王
? 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF
? 扫描主页左侧二维码,加我微信 一起学习、一起进步
? 欢迎点赞 ? 收藏 ⭐留言 ?
? 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕

前言:
能够使mybatis-plus像mybatis一样在xml中写SQL
前提是原本可以在项目中正常使用mybatis-plus

?看mybatisPlus自定义Sql语句操作之前,建议先看
1️⃣Mybatis-plus(MP)中CRUD操作保姆级笔记
2️⃣mybatisPlus实现ActiveRecord(AR)操作笔记

四、自定义sql语句

  • 建数据库表

  • 实体类
@TableName(value = "dept")
public class Dept extends Model<Dept> {
    @Override
    protected Serializable pkVal() {
        return id;
    }
    /**
     * 设置表的主键,分布式id,使用了雪花算法,字符串类型
     */
   @TableId(value = "id",type = IdType.AUTO)
    private String id;
    private  String deptName;
    private String deptMobile;
    private Integer deptManager;
  • 创建mapper
/**
 * DeptMapper是不需要使用的,MP需要使用DeptMapper获取到数据库的表的信息。
 * 如果不定义DeptMapper, MP会报错, 找不到表的定义信息
 * @author 王恒杰
 */
public interface DeptMapper extends BaseMapper<Dept> {
    /**
     * 添加
     */
    public void insertDept();

    /**
     * 通过id查询
     * @param id
     * @return
     */
   public Dept selectById(Integer id);

    /**
     * 通过姓名查询
     * @param name
     * @return
     */
   public List<Dept> selectByName(String name);
}


  • 新建sql映射xml文件:DeptMapper.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.tjcu.mapper.DeptMapper">
<!--添加-->
    <insert id="insertDept">
    insert into dept values(#{id},#{deptName},#{deptMobile},#{deptManager})
    </insert>
    <!--通过id查询-->
    <select id="selectById" resultType="com.tjcu.entity.Dept">
        select * from dept where id=#{id}
    </select>
    <!--通过姓名查询-->
    <select id="selectById" resultType="com.tjcu.entity.Dept">
        select * from dept where dept_name=#{name}
    </select>
</mapper>
  • 配置xml文件位置:配置application.yml (注意位置一定要对整齐)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/plus?useSSL=false&serverTimezone=UTC
    username: root
    password: root
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:com.tjcu/*Mapper.xml

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7EiQIxIf-1636353219615)(https://i.loli.net/2021/11/08/rgcYsnQV4i3IBpq.png)]

  • 添加测试
 /* 
 @SuppressWarnings("all"):就是为了不让deptMapper报错
 */
@SuppressWarnings("all")
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeptARTest {
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 添加操作
     */
    @Test
    public void  insertDeptTest(){
        Dept dept = new Dept();
        dept.setDeptName("销售表");
        dept.setDeptMobile("1235678");
        dept.setDeptManager(2);
       deptMapper.insertDept(dept);
    }

  • 添加日志

  • 通过id查询
  /**
     * 查询操作
     */
    @Test
    public void  selectDeptTest(){
        Dept dept = new Dept();

        Dept dept1 = deptMapper.selectById(1);
        System.out.println(dept1);
    }

  • ID查询日志

  • 通过姓名查询
    /**
     * 查询操作
     */
    @Test
    public void  selectDeptTest(){
        Dept dept = new Dept();

        List<Dept> depts = deptMapper.selectByName("销售表");
        System.out.println(depts);
    }
  • 通过姓名查询日志

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>