目录
1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件
1.1.5、扫描配置:添加 @Mapper 注解 / 添加扫描注解
在 properties 标签中加入版本号.
<mybatis-generator-plugin-version>1.4.1mybatis-generator-plugin-version>
在 build => plugins 标签中加入如下配置
-
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>${mybatis-generator-plugin-version}version>
- <executions>
- <execution>
- <id>Generate MyBatis Artifactsid>
- <phase>deployphase>
- <goals>
- <goal>generategoal>
- goals>
- execution>
- executions>
-
- <configuration>
-
- <verbose>trueverbose>
-
- <overwrite>trueoverwrite>
-
- <configurationFile>
- src/main/resources/mybatis/generatorConfig.xml
- configurationFile>
- configuration>
- plugin>
上述配置中需要注意的是 “配置文件路径”,这个路径就是用来生成 实体类和映射文件 配置规则的位置.
这个文件就是用来描述生成规则的.
根据路径(src/main/resources/mybatis),在 mybatis 目录下创建 generatorConfig.xml 文件.
Ps:下述配置文件中需要修改的有 数据库连接、实体类和映射文件的路径、数据库表名
- "1.0" encoding="UTF-8"?>
- 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:\class\source\mysql-connector-java-5.1.49.jar"/>
-
- <context id="DB2Tables" targetRuntime="MyBatis3">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true"/>
- <property name="suppressDate" value="true"/>
- commentGenerator>
-
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
- characterEncoding=utf8&useSSL=false"
-
- userId="root"
-
- password="1111">
- jdbcConnection>
- <javaTypeResolver>
-
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
- <javaModelGenerator targetPackage="com.example.cyk.model"
-
- targetProject="src/main/java">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- javaModelGenerator>
-
- <sqlMapGenerator targetPackage="mapper"
-
- targetProject="src/main/resources">
- <property name="enableSubPackages" value="true"/>
- sqlMapGenerator>
-
- <javaClientGenerator type="XMLMAPPER"
-
- targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
- <property name="enableSubPackages" value="true"/>
- javaClientGenerator>
-
- <table tableName="j_article" domainObjectName="Article"
-
- enableSelectByExample="false"
-
- enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
-
- enableCountByExample="false"
-
- enableUpdateByExample="false">
-
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="j_article_reply" domainObjectName="ArticleReply"
-
- enableSelectByExample="false"
-
- enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
-
- enableCountByExample="false"
-
- enableUpdateByExample="false">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="j_board" domainObjectName="Board"
-
- enableSelectByExample="false" enableDeleteByExample="false"
-
- enableDeleteByPrimaryKey="false" enableCountByExample="false"
-
- enableUpdateByExample="false">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="j_message" domainObjectName="Message"
-
- enableSelectByExample="false"
-
- enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
-
- enableCountByExample="false"
-
- enableUpdateByExample="false">
- <property name="useActualColumnNames" value="true"/>
- table>
- <table tableName="j_user" domainObjectName="User"
-
- enableSelectByExample="false" enableDeleteByExample="false"
-
- enableDeleteByPrimaryKey="false" enableCountByExample="false"
-
- enableUpdateByExample="false">
- <property name="useActualColumnNames" value="true"/>
- table>
- context>
- generatorConfiguration>
注意:
驱动包路径是自己本地仓库的路径
但一定注意!! 需要在非中文的目录下,因此你可以把这个驱动包拷贝出来,放到一个非中文的目录中即可.

重新加载Maven项⽬,在Plugins节点下出现mybatis-generator,双击运⾏,在对应的目录下⽣成相应的类与映射⽂件:

接着你就可以看到对应的生成了

在生成的 xml 文件中,给每一个 insert 标签都添加以下属性:useGeneratedKeys="true" keyProperty="id"
-
-
-
- <insert id="insert" parameterType="com.example.cyk.model.User"
- useGeneratedKeys="true" keyProperty="id" >
Ps:这个选项也可以自动生成,但是不理想(有些问题)
有两种方式配置扫描 Mapper 接口.
1)给每个 mapper 包下的 mapper 接口都添加 @Mapper 注解.

2)给启动类上 或者 新建一个配置类(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.
在 yml 文件中配置
- mybatis:
- mapper-locations: classpath:mapper/**/*Mapper.xml
- @SpringBootTest
- public class TestMapper {
-
- @Autowired
- private UserMapper userMapper;
-
- @Test
- public void select() {
- User user = userMapper.selectByPrimaryKey(1L);
- System.out.println(user);
- }
-
- }

