最近自己在搞一个项目,然后想使用mybatis generator一键生成mybatis的java代码,还有xml配置,但是mybatis generator默认生成的sql相当不好用,insert、update会要求全字段set,还没有find方法,有没有人教下我这个菜,不修改mybatis generator源码,实现修改mybatis generator生成的默认sql,就像修改Dao mapper文件名样,只用继承 import org.mybatis.generator.api.PluginAdapter; 然后改改就可以实现一样,修改xml中的sql,有没有可以继承修改的方法
我想要的生成效果,如下:
<?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.cmpay.phone.dao.CustomerPopupDao">
<resultMap id="BaseResultMap" type="com.cmpay.phone.entity.TestTableDO">
<id column="serial_no" jdbcType="VARCHAR" property="serialNo" />
<result column="product_code" jdbcType="VARCHAR" property="productCode" />
<result column="effective_flag" jdbcType="VARCHAR" property="effectiveFlag" />
<result column="describe_m" jdbcType="VARCHAR" property="describe" />
<result column="prompts" jdbcType="VARCHAR" property="prompts" />
<result column="image_string" jdbcType="VARCHAR" property="imageString" />
<result column="operator_m" jdbcType="VARCHAR" property="operator" />
<result column="operate_date" jdbcType="VARCHAR" property="operateDate" />
<result column="operate_time" jdbcType="VARCHAR" property="operateTime" />
</resultMap>
<sql id="Base_Column_List">
serial_no, product_code, effective_flag, describe_m, prompts, image_string, operator_m,
operate_date, operate_time
</sql>
<select id="get" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from test_table
where product_code = #{productCode,jdbcType=VARCHAR}
</select>
<delete id="delete" parameterType="java.lang.String">
delete from test_table
where product_code = #{productCode,jdbcType=VARCHAR}
</delete>
<insert id="insert" keyColumn="serial_no" keyProperty="serialNo" parameterType="com.cmpay.phone.entity.TestTableDO" useGeneratedKeys="true">
insert into test_table
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="serialNo != null">
serial_no,
</if>
<if test="productCode != null">
product_code,
</if>
<if test="effectiveFlag != null">
effective_flag,
</if>
<if test="describe != null">
describe_m,
</if>
<if test="prompts != null">
prompts,
</if>
<if test="imageString != null">
image_string,
</if>
<if test="operator != null">
operator_m,
</if>
<if test="operateDate != null">
operate_date,
</if>
<if test="operateTime != null">
operate_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="serialNo != null">
#{serialNo,jdbcType=VARCHAR},
</if>
<if test="productCode != null">
#{productCode,jdbcType=VARCHAR},
</if>
<if test="effectiveFlag != null">
#{effectiveFlag,jdbcType=VARCHAR},
</if>
<if test="describe != null">
#{describe,jdbcType=VARCHAR},
</if>
<if test="prompts != null">
#{prompts,jdbcType=VARCHAR},
</if>
<if test="imageString != null">
#{imageString,jdbcType=VARCHAR},
</if>
<if test="operator != null">
#{operator,jdbcType=VARCHAR},
</if>
<if test="operateDate != null">
#{operateDate,jdbcType=VARCHAR},
</if>
<if test="operateTime != null">
#{operateTime,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="update" parameterType="com.cmpay.phone.entity.TestTableDO">
update test_table
<set>
<if test="productCode != null">
product_code = #{productCode,jdbcType=VARCHAR},
</if>
<if test="effectiveFlag != null">
effective_flag = #{effectiveFlag,jdbcType=VARCHAR},
</if>
<if test="describe != null">
describe_m = #{describe,jdbcType=VARCHAR},
</if>
<if test="prompts != null">
prompts = #{prompts,jdbcType=VARCHAR},
</if>
<if test="imageString != null">
image_string = #{imageString,jdbcType=VARCHAR},
</if>
<if test="operator != null">
operator_m = #{operator,jdbcType=VARCHAR},
</if>
<if test="operateDate != null">
operate_date = #{operateDate,jdbcType=VARCHAR},
</if>
<if test="operateTime != null">
operate_time = #{operateTime,jdbcType=VARCHAR},
</if>
</set>
where product_code = #{productCode,jdbcType=VARCHAR}
</update>
</mapper>
现在mybatis generator给我默认生成的效果如下:
<?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.zc.mock.dao.TestTableDao" >
<resultMap id="BaseResultMap" type="com.zc.mock.entity.TestTableDO" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="open_type" property="openType" jdbcType="INTEGER" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from test_table
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.zc.mock.entity.TestTableDO" >
insert into test_table (id, open_type)
values (#{id,jdbcType=BIGINT}, #{openType,jdbcType=INTEGER})
</insert>
<update id="updateByPrimaryKey" parameterType="com.zc.mock.entity.TestTableDO" >
update test_table
set open_type = #{openType,jdbcType=INTEGER}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select id, open_type
from test_table
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, open_type
from test_table
</select>
</mapper>
如果是仅仅是修改某一个实体类对应的单个sql,可以参考如下链接,但是如果想通过配置数据库连接信息批量生成xml文件中sql语句,估计很难实现,因为无法识别你要操作那个表以及具体哪些字段,如果非要这样实现,估计要改源码,自定义生成规则才可以。最简单的做法是项目集成mybatis-plus,这样单表的话就不用写sql了,直接继承BaseMapper后就可以调用增删改查相关的接口,而且还可以自定义sql,灵活方便https://blog.csdn.net/weixin_41547362/article/details/122862765
1.首先在pom.xml引入所需要的jar包依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2.在项目的根目录下创建一个mybatis-generator.xml如图:
mybatis-generator.xml 配置文件。
<!--去除多余的注释 -->
<commentGenerator >
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/frame"
userId="root"
password="pass">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator
targetPackage="com.hty.pojo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator
targetPackage="mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.hty.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="userInfo" domainObjectName="UserInfor"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" >
</table>
3.创建执行逆向工程的代码如下:
public class Mbg {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mybatis-generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("执行完毕");
}
}
4.执行一下上述代码,刷新项目就可以得到相应的实体类和mapper文件以及对应的mapper.xml文件。
下载code generator插件,然后自定义模板,可以解决你的问题。
通过定义模板来实现,我这里有现成的,如有需要,可以找我。我可以教你怎么自定义修改。
下面那个图就是我自己修改的模板。如有帮助,请采纳!
mapper.xml是非必须的,建议使用注解代替xml,在mapper接口上使用注解写sql,再结合mybatisplus,可以满足几乎全部的功能。
现在SQL默认都不生成了,都是用QueryWrapper,复杂的就自己写几个SQL,