springbootmybatis plus增删改查CURD操作

一、注解

@TableName(value = "person")//指定表名

@TableId(value = "id",type = IdType.AUTO)//指定主键名及自增

@TableField(value = "name")//指定字段名

@TableField(exist = false)//表中不存在的字段

二、增

// modifyNum:返回数据库影响条数
Integer modifyNum = personMapper.insert(person);
// person.getId():插入完成后,获取插入的id
System.out.println(person.getId());

三、删

Integer modifyNum = personMapper.deleteById(id);

Integer modifyNum = personMapper.deleteBatchIds(idList);

Integer modifyNum = personMapper.deleteByMap(personMap);

四、改

Integer modifyNum = personMapper.updateById(person);

五、查

Integer id=1;
Person person = personMapper.selectById(id);

// where id in (?,?)
List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(2);
List<Person> personList1 = personMapper.selectBatchIds(ids);

// where name=? and age=?
Map<String, Object> map = new HashMap<>();
map.put("name","linlong");
map.put("age",27);
List<Person> personList2 = personMapper.selectByMap(map);

六、Wapper

Person person = new Person();
person.setName("linlong");

// UpdateWrapper<Person> updateWrapper = new UpdateWrapper<>();

QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(!StringUtils.isEmpty(person.getName()),"name",person.getName());

List<Person> list = personMapper.selectList(queryWrapper);

七、自定义sql

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
@Repository
public interface PersonMapper extends BaseMapper<Person> {

    @Select("select * from person")
    List<Person> findAllPerson();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wuxi.dao.PersonMapper">
    <select id="findAllPerson" resultType="com.wuxi.bean.Person">
        select * from person
    </select>
</mapper>

八、分页插件

@EnableTransactionManagement
@Configuration
@MapperScan("com.wuxi.dao")
public class MybatisPlusCinfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}
@Repository
public interface PersonMapper extends BaseMapper<Person> {
    @Select("select * from person")
    IPage<Person> findAllPerson(Page<Person> page);//分页对象必须在参数列表第一个位置,后面是查询条件参数
}
public void selectPersonPage(){
    Page<Person> page = new Page<>(1,10);//参数1:查询页码;参数2:每页显示数量
    IPage<Person> iPage = personMapper.findAllPerson(page);
    System.out.println("当前页码"+iPage.getCurrent());
    System.out.println("每页显示数量"+iPage.getSize());
    System.out.println("总记录数"+iPage.getTotal());
    System.out.println("总页数"+iPage.getPages());
    List<Person> list = iPage.getRecords(); // 数据集合
}

九、通用service

/**
 * Person:bean
 */
public interface PersonService extends IService<Person> {
}
/**
 * PersonMapper:dao
 * Person:bean
 */
@Service
@Transactional
public class PersonServiceImpl extends ServiceImpl<PersonMapper,Person> implements PersonService {
}
原文地址:https://www.cnblogs.com/linding/p/12608678.html