项目设计_Mybatis-Plus的使用

Mybatis-Plus版本问题及代码生成器问题(2019/3/23)

所使用依赖:
<!--Mybatis Plus版本(Feb 24, 2019)-->
    <MybatisPlus.version>3.1.0</MybatisPlus.version>
    <!--MybatisPlus代码生成器版本-->
    <MPG.version>3.1.0</MPG.version>
    <!--MybatisPlus代码生成器模板引擎Apache的Velocity模板版本-->
    <ApacheVelocity.version>2.0</ApacheVelocity.version>
<!--Mybatis持久层框架使用MybatisPlus插件包,不需要再导入Mybatis和Mybatis-Spring-->
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>${MybatisPlus.version}</version>
    </dependency>
    <!--AutoGenerator是MyBatis-Plus的代码生成器,类似Mybatis的MBG-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>${MPG.version}</version>
    </dependency>
    <!--MP的代码生成器默认的模板引擎使用的是Apache的Velocity模板,也可以更换为别的模板技术-->
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-engine-core</artifactId>
      <version>${ApacheVelocity.version}</version>
    </dependency>


所使用测试类:

package test;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @ClassName: MPGenerator
 * @Description: MybatisPlus代码构造器使用
 * @Date 2019/3/23 16:13
 * @Version: 1.0
 **/
public class MPGenerator {
//通过传入表名,生成对应表信息
    @Test
    public void generateCode() {
        //管理员表
        //generateByTables("admin");
        //用户表
        //generateByTables("users");
        //学生表
        //generateByTables("student");
        //教师表
        //generateByTables("teacher");
    }

    private void generateByTables(String tableNames) {

    //1.全局配置
        GlobalConfig config = new GlobalConfig();
               //是否支持AR模式,使用类直接调用CRUD
        config.setActiveRecord(true)
                //设置作者
                .setAuthor("Carlson")
                //设置生成代码位置(绝对地址)
                .setOutputDir("D:\\Java\\src\\main\\java")
                //多次生成文件覆盖
                .setFileOverride(true)
                //主键策略,标明主键
                .setIdType(IdType.AUTO)
                //生成mapper.xml映射文件
                .setBaseResultMap(true)
                //生成ResultMap
                .setBaseColumnList(true);
        // 自定义文件命名,注意 %s 会自动填充表实体属性!,不使用“I”开头命名
        config.setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");


    //2.数据源配置
        //连接地址
        String dbUrl = "数据库连接地址";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
                        //设置数据库类型
        dataSourceConfig.setDbType(DbType.MYSQL)
                //设置数据连接信息
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.cj.jdbc.Driver");


    //3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                //全局大写命名
                .setCapitalMode(true)
                //配置使用Lombok
                .setEntityLombokModel(true)
                //表明生成策略,驼峰命名
                .setNaming(NamingStrategy.underline_to_camel)
                //需要生成的表,多个表名传数组
                .setInclude(tableNames);
                //此版本没有该属性
                //setDbColumnUnderline(true)//全局下划线命名

     //4.包配置
        PackageConfig packageConfig = new PackageConfig();
                      //设置父包
        packageConfig.setParent("edu.hut.hz")
                .setMapper("dao")
                .setXml("dao.mapper")
                .setService("services")
                .setServiceImpl("servicesImpl")
                .setController("controller")
                .setEntity("pojo");

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

        autoGenerator.setGlobalConfig(config)
                    .setDataSource(dataSourceConfig)
                    .setStrategy(strategyConfig)
                    .setPackageInfo(packageConfig);

     //6.执行
        autoGenerator.execute();
    }
}

问题1:
  表的字段名不能使用Java关键字。

问题2:

@RequestMapping("/admin")写在方法上。(默认写在类上为父路径)

其他:

protected Serializable pkVal() {
        return this.userId;
    }
即使用AR(ActiveRecord)继承 Model<>所要重写方法。

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

}
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
    @Autowired
    protected M baseMapper;}

所使用的AdminServiceImpl继承了ServiceImpl,里面已经注入baseMapper,无需再注入,即可直接使用已有方法。

Mapper通用方法和Service通用方法:
 

Mapper通用方法和Service通用方法对照:
Mapper CRUD 接口

 

Service CRUD 接口

 

insert(同右)save(插入一条记录)
deleteByIdremoveById(根据ID删除)
deleteByMapremoveByMap(根据 columnMap 条件,删除记录)
deleteremove(根据 entity 条件,删除记录)
deleteBatchIdsremoveByIds(删除(根据ID 批量删除))
updateByIdupdateById(根据 ID 选择修改)
updateupdate(根据 whereEntity 条件,更新记录)
selectByIdgetById(根据 ID 查询)
selectBatchIdslistByIds(查询(根据ID 批量查询))
selectByMapgetMap(查询(根据 columnMap 条件))
selectOne(根据 entity 条件,查询一条记录)

getOne(根据 Wrapper,查询一条记录)

  getOne(Wrapper<T> queryWrapper,

boolean throwEx);

selectCountcount(根据 Wrapper 条件,查询总记录数)
selectList(根据 entity 条件,查询全部记录)list(查询列表)
selectMaps(根据 Wrapper 条件,查询全部记录)listByMap(查询(根据 columnMap 条件))
selectObjs(根据 Wrapper 条件,查询全部记录)

getObj(根据 Wrapper,查询一条记录)

  Object getObj(Wrapper<T> queryWrapper);

selectPage(根据 entity 条件,查询全部记录(并翻页))

page(翻页查询)

  IPage<T> page(IPage<T> page,

Wrapper<T> queryWrapper);

selectMapsPage(根据 Wrapper 条件,查询全部记录(并翻页))

pageMaps(翻页查询)

  IPage<Map<String, Object>>

pageMaps(IPage<T> page,

Wrapper<T> queryWrapper);

 

saveBatch( 插入(批量))

  boolean saveBatch(T entity);

 

saveBatch

 boolean saveBatch(Collection<T> entityList, int batchSize);

 

saveOrUpdateBatch(批量修改插入)(与上类同)

  @param entityList 实体对象集合

 

saveOrUpdateBatch
  @param entityList 实体对象集合
  @param batchSize  每次的数量

 updateBatchById(根据ID 批量更新)
 saveOrUpdate(TableId 注解存在更新记录,否插入一条记录)
 listMaps(查询列表)
 listObjs(根据 Wrapper 条件,查询全部记录)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值