mybaties generator 表名/字段名下划线转驼峰

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(3591)   2023-03-28 11:29:14

一、前言

mybaties generator在使用mybaties框架开发的时候会给我们省下一大笔写基础代码的时间。对我们程序员来说真是太好啦。(废话结束)

二、环境准备

spring boot 2.0 idea项目
测试表格DDL语句:
CREATE TABLE
    user_info
    (
        user_address VARCHAR(50),
        user_nickname VARCHAR(50),
        user_name VARCHAR(50)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE=utf8_general_ci;
 

三、mybaties generator 驼峰配置

主要是改mybaties generator的配置文件。可参考之前发布的idea Mybatis generator插件的配置和使用 查看默认配置
修改表格部分如下:
 
<table tableName="user_info"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false" />
            <!--true:MyBatis Generator会使用数据库中实际的字段名字作为生成的实体类的属性名。
          false:这是默认值。如果设置为false,则MyBatis Generator会将数据库中实际的字段名字转换为Camel Case风格作为生成的实体类的属性名。-->
        </table>
修改配置完成后就可以实现表明字段名都下划线转驼峰啦

四、测试结果

按照之前一篇文章idea Mybatis generator插件的配置和使用讲解的,配置好后运行插件我们可以看到生成的代码结果如下
$title(UserInfo.java)
package com.leftso.demo.business.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

public class UserInfo implements Serializable {
    private String userAddress;

    private String userNickname;

    private String userName;

    private static final long serialVersionUID = 1L;

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress == null ? null : userAddress.trim();
    }

    public String getUserNickname() {
        return userNickname;
    }

    public void setUserNickname(String userNickname) {
        this.userNickname = userNickname == null ? null : userNickname.trim();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    /**
     * This enum was generated by MyBatis Generator.
     * This enum corresponds to the database table user_info
     *
     * @mbg.generated
     * @project https://github.com/itfsw/mybatis-generator-plugin
     */
    public enum Column {
        userAddress("user_address", "userAddress", "VARCHAR", false),
        userNickname("user_nickname", "userNickname", "VARCHAR", false),
        userName("user_name", "userName", "VARCHAR", false);

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private static final String BEGINNING_DELIMITER = "\"";

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private static final String ENDING_DELIMITER = "\"";

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private final String column;

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private final boolean isColumnNameDelimited;

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private final String javaProperty;

        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        private final String jdbcType;

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String value() {
            return this.column;
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String getValue() {
            return this.column;
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String getJavaProperty() {
            return this.javaProperty;
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String getJdbcType() {
            return this.jdbcType;
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table user_info
         *
         * @mbg.generated
         * @project https://github.com/itfsw/mybatis-generator-plugin
         */
        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }
    }
}
由于引入了三方插件,看起来并不是特别清爽,不用在意。看字段名就好哈哈。。。
$title(UserInfoMapper.java)
package com.leftso.demo.business.mapper;

import com.leftso.demo.business.model.UserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserInfoMapper {
    int insert(UserInfo record);

    int insertSelective(UserInfo record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user_info
     *
     * @mbg.generated
     * @project https://github.com/itfsw/mybatis-generator-plugin
     */
    int batchInsert(@Param("list") java.util.List<UserInfo> list);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user_info
     *
     * @mbg.generated
     * @project https://github.com/itfsw/mybatis-generator-plugin
     */
    int batchInsertSelective(@Param("list") java.util.List<UserInfo> list, @Param("selective") UserInfo.Column ... selective);
}
$title(UserInfoMapper.xml)
<?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.leftso.demo.business.mapper.UserInfoMapper">
  <resultMap id="BaseResultMap" type="com.leftso.demo.business.model.UserInfo">
    <result column="user_address" jdbcType="VARCHAR" property="userAddress" />
    <result column="user_nickname" jdbcType="VARCHAR" property="userNickname" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
  </resultMap>
  <insert id="insert" parameterType="com.leftso.demo.business.model.UserInfo">
    insert into user_info (user_address, user_nickname, user_name
      )
    values (#{userAddress,jdbcType=VARCHAR}, #{userNickname,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.leftso.demo.business.model.UserInfo">
    insert into user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userAddress != null">
        user_address,
      </if>
      <if test="userNickname != null">
        user_nickname,
      </if>
      <if test="userName != null">
        user_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userAddress != null">
        #{userAddress,jdbcType=VARCHAR},
      </if>
      <if test="userNickname != null">
        #{userNickname,jdbcType=VARCHAR},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <insert id="batchInsert" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      @project https://github.com/itfsw/mybatis-generator-plugin
    -->
    insert into user_info
    (user_address, user_nickname, user_name)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.userAddress,jdbcType=VARCHAR}, #{item.userNickname,jdbcType=VARCHAR}, #{item.userName,jdbcType=VARCHAR}
        )
    </foreach>
  </insert>
  <insert id="batchInsertSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      @project https://github.com/itfsw/mybatis-generator-plugin
    -->
    insert into user_info (
    <foreach collection="selective" item="column" separator=",">
      ${column.escapedColumnName}
    </foreach>
    )
    values
    <foreach collection="list" item="item" separator=",">
      (
      <foreach collection="selective" item="column" separator=",">
        <if test="'user_address'.toString() == column.value">
          #{item.userAddress,jdbcType=VARCHAR}
        </if>
        <if test="'user_nickname'.toString() == column.value">
          #{item.userNickname,jdbcType=VARCHAR}
        </if>
        <if test="'user_name'.toString() == column.value">
          #{item.userName,jdbcType=VARCHAR}
        </if>
      </foreach>
      )
    </foreach>
  </insert>
</mapper>

结束Over.!
 
地址:https://www.leftso.com/article/505.html

相关阅读

mybaties generator 表名/字段名下划线转驼峰,Spring Boot 2.0 整合 mybaties generator 表名/字段名下划线转驼峰 idea上的配置
idea Mybatis generator插件的配置和使用,在eclipse中,我们可以在eclipse的插件市场安装一个Mybatis generator的插件。然后对Mybatis gen...
       本文主要讲解在使用mybaties中通过mybaties generator生成基本操作代码,然后通过 mybaties mapper 继承机制来解决某些情况下经常改表导致改map...
spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个...
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
spring boot框架整合mybaties数据库暂时选用MySQL
某些情况下,我们使用mybaties时需要使用IN(虽然IN数据多了效率不高,但是少量还是可以用得)条件查询,这时候我们就需要传递参数了,下面是mybaties处理IN条件得参数使用方法首先Ma...
mybaties like参数写法 and a.link like CONCAT(CONCAT('%', #{params.link}), '%')
mybatis plus 逻辑删除使用说明全局逻辑值配置,application.properties# 逻辑已删除值(默认为 1) mybatis-plus.global-config.db...