1.创建springBoot项目,生成mybatis-generator.xml文件
1
2.mybatis-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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
" userId="root" password="123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 设置Java类生成的位置 -->
<javaModelGenerator targetPackage="com.q.v13.entity"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.q.v13.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.q.springboothello.mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_product_type"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="t_product"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="t_product_desc"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
3.在测试类中写main方法,完成生成entity和mapper文件.
public class MBG {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mybatis-generator.xml");
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);
}
}
4.注意四点:
1.java和source目录要清空
2.链接数据库依赖要和数据库版本统一
3.数据库时间格式
4.资源路径
二:以及xml文件中出现重复resultmap解决办法。
产生原因:链接到数据库的当前用户名下有多个相同的表名;它会扫描所有的数据库找到指定名称的tableName然后生成xml文件。
解决办法:百度很多方法说在指定表的时候加schema字段指定用户名。其实没有用,因为数据库连接的时候的用户名已经指定: <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&characterEncoding=UTF-" userId="root" password="123">
在这儿已经指定为root
有效方法:在mysql数据库新建用户test,只给需要操作的数据库的权限到当前用户,然后用新创建的用户链接数据库生成实体类<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&characterEncoding=UTF-" userId="test" password="test">