日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis Generator的使用

發布時間:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis Generator的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在寫代碼過程中,常常要寫一些簡單的CURD操作,為了能夠把時間用在業務邏輯上,看了Mybatis Generator生成工具,根據官網的文檔,改成適合自己使用的生成器。

mybatis generator的配置文件 如下:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!--讀取配置文件--><properties resource="generator.properties" /><context id="MySQLContext" targetRuntime="MyBatis3"><!--設置文件編碼--><property name="javaFileEncoding" value="UTF-8"/><!--配置去掉所有生成的注釋--><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!--設置數據庫連接驅動--><jdbcConnection driverClass="${jdbc.driverClass}"connectionURL="${jdbc.url}"userId="${jdbc.username}"password="${jdbc.password}"></jdbcConnection><!--當字段類型是 DECIMAL或者 NUMERIC時,是否強制轉換為BigDecimal,否的話會自動根據規模的大小選擇合適的類型 --><javaTypeResolver ><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 生成模型的包名和位置--><javaModelGenerator targetPackage="me.xueyao.model" targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成映射文件的包名和位置--><sqlMapGenerator targetPackage="me.xueyao.mapper" targetProject=".\src\main\resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置--><javaClientGenerator type="XMLMAPPER" targetPackage="me.xueyao.mapper"targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名,需要根據自己的需求修改--><table tableName="candidate" domainObjectName="Candidate" enableCountByExample="false"enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"><generatedKey column="id" sqlStatement="MySql" identity="true" /></table></context> </generatorConfiguration>

mybatis generator的執行文件 如下:

package me.xueyao;import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback;import java.io.InputStream; import java.util.ArrayList; import java.util.List;/*** @Description: Mybatis Generator 生成器* @Author: Simon.Xue* @Date: 2019/1/18 13:44*/ public class Generator {public static void main(String[] args) throws Exception {//警告信息集合List<String> warnings = new ArrayList<String>();//讀取生成器的配置文件InputStream resourceAsStream = Generator.class.getResourceAsStream("/mybatis-generator.xml");//創建配置解析器ConfigurationParser configurationParser = new ConfigurationParser(warnings);//解析配置文件Configuration configuration = configurationParser.parseConfiguration(resourceAsStream);resourceAsStream.close();//true時,如果有相同的文件則覆蓋文件DefaultShellCallback defaultShellCallback = new DefaultShellCallback(true);//創建生成器對象MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, defaultShellCallback, warnings);//執行生成代碼myBatisGenerator.generate(null);//輸出警告信息for (String warning : warnings) {System.out.println(warning);}} }

源代碼托管在GitHub

轉載于:https://www.cnblogs.com/loveyous/p/10291457.html

總結

以上是生活随笔為你收集整理的Mybatis Generator的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。