前言
今天将表建好了,但是一个一个的建实体类、Mapper接口、Mapper.xml文件就十分的麻烦,所以我就想到了MyBatis逆向,今天就操作一把!这里我们采用maven来进行操作。
一、新建generatorConfig.xml文件
首先建好表,在你的项目的resource文件中新建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>
<!--这里是找到你本地mysql链接驱动,但是8.0以上的会报错,所以我用的5.1.13,在D盘新建了一个文件夹-->
<classPathEntry location="D:\mybatisGenerator/mysql-connector-java-5.1.13-bin.jar"/>
<!--id可以随意,但targetRuntime必须MyBatis3-->
<context id="DB2Tables1" targetRuntime="MyBatis3">
<!-- 配置pojo的序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库的链接 改成你的 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/xxxx"
userId="xxxxx" password="xxxxx"/>
<javaTypeResolver type="">
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体 -->
<javaModelGenerator targetPackage="com.mangocms.mango.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- .xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSuzbPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mangocms.mango.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 对应的表名称 和转化的实体名称 -->
<table tableName="sys_user" domainObjectName="User"
enableSelectByExample="false"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
注意:
1、实体、mapper、.xml三者的targetPackage不需要在项目中建立对应的包,只需要指定要叫什么名字就可以了,这样会自动新建包。
2、.xml文件我们建立在targetProject="src/main/resources"这个路径下,因为SpringBoot会默认扫描resource文件下名为mapper文件夹中的mapper.xml文件
3、后面的表对应每次只能执行一张表
二、pom.xml配置
看看项目pom.xml里有没有如下配置:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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.7</version>
</dependency>
主要的来了!!!
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
这个插件加上之后,maven那里就会多了一个如图所示的插件:
每次点击这个插件就可以执行generatorConfig.xml了,自动生成mapper、pojo、.xml文件,但是生成之后要把它从项目里去取消,因为我试了他在里面会报错,也不知道是不是我项目的原因,我生成完了就取消了,会报下面的错,不知道为什么!
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\SpringBootCode\mango\target\classes\com\mangocms\mango\dao\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\SpringBootCode\mango\target\classes\generatorConfig.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.NullPointerException
标签:xml,exception,generator,生成器,springframework,nested,mybatis,org From: https://blog.51cto.com/u_12866610/8751468