SpringBoot二十九:Mybatis逆向工程

mac2024-05-28  41

pom文件引入以下依赖

<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies>

mybatis逆向工程插件

<build> <plugins> <!-- mybatis逆向工程插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </plugin> </plugins> </build>

generator的配置文件generatorConfig.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> <!--导入属性配置 --> <properties resource="datasource.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置 --> <classPathEntry location="${db.driverLocation}" /> <!-- <classPathEntry location="D:/Java/Maven/reponsitory/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar" /> --> <context id="default" targetRuntime="MyBatis3"> <!-- 一些工具 生成的实体类时的一些方法 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> --> <!--<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> --> <!-- optional,旨在创建class时,对注释进行控制 --> <commentGenerator> <!-- 插入一个日期字段 --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="${db.driverClassName}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 --> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="cn.com.javakf.po" targetProject="./src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false" /> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="true" /> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true" /> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false" /> </javaModelGenerator> <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> <sqlMapGenerator targetPackage="cn.com.javakf.mapper" targetProject="./src/main/java"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口dao生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.javakf.mapper" targetProject="./src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 个人觉得生成那么多的example类很繁琐,因此设置为false --> <table schema="mybatis" tableName="user" domainObjectName="user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <table schema="mybatis" tableName="orders" domainObjectName="orders" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> </context> </generatorConfiguration>

数据库配置信息datasource.properties

db.driverLocation=D:/Java/Maven/reponsitory/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/mybatis db.username=root db.password=123456

运行方式1:运行generator,自动生成代码

右击项目名:Run as–>maven build,在Goals:mybatis-generator:generate

运行方式2:main运行generator,自动生成代码

public class Generator { public void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 指定逆向工程配置文件 String genCfg = "/generatorConfig.xml"; File configFile = new File(Generator.class.getResource(genCfg).getFile()); 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); } public static void main(String[] args) throws Exception { try { Generator generator = new Generator(); generator.generator(); } catch (Exception e) { e.printStackTrace(); } } }

报错1:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决:修改datasource.properties

db.driverLocation=D:/Java/Maven/reponsitory/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar db.driverClassName=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC db.username=root db.password=123456

报错2:自动生成mybatis实体类时多出了***WithBLOBs 解决:修改generatorConfig.xml

<property name="nullCatalogMeansCurrent" value="true" />

代码托管:springboot_mybatis_generator

最新回复(0)