mysqlplus实体类getset_MyBatis-Plus使⽤
MyBatis-Plus
MyBatis-Plus(简称 MP)是⼀个 MyBatis的增强⼯具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提⾼效率⽽⽣。环境介绍
环境要素
版本
maven
3.3.3
jdk
1.8.0_1.81
编译
idea
springboot
2.1.7.RELEASE
系统
win10
快速开始
添加依赖
com.baomidou
mybatis-plus-boot-starter
3.2.0
mysql
mysql-connector-java
8.0.17
runtime
org.projectlombok
lombok
1.18.8
org.springframework.boot
spring-boot-starter
2.1.7.RELEASE
org.springframework.boot
spring-boot-starter-test
2.1.7.RELEASE
test
配置
在application.properties中添加数据库配置
spring.datasource.driverClassName = sql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/db_demo?
useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username = root
spring.datasource.password = 123456
在数据库造点东西
CREATE TABLE `tb_sys_role` (
`id` bigint(1) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`name` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`description` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `id`(`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of tb_sys_role
-- ----------------------------
INSERT INTO `tb_sys_role` VALUES (1, '管理员', '⼀切皆可管');
在springboot项⽬启动类DemoApplication中,添加 @MapperScan 注解,扫描 Mapper ⽂件夹:
@SpringBootApplication(scanBasePackages = "com.smart.demo.*")
@MapperScan("com.smart.demo.dao.mapper")
public class SmartStreetApplication {
public static void main(String[] args) {
SpringApplication.run(SmartStreetApplication.class, args);
}
}
编码
编写⼀个实体类TbSysRole(使⽤lombok简化代码)
@Data
public class TbSysRole {
private Long id;
编写⼀个Mapper类,在上⾯的com.smart.demo.dao.mapper包下
public interface SysRoleMapper extends BaseMapper {
}
开始使⽤
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SmartDemoApplication.class)
public class SampleTest {
@Resource
private SysRoleMapper roleMapper;
@Test
public void testRole() {
System.out.println(("----- selectAll method test ------"));
List list = roleMapper.selectList(null);
Assert.assertEquals(1, list.size());
}
}
⼩结
jpa mybatis
通过以上⼏个简单的步骤,我们就实现了 User 表的 CRUD 功能。
从以上步骤中,我们可以看到集成 MyBatis-Plus ⾮常的简单,只需要引⼊ starter ⼯程,并配置 mapper 扫描路径即可。我那个擦,都不⽤xml 了,像JPA的调⽤⼀样丝滑。
下⾯介绍MP的具体使⽤⽅式
Mapper CRUD 接⼝
MP 封装通⽤的CRUD的接⼝BaseMapper;启动时⾃动解析实体表关系映射转换为 Mybatis 内部对象注⼊到容器
使⽤实例
定义Mapper接⼝继承BaseMapper
public interface SysRoleMapper extends BaseMapper {
}
定义实体类
@Data
public class TbSysRole {
private Long id;
测试⽤例
@Test
public void testMapper() {
TbSysRole r = new TbSysRole();
r.setName("aa");
roleMapper.insert(r);
QueryWrapper query = Wrappers.query();
query.eq("name","aa");
TbSysRole role= roleMapper.selectOne(query);
Map map=new HashMap<>();
map.put("name",Name());
roleMapper.deleteByMap(map);
roleMapper.insert(r);
roleMapper.delete(query);
List ids=new ArrayList<>();
ids.add(2L);
ids.add(3L);
roleMapper.deleteBatchIds(ids);
}
测试⽤例
@Resource
private SysRoleMapper roleMapper;
@Test
public void testRole() {
System.out.println(("----- selectAll method test ------"));
List list = roleMapper.selectList(null);
Assert.assertEquals(1, list.size());
}
庐⼭⾯⽬
实体类的Mapper 继承BaseMapper 。其中泛型 T 为实体类;Wrapper 条件构造器public interface BaseMapper extends Mapper {
//插⼊⼀条记录
int insert(T entity);
//删除
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map columnMap);
int delete(@Param("ew") Wrapper wrapper);
int deleteBatchIds(@Param("coll") Collection extends Serializable> idList);
//更新
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper updateWrapper);
//查询
T selectById(Serializable id);
List selectBatchIds(@Param("coll") Collection extends Serializable> idList);
List selectByMap(@Param("cm") Map columnMap);
T selectOne(@Param("ew") Wrapper queryWrapper);
Integer selectCount(@Param("ew") Wrapper queryWrapper);
List selectList(@Param("ew") Wrapper queryWrapper);
List> selectMaps(@Param("ew") Wrapper queryWrapper);
List selectObjs(@Param("ew") Wrapper queryWrapper);
//分页
IPage selectPage(IPage page, @Param("ew") Wrapper queryWrapper);
IPage> selectMapsPage(IPage page, @Param("ew") Wrapper queryWrapper);
}
Service CRUD接⼝
mybatis-plus 进⼀步封装了CRUD⽅法, 封装的接⼝为IService,实现类为ServiceImpl,当然为了避免和Mapper层的混淆,⽅法的命名有些改变。
命名区别
Mapper
Service
insert
save/saveBatch/saveOrUpdateBatch
delete
remove
update

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。