MyBatis-Plus

1.简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生

官网:https://mybatis.plus/

2.搭建环境

2.1pom.xml配置


        
            com.baomidou
            mybatis-plus
            3.3.2
        
        
            org.springframework
            spring-context
            5.1.6.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.6.RELEASE
        
        
            org.springframework
            spring-orm
            5.1.6.RELEASE
        
        
            com.alibaba
            druid
            1.1.21
        
        
            mysql
            mysql-connector-java
            runtime
            5.1.47
        
        
            junit
            junit
            4.12
            test
        
        
            com.github.pagehelper
            pagehelper
            5.1.8
        


2.2spring配置




    
    
        
        
        
        
    
    
    
        
        

       
    
    
    
        
        
    



2.3mybatis配置




    
        
        
    
    
        
        
            
        

    



实体配置

//@TableName 注解,帮助mybatis与数据库表关联
@TableName("t_departments")
public class Department {
    @TableId(value = "id",type = IdType.AUTO)//指定自增策略
    private Integer id;
    private String name;
    private String location;
}


3.案例

3.1插入

    /**
     * 测试,mybatisplus的插入功能
     */
    @Test
    public void testInsert() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        //调用mybatisplus的插入方法
        departmentMapper.insert(new Department(null, "人事部", "杭州"));
    }


3.2修改

/**
     * 根据 ID 修改
     */
    @Test
    public void testupdateById() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department d = new Department();
        d.setId(15);
        d.setName("abc");
        d.setLocation("abc");
        departmentMapper.updateById(d);
    }

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * 实体对象 (set 条件值,可以为 null)
     * 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    @Test
    public void testupdate() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department d = new Department();
        d.setId(16);
        d.setName("123");
        d.setLocation("123");
        departmentMapper.update(d,new UpdateWrapper()
                .eq("name","abc")
                .eq("location","abc"));

    }


3.3删除

@Test
    public void testDeleteByMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Map map = new HashMap<>();
        map.put("id",11);
        map.put("name","人事部");
        departmentMapper.deleteByMap(map);
    }

    /**
     * 根据 entity 条件,删除记录
     *
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testDelete() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        departmentMapper.delete(new QueryWrapper().eq("id",12));
    }

    /**
     * 删除多条数据
     * 传入参数为collection
     */
    @Test
    public void testdeleteBatchIds() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        departmentMapper.deleteBatchIds(Arrays.asList(10,13,14));
    }


3.4查询

/**
     * 根据 ID 查询
     *
     * id 主键ID
     */
    @Test
    public void testselectById(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department department = departmentMapper.selectById(1);
        System.out.println(department);
    }

    /**
     * 查询(根据ID 批量查询)
     *
     *  主键ID列表(不能为 null 以及 empty)
     */
    @Test
    public void testselectBatchIds(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List list = departmentMapper.selectBatchIds(Arrays.asList(1,2,15));
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 查询(根据 columnMap 条件)
     *
     * 表字段 map 对象
     */
    @Test
    public void testselectByMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Map map = new HashMap();
        map.put("name","研发部");
        map.put("location","上海");
        List list = departmentMapper.selectByMap(map);
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 根据 entity 条件,查询一条记录
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectOne(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department department = departmentMapper.selectOne(new QueryWrapper().gt("id",10));
        System.out.println(department);
    }

    /**
     * 根据 Wrapper 条件,查询总记录数
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectCount(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        int count = departmentMapper.selectCount(null);
        System.out.println(count);
    }

    /**
     * 根据 entity 条件,查询全部记录
     *
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List list = departmentMapper.selectList(null);
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     *  实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectMaps(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List> list = departmentMapper.selectMaps(new QueryWrapper().ne("id",100));
        for (Map stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     * 

注意: 只返回第一个字段的值

* * 实体对象封装操作类(可以为 null) */ @Test public void testselectObjs(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class); List list = departmentMapper.selectObjs(null); System.out.println(list); } /** * 分页查询 */ @Test public void testselectpage(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class); PageHelper.startPage(1,2); List list = departmentMapper.selectList(null); System.out.println(list); }

你可能感兴趣的:(MyBatis-Plus)