zoukankan      html  css  js  c++  java
  • 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 {
    }
  • 相关阅读:
    java访问修饰符
    java中的String
    int与Integer的爱恨情仇
    vs中动态DLL与静态LIB工程中加入版本信息的方法
    从页面中取出email地址和相对链接及绝对链接的代码...
    Web文件的ContentType类型大全
    将web站点下的绝对路径转换为虚拟路径_asp.net技巧
    在ASP.NET中重写URL 方法三:在IIS7中使用HttpModule 实现无扩展名的URL重写
    CSS中常用的选择器及三大特性
    JS“轮播图”
  • 原文地址:https://www.cnblogs.com/linding/p/12608678.html
Copyright © 2011-2022 走看看