一,mybatis example 形式代码
编写 generator.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动包位置 --> <!-- <classPathEntry location="D:softwarelibmysql-connector-java-5.1.21.jar" /> --> <classPathEntry location="C:oracleproduct10.2.0db_1jdbclibojdbc14.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> --> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="sy.model" targetProject="D:studymybatissrc"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="sy.mapping" targetProject="D:studymybatissrc"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="sy.dao" targetProject="D:studymybatissrc"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="tbug" domainObjectName="Bug" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
配置好上述文件之后,打开CMD命令行 ,java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
链接: https://pan.baidu.com/s/1aXR9dd_DfHAHG-86ipCxrA 提取码: k7bp
记得这些文件需要放在没有中文的文件夹路径,否则会报错,上述的src文件夹,为generator.xml配置的src位置。这样子即可一键生成mapper,model,example的代码。
二,mybatisplus Wrapper condition形式代码
pom引入需要的包
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-support</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency>
package com.infore.saas.filepreview; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * 代码生成器 * ps:只需要配置前面的静态参数,即可生成代码 */ public class MpGenerator { /** * mysql IP */ private static String MYSQL_IP = "127.0.0.1"; /** * mysql 端口 */ private static String MYSQL_PORT = "3306"; /** * mysql 数据库名称 */ private static String MYSQL_DATABASE = "demo"; /** * mysql 表名称 */ private static String MYSQL_TABLE_NAME = "student"; /** * mysql 用户名 */ private static String MYSQL_USER_NAME = "root"; /** * mysql 密码 */ private static String MYSQL_USER_PASSWORD = "123456"; /** * 包名 */ private static String JAVA_PACKAGE_NAME = "com.lawliet.system"; /** * 生成文件地址 */ private static String FILE_ADDRESS = "D:\code"; /** * 作者名称 */ private static String AUTHOR_NAME = "Lawliet"; /** * 业务类是否以I开头 */ private static boolean JAVA_SERVICE_NAME_START_WITH_I = false; /** * 生成源代码方法 * * @param serviceNameStartWithI 业务类是否以I开头 * @param packageName 包名 * @param tableNames 表名 */ private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { GlobalConfig config = new GlobalConfig(); String dbUrl = String.format("jdbc:mysql://%s:%s/%s", MYSQL_IP, MYSQL_PORT, MYSQL_DATABASE); DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL) .setUrl(dbUrl) .setUsername(MYSQL_USER_NAME) .setPassword(MYSQL_USER_PASSWORD) .setDriverName("com.mysql.jdbc.Driver"); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig .setCapitalMode(true) .setEntityLombokModel(false) .setDbColumnUnderline(true) .setNaming(NamingStrategy.underline_to_camel) .setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组 config.setActiveRecord(false) .setAuthor(AUTHOR_NAME) .setOutputDir(FILE_ADDRESS) .setFileOverride(true) .setEnableCache(false)// XML 二级缓存 .setBaseResultMap(true)// XML ResultMap .setBaseColumnList(true)// XML columList ; if (!serviceNameStartWithI) { config.setServiceName("%sService"); } new AutoGenerator().setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo( new PackageConfig() .setParent(packageName) //.setController("controller") .setEntity("model") ).execute(); } /** * 完成上面参数配置即可执行该main方法生成源代码 */ public static void main(String[] args) { System.out.println("-- 服务启动,正在初始化...."); new MpGenerator().generateByTables(JAVA_SERVICE_NAME_START_WITH_I, JAVA_PACKAGE_NAME, MYSQL_TABLE_NAME); System.out.println("-- " + MYSQL_TABLE_NAME + "相关的mapper,model,service生成成功。"); } }
mybatisplus 更加简单,将上述代码进行main函数运行即可生成各个模块的代码。