每天每天
越来越爱

mybatis-plus使用记录 mybatis-plus一对多

自动生成代码

<dependency>
  <groupId>org.mariadb.jdbc</groupId>
  <artifactId>mariadb-java-client</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.0.5</version>
</dependency>
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity-engine-core</artifactId>
  <version>2.0</version>
</dependency>
public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 获取当前路径
        String projectPath = System.getProperty("user.dir");
        // 写入到目录
        gc.setOutputDir(projectPath + "/");
        // 设置作者
        gc.setAuthor("");
        // 是否打开资源管理器
        gc.setOpen(false);
        // 是否覆盖
        gc.setFileOverride(true);
        // 去掉service前面的i
        gc.setServiceName("%sService");
        // 设置时间格式
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("org.mariadb.jdbc.Driver");
        dsc.setUrl("url");
        dsc.setPassword("password");
        dsc.setUsername("user");
        dsc.setDbType(DbType.MARIADB);

        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("leave");
        pc.setParent("com.***.***");
        pc.setController("controller");
        pc.setService("service");
        pc.setMapper("mapper");
        pc.setEntity("entity");
        mpg.setPackageInfo(pc);

        // 策略,逻辑删除。。。
        StrategyConfig strategy = new StrategyConfig();
        // 要映射的表
        // strategy.setInclude("f_bill","f_bill_period","f_chargeoff_period","sale_info","pay_info");
        strategy.setInclude("template_tag");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 自动使用lombok
        strategy.setEntityLombokModel(true);

        // 逻辑删除
        strategy.setLogicDeleteFieldName("is_delete");
        // 自动填充
        TableFill createTime = new TableFill("created_at", FieldFill.INSERT);
        TableFill updateTime = new TableFill("modified_at", FieldFill.INSERT_UPDATE);
        List<TableFill> tableFills = new ArrayList<TableFill>(2);
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);

        mpg.setStrategy(strategy);

        mpg.execute();
    }

在数据库中没有字段,但是在实体类中需要用到的属性,需要加@TableField(exist = false) 注解

分页插件

使用分页插件如果存在多表关联查询一对多数据时,会出现查询条数不正确问题。

解决办法如下:

Page<BArticle> articlePage(Page<BArticle> page,@Param("ew") QueryWrapper<BArticle> queryWrapper);

  <resultMap id="articleInfo" type="com.***.entity.BArticle">
    <id column="article_id" property="articleId" />
    <result column="title" property="title" />
    <result column="img" property="img" />
    <result column="is_public" property="isPublic" />
    <result column="status" property="status" />
    <result column="des" property="des" />
    <result column="md_content" property="mdContent" />
    <result column="creater_id" property="createrId" />
    <result column="thumhup" property="thumhup" />
    <result column="views" property="views" />
    <result column="html_content" property="htmlContent" />
    <result column="category_id" property="categoryId" />
    <result column="is_stick" property="isStick" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />

    <collection property="tags" ofType="java.lang.Integer" column="{articleId = article_id}" select="tagsInfo"></collection>
  </resultMap>
  <select id="articlePage" resultMap="articleInfo">
    select * from b_article ${ew.customSqlSegment}
  </select>
  <select id="tagsInfo" resultType="java.lang.Integer">
    select aa.tag_id  from b_tag aa , b_article_tag bb where aa.tag_id = bb.tag_id and bb.article_id = #{articleId}
  </select>

多表条件查询

@Repository
public interface SUserMapper extends BaseMapper<SUser> {
    Page<SUser> listUser(Page page,@Param("ew") QueryWrapper queryWrapper);
}
@Service
public Page<SUser> listUser(String token, Map<String, String> map) {
        Page<SUser> sUserPage = null;
        Page<SUser> page = new Page<>(pageNo, size);
        QueryWrapper<SUser> userServiceQueryWrapper = new QueryWrapper<>();
        // 拼接条件,多个表中字段名相同的时候,在前面加表前缀
        userServiceQueryWrapper.eq(!StringUtils.isEmpty(map.get("compId")), "aa.comp_id", map.get("compId"));
        userServiceQueryWrapper.eq(!StringUtils.isEmpty(map.get("orgId")), "aa.org_id", map.get("orgId"));
        userServiceQueryWrapper.eq(!StringUtils.isEmpty(map.get("userName")), "aa.user_name", map.get("userName"));
        userServiceQueryWrapper.eq(!StringUtils.isEmpty(map.get("name")), "aa.comp_id", map.get("name"));
        userServiceQueryWrapper.eq("aa.comp_id", user.getCompId());
        userServiceQueryWrapper.and(qw->qw.eq().or().eq())
        sUserPage = userMapper.listUser(page,userServiceQueryWrapper);
    mapper中的条件
    // ${ew.customSqlSegment}

        return sUserPage;
    }

在使用mybatis添加数据的时候,数据添加成功后没有返回主键id,添加 keyProperty="id" keyColumn="id"

<insert id="saveTalentInfo" keyProperty="id" keyColumn="id">

</insert>

mybatis的xml中使用来判断时,如果属性的类型为Integer 类型,但是传参数为0 这时候 mybatis会默认为null 而不拼接查询条件,可以使用 or的方式。

<if test="status !='' and status !=null or status ==0" >
    and status = #{status}
</if>

用到了再补充......

赞(0) 打赏

评论 抢沙发