我是用的maven,所以是以插件开发
- 导入插件
- 编写generatorConfig.xml文件
- 执行插件
- 查看结果
- 编写测试
将mapper,xml文件修改文件java注解方式
导入插件
<build> <finalName>IDEAMaven</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <!--mybatis自动生成代码插件--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <!--数据库驱动--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql</artifactId> <version>5.1.7</version> </dependency> </dependencies> <configuration> <!--<configurationFile>${basedir}/src/main/resources/generator/</configurationFile>--> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
2. 编写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>
<context id="Db2Tables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<!-- <property name="suppressDate" value="true" /> 是否生成注释代时间戳 -->
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" password="123123" userId="root"></jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下 -->
<javaModelGenerator targetPackage="com.xpf.domain"
targetProject="./src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xpf.mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table tableName="user_info" domainObjectName="UserInfo" schema="test" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 忽略列,不生成bean 字段
<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
<!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名。false是 Camel Case风格-->
<property name="useActualColumnNames" value="false" />
</table>
<table tableName="sys_role" domainObjectName="SysRole" schema="test" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 忽略列,不生成bean 字段
<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
<!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名。false是 Camel Case风格-->
<property name="useActualColumnNames" value="false" />
</table>
<table tableName="sys_permission" domainObjectName="Permission" schema="test" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 忽略列,不生成bean 字段
<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
<!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名。false是 Camel Case风格-->
<property name="useActualColumnNames" value="false" />
</table>
<table tableName="sys_user_role" domainObjectName="UserRole" schema="test" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 忽略列,不生成bean 字段
<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
<!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名。false是 Camel Case风格-->
<property name="useActualColumnNames" value="false" />
</table>
<table tableName="sys_role_permission" domainObjectName="RolePermission" schema="test" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 忽略列,不生成bean 字段
<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
<!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名。false是 Camel Case风格-->
<property name="useActualColumnNames" value="false" />
</table>
</context>
</generatorConfiguration>
3. 执行插件
4. 查看结果
5. 编写测试(这个测试的时候有各种出错,所以请看下一步)
`/**
* spring的环境支持:
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Autowired
UserInfoMapper userInfoMapper;
@org.junit.Test
public void find(){
System.out.println(userInfoMapper.selectByPrimaryKey(Long.valueOf(1)));
}
}
`
6. 将mapper.xml文件修改文件java注解方式(将mapper.xml删除)
package com.xpf.mapper;
import com.xpf.domain.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface UserInfoMapper {
@Select("select * from user_info")
List<UserInfo> findList();
/* int deleteByPrimaryKey(Long uid);
int insert(UserInfo record);
int insertSelective(UserInfo record);
UserInfo selectByPrimaryKey(Long uid);
int updateByPrimaryKeySelective(UserInfo record);
int updateByPrimaryKey(UserInfo record);*/
}