springboot mybatis-plus 使用

版权声明:转载请注明出处 https://blog.csdn.net/Lee_Suoer/article/details/82711869

创建springboot环境,略过

pom文件导入依赖

application.properties文件进行配置

#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mp?characterEncoding=UTF-8&useUnicode=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#mybatis-plus
mybatis-plus.config-location=classpath:mybatis/mybatis-config.xml
mybatis-plus.mapper-locations=classpath:mybatis/mappers/*.xml
#ExecutorType.SIMPLE:该执行器类型不做特殊的事情,
#   为每个语句的执行创建一个新的预处理语句(PreparedStatement)
#ExecutorType.REUSE:该执行器类型会复用预处理语句(PreparedStatement)
#ExecutorType.BATCH:该执行器类型会批量执行所有的更新语句
mybatis-plus.executor-type=simple

其他配置信息看官网         http://mp.baomidou.com/guide/

代码生成器

 @Test
    public void contextLoads() {

        //1. 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true) // 是否支持AR模式
                .setAuthor("lee-suoer") // 作者
                .setOutputDir("D:\\Users\\Administrator\\IdeaProjects\\mybatisplus\\src\\main\\java") // 生成路径
                .setFileOverride(true)  // 文件覆盖
                .setIdType(IdType.AUTO) // 主键策略
                //不已I开头
                .setServiceName("%sService")  // 设置生成的service接口的名字的首字母是否为I
                // IEmployeeService
                .setBaseResultMap(true)
                .setBaseColumnList(true);

        //2. 数据源配置
        DataSourceConfig dsConfig  = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // 设置数据库类型
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/mp")
                .setUsername("root")
                .setPassword("root");

        //3. 策略配置
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true) //全局大写命名
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
                .setTablePrefix("tbl_")
                .setInclude("tbl_employee");  // 生成的表

        //4. 包名策略配置
        PackageConfig pkConfig = new PackageConfig();
//        各种文件生成的位置
        pkConfig.setParent("com.example.mybatisplus")
                .setMapper("mapper")
                .setService("service")
                .setController("controller")
                .setEntity("entity")
                .setXml("mapper");

        //5. 整合配置
        AutoGenerator ag = new AutoGenerator();

        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig);

        //6. 执行
        ag.execute();

    }

运行之后生成文件

可以将mapper.xml放入准备好的文件夹,便于管理

实体类

dao接口

扫描二维码关注公众号,回复: 4502581 查看本文章

service

自动注入了mapper

通用crud

自带的serviceImpl已经为我们注入了basemapper,我们不需要手动注入mapper了,直接使用。

public void insert() {
    //初始化Employee对象
    Employee employee  = new Employee();
    employee.setLastName("MP");
    employee.setEmail("[email protected]");
    employee.setGender("1");
    employee.setAge(22);
    Integer result = baseMapper.insert(employee);
    System.out.println("result: " + result );
    //获取当前数据在数据库中的主键值
    Integer key = employee.getId();
    System.out.println("key:" + key );

}
public void delete() {
//      1 .根据id进行删除
        Integer result = baseMapper.deleteById(13);
        System.out.println("result: " + result );
        //2. 根据 条件进行删除
//    Map<String,Object> columnMap = new HashMap<>();
//    columnMap.put("last_name", "MP");
//    columnMap.put("email", "[email protected]");
//    Integer result = baseMapper.deleteByMap(columnMap);
//    System.out.println("result: " + result );

        //3. 批量删除
//    List<Integer> idList = new ArrayList<>();
//    idList.add(3);
//    idList.add(4);
//    idList.add(5);
//    Integer result = baseMapper.deleteBatchIds(idList);
//    System.out.println("result: " + result );

      4.包装条件
      baseMapper.delete(new QueryWrapper<Employee>().eq("last_name", "MP"));
    }
public void update() {
    //初始化修改对象
    Employee employee = new Employee();
    employee.setId(7);
    employee.setLastName("aa");
    employee.setEmail("[email protected]");
    employee.setGender("0");
    //employee.setAge(33);

    Integer result = baseMapper.updateById(employee);

    baseMapper.update(employee,new UpdateWrapper<Employee>()
            .eq("last_name", "Tom")
            .eq("age", 44));

    System.out.println("result: " + result );
}
public void select() {

        //1. 通过id查询
      Employee employee = baseMapper.selectById(7);
      System.out.println(employee);

        //2. 通过多个列进行查询    id  +  lastName
//    Employee  employee = new Employee();
//    employee.setId(7);
//    employee.setLastName("ssss");
//    employee.setGender("0");

//    Employee result = baseMapper.selectOne(employee);
//    System.out.println("result: " +result );


        //3. 通过多个id进行查询    <foreach>
//    List<Integer> idList = new ArrayList<>();
//    idList.add(4);
//    idList.add(5);
//    idList.add(6);
//    idList.add(7);
//    List<Employee> emps = baseMapper.selectBatchIds(idList);
//    System.out.println(emps);

        //4. 通过Map封装条件查询
//    Map<String,Object> columnMap = new HashMap();
//    columnMap.put("last_name", "Tom");
//    columnMap.put("gender", 1);
//    List<Employee> emps = baseMapper.selectByMap(columnMap);
//    System.out.println(emps);

        //5. 分页查询
        IPage<Employee> page = baseMapper.selectPage(new Page<Employee>(3, 2), null);
        System.out.println(page.getRecords());

    }

AR操作

public void ARdelete() {
        Employee employee = new Employee();
        boolean result = employee.deleteById(2);
//    employee.setId(2);
//    boolean result = employee.deleteById();
//    System.out.println("result:" +result );


        boolean result = employee.delete(new QueryWrapper<Employee>().like("last_name", "小"));
        System.out.println(result );

    }
public void ARselect() {
            Employee employee = new Employee();

            //Employee result = employee.selectById(14);
//    employee.setId(14);
//    Employee result = employee.selectById();
//    System.out.println(result );


//    List<Employee> emps = employee.selectAll();
//    System.out.println(emps);

//    List<Employee > emps= 
//          employee.selectList(new QueryWrapper<Employee>().like("last_name", "老师"));
//    System.out.println(emps);

        Integer result = employee.selectCount(new QueryWrapper<Employee>().eq("gender", 0));
        System.out.println("result: " +result );
    }
public void ARinsert() {
        Employee employee = new Employee();
        employee.setLastName("sdsds");
        employee.setEmail("[email protected]");
        employee.setGender("1");
        employee.setAge(35);

        boolean result = employee.insert();
        System.out.println("result:" +result );
}
public void ARupdate() {
    Employee employee = new Employee();
    employee.setId(20);
    employee.setLastName("as");
    employee.setEmail("[email protected]");
    employee.setGender("1");
    employee.setAge(36);


    boolean result = employee.updateById();
    System.out.println("result:" +result );
    employee.update(new QueryWrapper<Employee>().eq("last_name","dsad"));
}

逻辑删除

实体类注解标注

application.properties

添加配置类

性能分析

添加一个插件

乐观锁

对应的表字段加注解

配置类

猜你喜欢

转载自blog.csdn.net/Lee_Suoer/article/details/82711869