springboot-mybatis 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 {
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM