MyBatis-Plus使用ActiveRecord(AR)实现CRUD
1.什么是ActiveRecord(AR)?
ActiveRecord 是什么:
- 每一个数据库表应该对应创建一个实体类,类的每一个对象的实例对应于数据库中表的一行记录; 通常表的每个字段在类中都有相应的方法Field;
- ActiveRecord 负责把自己持久化. 在 ActiveRecord 中封装了对数据库的访问,通过对象自己实现 CRUD,实现优雅的数据库操作。
- ActiveRecord 也封装了部分业务逻辑。可以作为业务对象使用。
2.通过AR实现CRUD
首先创建一张表。
创建一个SpringBoot工程,在pom文件中添加依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.9</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
在核心配置文件中,配置数据库相关的连接信息。
#配置数据库的相关连接信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=12345678 #配置对应的日志信息 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
创建一个实体类,要使用AR,那么实体类就必须继承MP框架中的Model这个类。
package com.szh.mybatisplus.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.extension.activerecord.Model; /** * 使用AR,要求实体类必须继承MP框架中的Model类 * Model类中提供了数据库相关的CRUD操作 */ public class Dept extends Model<Dept> { @TableId(value = "id",type = IdType.AUTO) private Integer id; private String name; private String mobile; private Integer manager; //getter and setter //toString }
可以从Model类的源码中看到,这其中定义了大量关于CRUD操作的方法。
创建一个mapper接口。这里虽然不使用 mapper,但也需要定义这个它,MP 通过 mapper 获取到表的结构;不定义时,MP 报错无法获取表的结构信息。
package com.szh.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.szh.mybatisplus.entity.Dept; /** * */ public interface DeptMapper extends BaseMapper<Dept> { }
在SpringBoot项目的启动入口类上方,添加@MapperScan注解,确保可以扫描到MyBatis、MP下的相关注解。
package com.szh.mybatisplus; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan(value = "com.szh.mybatisplus.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
1.1 insert
@Test void testDeptInsert() { Dept dept=new Dept(); dept.setName("销售部"); dept.setMobile("12345678900"); dept.setManager(1); //调用实体类对象自己的方法,完成对象自身到数据库的添加操作 boolean flag=dept.insert(); System.out.println("insert的结果:" + flag); }
1.2 update
@Test void testDeptUpdate() { Dept dept=new Dept(); dept.setId(1); dept.setName("研发部"); dept.setMobile("99999999999"); dept.setManager(2); //调用实体类对象自己的方法,完成对象自身到数据库的更新操作 boolean flag=dept.updateById(); System.out.println("update的结果:" + flag); }
1.3 delete
@Test void testDeptDelete() { Dept dept=new Dept(); boolean result = dept.deleteById(2); System.out.println("delete的结果:" + result); }
@Test void testDeptDelete2() { Dept dept=new Dept(); dept.setId(2); boolean result = dept.deleteById(); System.out.println("delete的结果:" + result); }
1.4 select
@Test void testSelect() { Dept dept=new Dept(); dept.setId(3); Dept dept1 = dept.selectById(); System.out.println("select的结果:" + dept1); }
@Test void testSelect2() { Dept dept=new Dept(); Dept dept1 = dept.selectById(3); System.out.println("select的结果:" + dept1); }
@Test void testSelect3() { Dept dept=new Dept(); List<Dept> deptList=dept.selectAll(); deptList.forEach( dept1 -> { System.out.println(dept1); }); }
到此这篇关于MyBatis-Plus使用ActiveRecord(AR)实现CRUD的文章就介绍到这了,更多相关MyBatis-Plus实现CRUD内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
相关文章
- 这篇文章主要介绍了mybatis-plus 表名添加前缀的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-26
- 这篇文章主要介绍了mybatis-plus 返回部分字段的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-02
- 这篇文章主要介绍了MyBatis-Plus自动填充功能失效导致的原因及解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
- 这篇文章主要介绍了解决mybatis-plus 查询耗时慢的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-04
使用mybatis-plus报错Invalid bound statement (not found)错误
这篇文章主要介绍了使用mybatis-plus报错Invalid bound statement (not found)错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-02- 这篇文章主要介绍了mybatis-plus实体类主键策略有3种(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-27
- 这篇文章主要介绍了mybatis-plus getOne和逻辑删除,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-26
mybatis-plus mapper中foreach循环操作代码详解(新增或修改)
这篇文章主要介绍了mybatis-plus mapper中foreach循环操作代码详解(新增或修改),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-17- 这篇文章主要介绍了Mybatis和Mybatis-Plus时间范围查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-06
- 这篇文章主要介绍了Mybatis-Plus根据时间段去查询数据的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-18
Mybatis-plus使用TableNameHandler分表详解(附完整示例源码)
这篇文章主要介绍了Mybatis-plus使用TableNameHandler分表详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26- 这篇文章主要给大家介绍了关于MongoDB CRUD操作中的插入的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用MongoDB具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-12-08
Mybatis-plus实现主键自增和自动注入时间的示例代码
这篇文章主要介绍了Mybatis-plus实现主键自增和自动注入时间的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-20- 这篇文章主要介绍了mybatis-plus读取JSON类型的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-25
解决Mybatis-plus找不到对应表及默认表名命名规则的问题
这篇文章主要介绍了解决Mybatis-plus找不到对应表及默认表名命名规则的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18- 这篇文章主要介绍了搭建MyBatis-Plus框架并进行数据库增删改查,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-12
Mybatis或Mybatis-Plus框架的xml文件中特殊符号的使用详解
这篇文章主要介绍了Mybatis或Mybatis-Plus框架的xml文件中特殊符号的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19SpringBoot+Mybatis-Plus实现mysql读写分离方案的示例代码
这篇文章主要介绍了SpringBoot+Mybatis-Plus实现mysql读写分离方案的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09- 这篇文章主要介绍了mybatis-plus主键生成策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-27