广告位联系
返回顶部
分享到

若依后端MyBatis改为MyBatis-Plus方式

java 来源:互联网 作者:佚名 发布时间:2024-11-16 10:48:22 人浏览
摘要

1.引入MyBatis-Plus依赖 在总目录的pom.xml,导入依赖 1 2 3 4 5 6 7 8 mybatis-plus.version3.4.3/mybatis-plus.version !-- mybatis-plus 增强CRUD -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId versi

1.引入MyBatis-Plus依赖

在总目录的pom.xml,导入依赖

1

2

3

4

5

6

7

8

<mybatis-plus.version>3.4.3</mybatis-plus.version>

 

<!-- mybatis-plus 增强CRUD -->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>${mybatis-plus.version}</version>

</dependency>

2.在ruoyi-common下的pom.xml添加mybatis-plus依赖

1

2

3

4

5

<!-- mybatis-plus 增强CRUD -->

<dependency>

    <groupId>com.baomidou</groupId>

    <artifactId>mybatis-plus-boot-starter</artifactId>

</dependency>

3.修改配置文件

ruoyi-admin下src\main\resources\application.yml,注释mybatis的配置,新增plus的配置,这段配置我是从若依官方拷贝过来的,如果跟你实际不一样就修改一下

1

2

3

4

5

6

7

8

9

# MyBatis配置

#mybatis:

#  # 搜索指定包别名

#  typeAliasesPackage: com.spiceIngredients.**.domain

#  # 配置mapper的扫描,找到所有的mapper.xml映射文件

#  mapperLocations: classpath*:mapper/**/*Mapper.xml

#  # 加载全局的配置文件

#  configLocation: classpath:mybatis/mybatis-config.xml

# MyBatis Plus配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

mybatis-plus:

  # 搜索指定包别名

  typeAliasesPackage: com.spiceIngredients.**.domain

  # 配置mapper的扫描,找到所有的mapper.xml映射文件

  mapperLocations: classpath*:mapper/**/*Mapper.xml

  # 加载全局的配置文件

  configLocation: classpath:mybatis/mybatis-config.xml

  # MyBatis-Plus分页插件配置

  pagination:

    # 分页合理化参数,默认false

    reasonable: true

    # 支持通过 Mapper 接口参数来传递分页参数

    supportMethodsArguments: true

    # 默认值为false,设置为true时,允许在参数中使用countSql

    params: count=countSql

4.重写mybatis配置文件

在framework中,找到MybatisConfig文件,然后删除,在刚删除的文件夹下,新建一个MybatisPlus的java文件,复制已下内容到java文件中:

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

package com.spiceIngredients.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;

 

/**

 * Mybatis Plus 配置

 *

 * @author ruoyi

 */

@EnableTransactionManagement(proxyTargetClass = true)

@Configuration

public class MybatisPlusConfig

{

    @Bean

    public MybatisPlusInterceptor mybatisPlusInterceptor()

    {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页插件

        interceptor.addInnerInterceptor(paginationInnerInterceptor());

        // 乐观锁插件

        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());

        // 阻断插件

        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());

        return interceptor;

    }

 

    /**

     * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html

     */

    public PaginationInnerInterceptor paginationInnerInterceptor()

    {

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();

        // 设置数据库类型为mysql

        paginationInnerInterceptor.setDbType(DbType.MYSQL);

        // 设置最大单页限制数量,默认 500 条,-1 不受限制

        paginationInnerInterceptor.setMaxLimit(-1L);

        return paginationInnerInterceptor;

    }

 

    /**

     * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html

     */

    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()

    {

        return new OptimisticLockerInnerInterceptor();

    }

 

    /**

     * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html

     */

    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()

    {

        return new BlockAttackInnerInterceptor();

    }

}

5.把代码生成修改为mybatis-plus版本

ruoyi-generator下src\main\resources\vm\java 文件夹下面,直接文件内容替换:

1. domain.java.vm

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

package ${packageName}.domain;

 

#foreach ($import in $importList)

import ${import};

#end

import com.baomidou.mybatisplus.annotation.IdType;

import com.baomidou.mybatisplus.annotation.TableId;

import com.baomidou.mybatisplus.annotation.TableName;

import org.apache.commons.lang3.builder.ToStringBuilder;

import org.apache.commons.lang3.builder.ToStringStyle;

import com.rchuing.sis.common.annotation.Excel;

import java.io.Serializable;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;

#if($table.crud || $table.sub)

import com.rchuing.sis.common.core.domain.BaseEntity;

#elseif($table.tree)

import com.fenghuang.common.core.domain.TreeEntity;

#end

 

/**

 * ${functionName}对象 ${tableName}

 *

 * @author ${author}

 * @date ${datetime}

 */

#if($table.crud || $table.sub)

    #set($Entity="BaseEntity")

