MybatisPlus +SpringBoot入门新手教程 IDEA版 (学习进度3)

4 篇文章 0 订阅
4 篇文章 0 订阅
本文是MybatisPlus与SpringBoot整合的入门教程,通过在pom.xml中引入Mybatis Plus的依赖,修改application.yml配置,开启ActiveRecord功能,并在实体类、Dao层、Service层进行相应配置,最后在Controller层使用,实现了数据查询操作。
摘要由CSDN通过智能技术生成

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1、pom.xml引入MyBatis Plus的jar,覆盖Spring Mybatis

        <!--Mybatis-->
        //<dependency>
        //    <groupId>org.mybatis.spring.boot</groupId>
        //    <artifactId>mybatis-spring-boot-starter</artifactId>
        //    <version>2.1.1</version>
        // </dependency>
		
        <!--这里是 Mybatis—plus jar ↓↓ ↓↓↓ ↓↓↓-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

2、修改application.yml文件

#mybatis:
#  mapper-locations: classpath:mapper/*.xml

#<这里是 mybatisplus 配置 ↓↓ ↓↓↓ ↓↓↓>
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.example.demo.entity

3、开启AR功能(ActiveRecord)领域模型

3.1、实体类开启AR (同时开启Dao层AR)
继承MP特殊类Model<> 泛型:当前实体类
//把普通pojo实例化到spring容器中
@Component
public class Student extends Model<Student> {//extends Model<Student>

    //MP中的大部分方法都是依赖主键来生成SQL的,所以需要指定标识实体类中的主键
    @TableId(value = "id", type = IdType.AUTO)
    private int id;
    @TableField("name")
    private String name;
    @TableField("sex")
    private String sex;
    @TableField("classname")
    private String classname;
    
    //序列化主键
    @Override
    protected Serializable pkVal() {
        return this.id;
    }
    //省略get/set 构造 toString
  }
3.2、Dao层开启AR
继承MP特殊类BaseMapper<> 泛型:当前实体类
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    public List<Student> getAll();
    public Student getStudentById();

}

4、最后就是Service 开启AR

4.1、Service层 接口 interface
继承MP特殊类IService<> 泛型:当前实体类
public interface IStudent extends IService<Student> {

    public List<Student> getAll();
    public Student getStudentById();
}

4.1、Service层 实现 implement
继承MP特殊类ServiceImpl<mapper,实体类>
@Service
public class StudentImpl extends ServiceImpl<StudentMapper,Student> implements IStudent  {//开启AR

    @Autowired
    StudentMapper studentMapper;

    public Student getStudentById() {
        Student student = studentMapper.selectById("1710272147");//通过AR 查询id是1710272147的帅小子
        return student;
    }

    @Override
    public List<Student> getAll() {
        List<Student> students = studentMapper.getAll();
        return students;
    }
}
4.2、这时候就可以调用许多AR 内置的方法了(附带一张效果图)

在这里插入图片描述

5、Controller层 不做修改,

@Controller
public class HelloController {

    @Autowired
    private IStudent iStudent;

    @RequestMapping("/selectStudentById")
	@ResponseBody
    public Student selectStudentById() {
        Student student = iStudent.getStudentById();
        return student;
    }
}

6、运行程序入口 访问地址

在这里插入图片描述

帅小伙的信息就查询出来了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值