一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

一、注解

@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 {
}

 

posted on   一路繁花似锦绣前程  阅读(2407)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
 
点击右上角即可分享
微信分享提示