如果出现实体类和表名不一致在实体类中使用@TableName注解
如果出现实体类属性名和字段名不一致在实体类中使用@TableField注解
注:mybatisplus只能进行单表的CRUD操作,如需联表需要自己另写sql】
增删改查
@Autowired
private UserMapper userMapper;
//增加
@Test
void insert(){
//单个添加
usermapper.insert(new User(参数值));
//批量参加
List<User> list = new ArrayList<>();
list.add(new User(user参数));
list.add(new User(user参数));
list.add(new User(user参数));
list.add(new User(user参数));
userMapper.insert(list);
//根据id添加或修改(如果有id则进行修改操作,没有则添加新数据)
userMapper.insertOrUpdate(new User(参数));
}
//删除
@Test
void delete(){
//根据主键删除
userMapper.deleteById(1);
//根据主键批量删除
userMapper.deleteByIds(Arrays.saList(1,2,3));
//根据条件删除
UpdateWrapper<User> w = new UpdateWrapper<>();
w.like("uname","刘"); //删除uname为刘的值删除
w.le("age",19); //在uname的基础上再找到age小于19的值删除
w.in("uname","jone","Tom"); //删除uname有jone或Tom的值
w.ne("age",19); //
w.between("age",19,40); //删除age19到40之间的
w.like("uname","刘").or().gt("age",19); //名字姓刘或年龄大于19的
userMapper.delete(w);
}
//修改
void update(){
//根据主键修改
userMapper.updateById(new User(属性值));
//批量修改
List<User> users = new ArrayList<>();
Colections.addAll(users,
new User(数据),
new User(数据),
new User(数据)
)
userMapper.updateById(users);
//根据其他条件修改
User user = new User();
user.setAge(10);
userMapper.update(user,new UpdateWrapper<User>().eq("uname","哈哈哈"))''
}
//查询
@Test
void Select() {
//根据id查找
System.out.println(userMapper.selectById(1));
//根据条件查询
UserVo.userVo = new UserVo();
userVo.setName("刘");
userVo.setAge(9);
QueryWrapper<User> w = new QueryWrapper<>();
if(StringUtiles.isNotBlank(userVo.getName())){
w.like("uname",userVo.getname());
}
if(userVo.getAge()!=null){
wrapper.ge("age",userVo.getAge());
}
userMapper.selectList(w);
}
分页查询
添加分页插件
@Configuration
public class MybatisPlusConfig{
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
//分页查询
@Test
void SelectPage(){
//分页对象
Page<User> page = new Page<>(2,2); //页数和每页展示数量
//查询条件对象
Page<User> userPage = userMapper.selectPage(page, null);
sout("总页数"+userPage.getPages());
sout("当前页记录"+userPage/getRecords());
sout("总条数"+userPage.getTotal());
}
联表分页条件查询
dao(蓝鸟)
IPage<User> selectByPage(IPage<User> page, @Param("ew") Wrapper<User> queryWrapper);
mapper映射文件
<resultMap id="BaseMapping" type="com.example.qy174springbootmp.entity.User">
<id property="id" column="id" javaType="java.lang.Integer"/>
<result property="name" column="uname" javaType="java.lang.String"/>
<result property="age" column="age" javaType="java.lang.Integer"/>
<result property="email" column="email" javaType="java.lang.String"/>
<!--多对一的标签-->
<association property="clazz" javaType="com.example.qy174springbootmp.entity.Clazz">
<id property="cid" column="cid" javaType="java.lang.Integer"/>
<result property="cname" column="cname" javaType="java.lang.String"/>
</association>
</resultMap>
<select id="selectByPage" resultMap="BaseMapping">
select * from tbl_user u join tbl_clazz c on u.class_id=c.cid
${ew.customSqlSegment}
</select>
@Test
public void testSelectByPage(){
Page<User> page=new Page<>(1,2);
QueryWrapper<User> wrapper=new QueryWrapper<>();
IPage<User> userPage = userMapper.selectByPage(page, wrapper);
System.out.println("总页数:"+userPage.getPages());
System.out.println("当前页的记录:"+userPage.getRecords());
System.out.println("总条数:"+userPage.getTotal());
}