赞
踩
目录
Mybatis-plus可以说是Mybatis的增强版,增强有以下几点:
1、相比较于Mybatis可以生成service,serviceImpl,controller层的代码,减少代码工作量。
2、service层实现了通用的CURD(增删改查功能),减少了用户人员的代码量,避免了大量的重复代码。
依赖环境集成
注意事项:
1.mybatis-plus和mybatis-plus-generator版本必须一致 。
2.mybatis-plus 和 springboot 的版本最好对应,可以上https://mvnrepository.com/搜索mybatis-plus-boot-starter,然后点击对应的版本,查看对应的springboot版本。
导入依赖:
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>3.2.0</version>
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.8</version>
- </dependency>
- mybatis-plus:
- mapper-locations:
- # xml文件的路径
- - classpath*:mapper/*.xml
- # 实体类的包路径
- type-aliases-package: com.cw.bean
@MapperScan({"mapper接口所在的包名"}),可以多个。
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class TestUserMapper {
-
- @Autowired
- private UserMapper userMapper;
-
- @Test
- public void testInsert(){
- User user=new User();
- user.setAge(12);
- user.setName("曹操");
- user.setPassword("123");
- user.setMail("caocao@qq.com");
- user.setUserName("曹操");
- user.setAddress("北京");
- //result数据库受影响的行数
- int result = userMapper.insert(user);
- System.out.println("result=>"+result);
- //获取自增长后的id值
- System.out.println(user.getId());//自增后的id会回填到对象中
- }
- }
- @Test
- public void testDeleteById() {
- //执行删除操作
- int result = this.userMapper.deleteById(6L);
- System.out.println("result = " + result);
- }
- @Test
- public void testDeleteByMap() {
- Map<String, Object> columnMap = new HashMap<>();
- columnMap.put("age",20); columnMap.put("name","张三");
-
- //将columnMap中的元素设置为删除的条件,多个之间为and关系
- int result = this.userMapper.deleteByMap(columnMap);
- System.out.println("result = " + result);
- }
- @Test public void testDeleteByMap() {
- User user = new User();
- user.setAge(20);
- user.setName("张三");
-
- //将实体对象进行包装,包装为操作条件
- QueryWrapper<User> wrapper = new QueryWrapper<>(user);
- int result = this.userMapper.delete(wrapper);
- System.out.println("result = " + result);
- }
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class UserMapperTest {
-
- @Autowired private UserMapper userMapper;
-
- @Test
- public void testUpdateById() {
- User user = new User(); user.setId(6L); //主键
- user.setAge(21); //更新的字段
- //根据id更新,更新不为null的字段
- this.userMapper.updateById(user);
- }
-
- }
- @Test public void testUpdate() {
- User user = new User(); user.setAge(22); //更新的字段
- //更新的条件
- QueryWrapper<User> wrapper = new QueryWrapper<>();
- wrapper.eq("id", 6);
- //执行更新操作
- int result = this.userMapper.update(user, wrapper);
- System.out.println("result = " + result);
- }
- @Test
- public void testSelectById() {
- //根据id查询数据
- User user = this.userService.getById(2L);
- System.out.println("result = " + user);
- }
- @Test
- public void testSelectBatchIds() {
- //根据id集合批量查询
- List<User> users = this.userService.listByIds(Arrays.asList(2L, 3L, 10L));
- for (User user : users) {
- System.out.println(user);
- }
- }
- @Test public void testList() {
- QueryWrapper<User> wrapper = new QueryWrapper<User>();
- wrapper.eq("name", "李四");
- List<User> user = this.userMapper.list(wrapper);
- }
- @Test public void testSelectOne() {
- QueryWrapper<User> wrapper = new QueryWrapper<User>();
- wrapper.eq("name", "李四");
- User user = this.userService.getOne(wrapper);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。