#elseif($table.tree)

    #set($Entity="TreeEntity")

#end

@Data

@Builder

@AllArgsConstructor

@NoArgsConstructor

@TableName("${tableName}")

public class ${ClassName} implements Serializable

        {

private static final long serialVersionUID = 1L;

 

#foreach ($column in $columns)

    #if(!$table.isSuperColumn($column.javaField))

    /** $column.columnComment */

        #if($column.list)

            #set($parentheseIndex=$column.columnComment.indexOf("("))

            #if($parentheseIndex != -1)

                #set($comment=$column.columnComment.substring(0, $parentheseIndex))

            #else

                #set($comment=$column.columnComment)

            #end

            #if($parentheseIndex != -1)

            @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")

            @TableField("`$column.columnName`")

            #elseif($column.javaType == 'Date')

            @JsonFormat(pattern = "yyyy-MM-dd")

            @Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")

            @TableField("`$column.columnName`")

            #else

            @Excel(name = "${comment}")

            @TableField("`$column.columnName`")

            #end

        #end

        #if($column.isPk == 1)

        @TableId(type = IdType.AUTO)

        #end

 

    private $column.javaType $column.javaField;

 

    #end

#end

2. mapper.java.vm

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

package ${packageName}.mapper;

 

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import ${packageName}.domain.${ClassName};

#if($table.sub)

import ${packageName}.domain.${subClassName};

#end

 

/**

 * ${functionName}Mapper接口

 *

 * @author ${author}

 * @date ${datetime}

 */

public interface ${ClassName}Mapper extends BaseMapper<${ClassName}> {

 

}

3. service.java.vm

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package ${packageName}.service;

 

import java.util.List;

import ${packageName}.domain.${ClassName};

import com.baomidou.mybatisplus.extension.service.IService;

 

/**

 * ${functionName}Service接口

 *

 * @author ${author}

 * @date ${datetime}

 */

public interface I${ClassName}Service extends IService<${ClassName}> {

 

}

4. serviceimpl.java.vm

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

32

package ${packageName}.service.impl;

 

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

    #foreach ($column in $columns)

        #if($column.javaField == 'createTime' || $column.javaField == 'updateTime')

        import com.rchuing.common.utils.DateUtils;

            #break

        #end

    #end

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

    #if($table.sub)

    import java.util.ArrayList;

    import com.rchuing.common.utils.StringUtils;

    import org.springframework.transaction.annotation.Transactional;

    import ${packageName}.domain.${subClassName};

    #end

import ${packageName}.mapper.${ClassName}Mapper;

import ${packageName}.domain.${ClassName};

import ${packageName}.service.I${ClassName}Service;

 

/**

 * ${functionName}Service业务层处理

 *

 * @author ${author}

 * @date ${datetime}

 */

@Service

public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service {

 

}

5.sub-domain.java.vm

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

package ${packageName}.domain;

 

    #foreach ($import in $subImportList)

    import ${import};

    #end

import org.apache.commons.lang3.builder.ToStringBuilder;

import org.apache.commons.lang3.builder.ToStringStyle;

import com.rchuing.common.annotation.Excel;

import com.rchuing.common.core.domain.BaseEntity;

 

/**

 * ${subTable.functionName}对象 ${subTableName}

 *

 * @author ${author}

 * @date ${datetime}

 */

public class ${subClassName} extends BaseEntity {

private static final long serialVersionUID = 1L;

 

#foreach ($column in $subTable.columns)

    #if(!$table.isSuperColumn($column.javaField))

    /** $column.columnComment */

        #if($column.list)

            #set($parentheseIndex=$column.columnComment.indexOf("("))

            #if($parentheseIndex != -1)

                #set($comment=$column.columnComment.substring(0, $parentheseIndex))

            #else

                #set($comment=$column.columnComment)

            #end

            #if($parentheseIndex != -1)

            @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")

            #elseif($column.javaType == 'Date')

            @JsonFormat(pattern = "yyyy-MM-dd")

            @Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")

            #else

            @Excel(name = "${comment}")

            #end

        #end

    private $column.javaType $column.javaField;

 

    #end

#end

#foreach ($column in $subTable.columns)

    #if(!$table.isSuperColumn($column.javaField))

        #if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))

            #set($AttrName=$column.javaField)

        #else

            #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})

        #end

    public void set${AttrName}($column.javaType $column.javaField) {

            this.$column.javaField = $column.javaField;

            }

 

    public $column.javaType get${AttrName}() {

            return $column.javaField;

            }

    #end

#end

 

@Override

public String toString() {

        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

    #foreach ($column in $subTable.columns)

        #if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))

            #set($AttrName=$column.javaField)

        #else

            #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})

        #end

            .append("${column.javaField}", get${AttrName}())

    #end

        .toString();

        }

        }


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计