https://baomidou.com/guide/#%E7%89%B9%E6%80%A7
https://github.com/baomidou/mybatis-plus
https://gitee.com/baomidou/mybatis-plus
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.4.0</version> </dependency>
1、pom.xml:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency>
@EqualsAndHashCode(callSuper = true) @Data @TableName("user") public class User { private Long id; private String name; private Integer age; private String email; }
public interface UserMapper extends BaseMapper<User> { }
@RunWith(SpringRunner.class) @SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); //User user = userMapper.selectById(1);//通过id单个查询 //User user = userMapper.selectOne(null)//通过查询条件单个查询 //List<User> users = userMapper.selectPage(new Page<>(1,2),null);//分页查询 Assert.assertEquals(5, userList.size()); userList.forEach(System.out::println); } } 备注:单个条件查询,若是符合这个条件的记录不止一条,那么就会报错。 结合条件查询器EntityWrapper使
User(id=1, name=Jone, age=18, email=test1@baomidou.com) User(id=2, name=Jack, age=20, email=test2@baomidou.com) User(id=3, name=Tom, age=28, email=test3@baomidou.com) User(id=4, name=Sandy, age=21, email=test4@baomidou.com) User(id=5, name=Billie, age=24, email=test5@baomidou.com)
@Test public void testInsert(){ User user = new User(); user.setName("东方不败"); user.setEmail("dfbb@163.com"); user.setAge(20); userMapper.insert(user); System.out.println(user.getId()); } 备注: mybatisplus会自动把当前插入对象在数据库中的id写回到该实体中 执行添加操作,直接调用insert方法传入实体即可。
@Test public void testUpdate(){ User user = new User(); user.setId(1); user.setName("更新测试"); userMapper.updateById(user); //userMapper.update(user); System.out.println(user.getId()); } 备注: 根据id进行更新,没有传值的属性就不会更新 执行更新操作,直接调用update方法传入实体也可。
userMapper.deleteById(1);//id删除 userMapper.deleteBatchIds(idList);//批量id删除
需求:
使用MyBatis :
使用MP:
接下来就直接看几个案例体会EntityWrapper的使用。
1、分页查询年龄在18 - 50且姓名为tom的用户:
List<User> users = userMapper.selectPage(new Page<User>(1,3), new EntityWrapper<User>().between("age",18,50) .eq("name","tom") );
注:
2、查询名字中带有老师、或者邮箱中带有a的用户:
List<User> users = userMapper.selectList( new EntityWrapper<User>() .like("name","老师") //.or()//和or new 区别不大 .orNew().like("email","a") );
注:
3、查询根据age排序:
List<User> users = userMapper.selectList( new EntityWrapper<User>() .orderBy("age")//直接orderby 是升序,asc );
注:
4、分页查询年龄在18 - 50且姓名为tom的用户:
List<User> users = userMapper.selectList( Condition.create() .between("age",18,50).eq("name","tom") );
注:
5、根据条件更新:
@Test public void testEntityWrapperUpdate(){ User user = new User(); user.setName("苍老师"); user.setEmail("cjk@sina.com"); userMapper.update(user, new EntityWrapper<User>() .eq("name","tom") .eq("age",25) );}
注:
6、根据条件删除:
userMapper.delete( new EntityWrapper<User>().eq("name","tom") .eq("age",16) );
https://baomidou.com/guide/crud-interface.html#service-crud-%E6%8E%A5%E5%8F%A3
MyBatis-Plus