MyBatis-Plus整合(二)

本文介绍了如何使用MyBatisPlus创建数据表、实体类、Mapper接口及XML,演示了如何调用DAO进行数据库操作,包括查询、插入和更新,并展示了在Spring Boot中集成和测试的步骤。MyBatisPlus简化了数据库操作,通过注解自动管理主键和映射关系。
摘要由CSDN通过智能技术生成

整合前的基本过程

创建数据表

创建实体类

Test实体类与test表对应,Test类的属性比照test表的字段名来写。

package com.imooc.reader.entity;
 
/**
 * 测试用的实体类,与test表对应;
 */
public class Test {
    private Integer id;
    private String content;
 
    public Test() {
    }
 
    public Test(Integer id, String content) {
        this.id = id;
        this.content = content;
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
}

创建mapper接口

/**
 * 演示Mybatis的,测试用的MapperDao,操作test表;
 */
public interface TestDao {
    public Test selectById();
    public void insert(Test test);
}

创建mapper.xml,编写SQL语句,实现接口定义的方法

<?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.imooc.reader.mapper.TestDao">
    <select id="selectById" parameterType="Integer" resultType="com.imooc.reader.entity.Test">
        select * from test where id = #{value}
    </select>
 
    <insert id="insert">
        insert into test(content) values (#{content})
    </insert>
</mapper>

调用Dao接口中的方法,操作数据库

@Service
public class TestService {
 
    @Resource
    private TestDao testDao;
    @Transactional
    public void testMybatis() {
        Test test = testDao.selectById(38);
        System.out.println(test.getContent());
        Test test1 = new Test();
        test1.setContent("hehehehe");
        testDao.insert(test1);
    }
 
}

编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestServiceTest {
 
    @Resource
    private TestService testService;
 
    @Test
    public void testMybatis() {
        testService.testMybatis();
    }
}

MyBatisPlus的使用

创建实体类

        @TableId注解当中type代表主键的生成方式, IdType.AUTO表示利用数据库底层的自增 主键来完成数据的插入工作。

        如果实体类的属性名数据表的字段名相同,或者符合其符合驼峰命名规则的话:实体类属性上的@TableField注解可以省略。

package com.imooc.reader.entity;
 
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
 
@TableName("test_mp")//说明这个实体类对应于哪张表;
public class TestMp {
    @TableId(type = IdType.AUTO) //说明这个属性对应了表的主键;
    @TableField("id")//说明属性对应于哪个字段;
    private Integer id;
    @TableField("content")
    private String content;
 
    public TestMp() {
    }
    public TestMp(Integer id, String content) {
        this.id = id;
        this.content = content;
    }
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
}

创建Mapper接口

        BaseMapper接口作为父接口,提供如 新增、修改、删除、查询 等方法的声明,在定义的时候需要传入一个泛型,说明对应的是哪个实体类;TestMpDao接口继承了BaseMapper接口,自然也继承了BaseMapper接口中的方法。

        BaseMapper接口中,定义了一系列select、insert、update、delete方法;这些方法需要慢慢熟悉和记忆;自己写的接口继承了BaseMapper接口,自然也继承了BaseMapper接口中的方法。

package com.imooc.reader.mapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.TestMp;
 
public interface TestMpDao extends BaseMapper<TestMp>{
 
}

创建mapper.xml,对应TestMpDao接口

        在这个xml文件当中并没有编写SQL语句实现TestMpDao接口中的方法,按照Mybatis的套路, 我们需要在xml文件中编写SQL语句,来实现DAO接口中的方法,但是因为现在使用MyBatisPlus,我们在xml中没有编写任何SQL语句去实现接口中的方法。

       虽然在xml文件中并没有编写SQL去实现insert()方法,但是随着loC容器初始化的过程中,MybatisPlus会根据我们在TestMp实体类中定义的映射关系,自动的去生成insert()方法的SQL语句。

        mybatisplus并没有修改mybatis的基础,所以原先mybatis的东西还可以继续使用,我们仍然可以在接口当中自己定义方法,然后在xml文件去编写sql语句实现方法,不过需要注意自己定义的方法,不要和BaseMapper接口中方法重名。

<?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.imooc.reader.mapper.TestMpDao">
 
</mapper>

测试用例:

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Test;
import com.imooc.reader.mapper.TestMapper;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyBatisPlusTest {
    @Resource
    private TestMapper testMapper;

    @org.junit.Test
    public void testInsert(){
        Test test = new Test();
        test.setContent("MyBatis Plus测试");
        testMapper.insert(test);
    }

    @org.junit.Test
    public void testUpdate(){
        Test test = testMapper.selectById(9);
        test.setContent("MyBatis Plus测试1");
        testMapper.updateById(test);
    }

    @org.junit.Test
    public void testDelete(){
        testMapper.deleteById(9);
    }
    @org.junit.Test
    public void testSelect(){
        //QueryWrapper是查询条件构造器,其作用就是组织查询所需的条件;
        //这儿需要传入实体类的泛型;
        QueryWrapper<Test> queryWrapper = new QueryWrapper<Test>();
        //然后通过QueryWrapper的一系列的方法,去组织SQL查询的条件;
        queryWrapper.eq("id", 7); //eq(方法就代表等值比较;比如这儿eq()的方法的意思是:查询id=7的记录;(字段名,字段值)
        queryWrapper.gt("id", 5);//gt是大于的意思,该语句的意思是查询所有id大于5的数据
        /*
            默认情况下,如果写了多条子句的话,他会用and符号进行连接
        */
        List<Test> list = testMapper.selectList(queryWrapper);
        System.out.println(list.get(0));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值