Spring框架引入MyBatis框架并完成Mapper的配置替代JDBC-TEMPLATE-----Spring框架

132 篇文章 0 订阅
131 篇文章 0 订阅
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.powernode.bank.mappers.AccountMapper">
  <resultMap id="BaseResultMap" type="com.powernode.bank.pojo.Account">
    <id column="actno" jdbcType="VARCHAR" property="actno" />
    <result column="balance" jdbcType="DOUBLE" property="balance" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    actno, balance
  </sql>
  <update id="update">
    update t_act set balance = #{balance} where actnp = #{actno}
  </update>
  <select id="selectAll" resultType="Account">
    select * from t_act
  </select>
  <select id="selectByExample" parameterType="com.powernode.bank.pojo.AccountExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByActno" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_act
    where actno = #{actno,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByActno" parameterType="java.lang.String">
    delete from t_act
    where actno = #{actno,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.powernode.bank.pojo.AccountExample">
    delete from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.powernode.bank.pojo.Account">
    insert into t_act (actno, balance)
    values (#{actno,jdbcType=VARCHAR}, #{balance,jdbcType=DOUBLE})
  </insert>
  <insert id="insertSelective" parameterType="com.powernode.bank.pojo.Account">
    insert into t_act
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="actno != null">
        actno,
      </if>
      <if test="balance != null">
        balance,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="actno != null">
        #{actno,jdbcType=VARCHAR},
      </if>
      <if test="balance != null">
        #{balance,jdbcType=DOUBLE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.powernode.bank.pojo.AccountExample" resultType="java.lang.Long">
    select count(*) from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update t_act
    <set>
      <if test="row.actno != null">
        actno = #{row.actno,jdbcType=VARCHAR},
      </if>
      <if test="row.balance != null">
        balance = #{row.balance,jdbcType=DOUBLE},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update t_act
    set actno = #{row.actno,jdbcType=VARCHAR},
      balance = #{row.balance,jdbcType=DOUBLE}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.powernode.bank.pojo.Account">
    update t_act
    <set>
      <if test="balance != null">
        balance = #{balance,jdbcType=DOUBLE},
      </if>
    </set>
    where actno = #{actno,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.powernode.bank.pojo.Account">
    update t_act
    set balance = #{balance,jdbcType=DOUBLE}
    where actno = #{actno,jdbcType=VARCHAR}
  </update>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.powernode.bank.mappers.AccountMapper">
  <resultMap id="BaseResultMap" type="com.powernode.bank.pojo.Account">
    <id column="actno" jdbcType="VARCHAR" property="actno" />
    <result column="balance" jdbcType="DOUBLE" property="balance" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    actno, balance
  </sql>
  <update id="update">
    update t_act set balance = #{balance} where actnp = #{actno}
  </update>
  <select id="selectAll" resultType="Account">
    select * from t_act
  </select>
  <select id="selectByExample" parameterType="com.powernode.bank.pojo.AccountExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByActno" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_act
    where actno = #{actno,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByActno" parameterType="java.lang.String">
    delete from t_act
    where actno = #{actno,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.powernode.bank.pojo.AccountExample">
    delete from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.powernode.bank.pojo.Account">
    insert into t_act (actno, balance)
    values (#{actno,jdbcType=VARCHAR}, #{balance,jdbcType=DOUBLE})
  </insert>
  <insert id="insertSelective" parameterType="com.powernode.bank.pojo.Account">
    insert into t_act
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="actno != null">
        actno,
      </if>
      <if test="balance != null">
        balance,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="actno != null">
        #{actno,jdbcType=VARCHAR},
      </if>
      <if test="balance != null">
        #{balance,jdbcType=DOUBLE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.powernode.bank.pojo.AccountExample" resultType="java.lang.Long">
    select count(*) from t_act
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update t_act
    <set>
      <if test="row.actno != null">
        actno = #{row.actno,jdbcType=VARCHAR},
      </if>
      <if test="row.balance != null">
        balance = #{row.balance,jdbcType=DOUBLE},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update t_act
    set actno = #{row.actno,jdbcType=VARCHAR},
      balance = #{row.balance,jdbcType=DOUBLE}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.powernode.bank.pojo.Account">
    update t_act
    <set>
      <if test="balance != null">
        balance = #{balance,jdbcType=DOUBLE},
      </if>
    </set>
    where actno = #{actno,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.powernode.bank.pojo.Account">
    update t_act
    set balance = #{balance,jdbcType=DOUBLE}
    where actno = #{actno,jdbcType=VARCHAR}
  </update>
</mapper>
package com.powernode.bank.mappers;

import com.powernode.bank.pojo.Account;
import com.powernode.bank.pojo.AccountExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface AccountMapper {
    long countByExample(AccountExample example);

    int deleteByExample(AccountExample example);

    int deleteByActno(String actno);

    int insert(Account row);

    int update(Account act);

    int insertSelective(Account row);
    List<Account> selectAll();

    List<Account> selectByExample(AccountExample example);

    Account selectByActno(String actno);

    int updateByExampleSelective(@Param("row") Account row, @Param("example") AccountExample example);

    int updateByExample(@Param("row") Account row, @Param("example") AccountExample example);

    int updateByPrimaryKeySelective(Account row);

    int updateByPrimaryKey(Account row);
}

package com.powernode.bank.mappers;

import com.powernode.bank.pojo.Account;
import com.powernode.bank.pojo.AccountExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface AccountMapper {
    long countByExample(AccountExample example);

    int deleteByExample(AccountExample example);

    int deleteByActno(String actno);

    int insert(Account row);

    int update(Account act);

    int insertSelective(Account row);
    List<Account> selectAll();

    List<Account> selectByExample(AccountExample example);

    Account selectByActno(String actno);

    int updateByExampleSelective(@Param("row") Account row, @Param("example") AccountExample example);

    int updateByExample(@Param("row") Account row, @Param("example") AccountExample example);

    int updateByPrimaryKeySelective(Account row);

    int updateByPrimaryKey(Account row);
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powernode</groupId>
    <artifactId>spring6-015-sm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <repositories>
        <!--Spring里程碑版本的仓库-->
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <dependencies>
        <!--spring context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!--当你引入Spring Context后,表示将Spring的基础依赖引入了-->
        <!--但想使用Spring的JDBC或其他的tx时,还需要再次添加依赖-->
        <!--引入Log4j2的依赖-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.19.0</version>
        </dependency>
        <!--JUNIT依赖-->
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!--    配置mybatis逆向工程的插件,build表示构建逆向工程-->
    <build>
        <!--        plugins表示配置多个插件-->
        <plugins>
            <plugin>
                <!--                插件的坐标-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <!--                允许覆盖,必须要有,防止生成的内容不是覆盖而是追加到源程序上导致错误-->
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powernode</groupId>
    <artifactId>spring6-015-sm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <repositories>
        <!--Spring里程碑版本的仓库-->
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <dependencies>
        <!--spring context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!--当你引入Spring Context后,表示将Spring的基础依赖引入了-->
        <!--但想使用Spring的JDBC或其他的tx时,还需要再次添加依赖-->
        <!--引入Log4j2的依赖-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.1.0-M2</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.19.0</version>
        </dependency>
        <!--JUNIT依赖-->
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!--    配置mybatis逆向工程的插件,build表示构建逆向工程-->
    <build>
        <!--        plugins表示配置多个插件-->
        <plugins>
            <plugin>
                <!--                插件的坐标-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <!--                允许覆盖,必须要有,防止生成的内容不是覆盖而是追加到源程序上导致错误-->
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
package com.powernode.bank.pojo;

public class Account {
    private String actno;

    private Double balance;

    public Account() {
    }

    @Override
    public String toString() {
        return "Account{" +
                "actno='" + actno + '\'' +
                ", balance=" + balance +
                '}';
    }

    public Account(String actno, Double balance) {
        this.actno = actno;
        this.balance = balance;
    }

    public String getActno() {
        return actno;
    }

    public void setActno(String actno) {
        this.actno = actno == null ? null : actno.trim();
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
}

package com.powernode.bank.pojo;

public class Account {
    private String actno;

    private Double balance;

    public Account() {
    }

    @Override
    public String toString() {
        return "Account{" +
                "actno='" + actno + '\'' +
                ", balance=" + balance +
                '}';
    }

    public Account(String actno, Double balance) {
        this.actno = actno;
        this.balance = balance;
    }

    public String getActno() {
        return actno;
    }

    public void setActno(String actno) {
        this.actno = actno == null ? null : actno.trim();
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是MyBatis的增强工具包,提供了许多便于开发的功能,其中之一就是代码生成器。代码生成器可以根据数据库表自动生成实体类、mapper接口、XML文件等代码。 使用MyBatis-Plus代码生成器,需要先在项目的pom.xml或build.gradle文件中添加相关依赖: ``` <!-- pom.xml --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>最新版本号</version> </dependency> // build.gradle compile group: 'com.baomidou', name: 'mybatis-plus-generator', version: '最新版本号' ``` 然后,在Spring Boot的配置类上添加如下配置: ``` @Configuration public class MybatisPlusConfig { @Bean public GlobalConfig globalConfig() { GlobalConfig config = new GlobalConfig(); // 配置生成的代码的输出目录 config.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); config.setAuthor("your name"); // 是否覆盖已有文件 config.setFileOverride(true); // 是否在xml中添加二级缓存配置 config.setEnableCache(false); // 自定义数据类型转换器 config.setTypeConvert(new MySqlTypeConvert()); // 生成后立即打开文件夹 config.setOpen(false); // service命名方式,null为使用默认方式,即去掉I前缀 config.setServiceName("%sService"); return config; } @Bean public DataSource dataSource() { // 自行实现 } @Bean public DataSourceConfig dataSourceConfig() { DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setTypeConvert(new MySqlTypeConvert()); dataSourceConfig.setDriverName("com.mysql.jdbc.Driver"); dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/db_name"); dataSourceConfig.setUsername("username"); dataSourceConfig.setPassword("password"); return dataSourceConfig; } @Bean public StrategyConfig strategyConfig() { StrategyConfig strategyConfig = new StrategyConfig(); // 全局大写命名 ORACLE 注意 strategyConfig.setCapitalMode(true); // 需要生成的表 strategyConfig.setInclude("table1", "table2"); // 数据库表映射到实体的命名策略 strategyConfig.setNaming(NamingStrategy.underline_to_camel); // 数据库表字段映射到实体的命名策略 strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); // 是否生成实体时,生成字段注解 strategyConfig.setEntityTableFieldAnnotationEnable(true); // 是否生成基础的Entity类 strategyConfig.setEntityBuilderModel(false); return strategyConfig; } @Bean public TemplateConfig templateConfig() { TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setEntity("templates/entity.java"); templateConfig.setMapper("templates/mapper.java"); templateConfig.setXml("templates/mapper.xml"); return templateConfig; } @Bean public AutoGenerator autoGenerator() { AutoGenerator autoGenerator = new AutoGenerator(); autoGenerator.setGlobalConfig(globalConfig()); autoGenerator.setDataSource(dataSource()); autoGenerator.setDataSourceConfig(dataSourceConfig()); autoGenerator.setStrategy(strategyConfig()); autoGenerator.setTemplate(templateConfig()); return autoGenerator; } } ``` 在上述配置中,需要特别注意templateConfig中的各项配置,它们指定了生成的文件的模板路径,可以按照自己的需求进行修改。 最后,在main方法中调用代码生成器即可: ``` @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); // 使用代码生成器 AutoGenerator autoGenerator = SpringContextHolder.getBean(AutoGenerator.class); autoGenerator.execute(); } } ``` 执行完代码生成器后,就会在指定的输出目录中生成实体类、mapper接口、XML文件等代码文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值