1.pom.xml添加:
<dependencies>
<!-- mybatis的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
<!--配置mybatis-generator的插件-->
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2.resources文件下添加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>
<!--<!– 本地数据库驱动程序jar包的全路径 –>-->
<!--<classPathEntry location=""/>-->
<!--<context id="context" targetRuntime="MyBatis3">-->
<!--<commentGenerator>-->
<!--<property name="suppressAllComments" value="false"/>-->
<!--<property name="suppressDate" value="true"/>-->
<!--</commentGenerator>-->
<!--<!– 数据库的相关配置 –>-->
<!--<jdbcConnection driverClass="" connectionURL="" userId="" password=""/>-->
<!--<javaTypeResolver>-->
<!--<property name="forceBigDecimals" value="false"/>-->
<!--</javaTypeResolver>-->
<!--<!– 实体类生成的位置 –>-->
<!--<javaModelGenerator targetPackage="目标包" targetProject="目标项目classpath">-->
<!--<property name="enableSubPackages" value="false"/>-->
<!--<property name="trimStrings" value="true"/>-->
<!--</javaModelGenerator>-->
<!--<!– *Mapper.xml 文件的位置 –>-->
<!--<sqlMapGenerator targetPackage="目标包" targetProject="目标项目classpath">-->
<!--<property name="enableSubPackages" value="false"/>-->
<!--</sqlMapGenerator>-->
<!--<!– Mapper 接口文件的位置 –>-->
<!--<javaClientGenerator targetPackage="目标包" targetProject="目标项目classpath" type="XMLMAPPER">-->
<!--<property name="enableSubPackages" value="false"/>-->
<!--</javaClientGenerator>-->
<!--<!– 相关表的配置 –>-->
<!--<table tableName="表名" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--enableUpdateByExample="false"/>-->
<!--</context>-->
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@10.16.17.22:1521/wsbs" userId="hr" password="hr123"> </jdbcConnection> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/testdb" userId="root" password="root">
<!-- 阻止生成别的数据库的同名表,user表 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成POJO实体类的位置 -->
<javaModelGenerator targetPackage="com.songyi.demo.model" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
<!-- 是否添加构造函数 -->
<property name="constructorBased" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper.xml映射文件生成的位置,XMLMAPPER时必须有一个,其他时候不起作用 -->
<sqlMapGenerator targetPackage="com.songyi.demo.mapper" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀,是否生成多个xml,true的时候targetPackage="test.xml" -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<!-- ANNOTATEDMAPPER,XMLMAPPER,MIXEDMAPPER:XML -->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.songyi.demo.mapper" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定要生成的表名的列表 -->
<!--<table tableName="items"></table> -->
<table tableName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_hb" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_visit" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3.配置maven运行
cd ~/Workspaces/IdeaProjects/mybatis-generator-idea;//到项目路径下
mybatis-generator:generator -e -X
或者配置如下
4.insert后返回新增的id:
在insert方法上加注解:
@SelectKey(statement = “select last_insert_id()”, keyProperty = “id”, before = false, resultType = int.class)
插入user后直接使用user.getId()就可获取:
@Insert({
"insert into user_visit (id, uid, ",
"tag, add_time)",
"values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=VARCHAR}, ",
"#{tag,jdbcType=INTEGER}, #{addTime,jdbcType=TIMESTAMP})"
})
@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = int.class)
int insert(UserVisit record);
@InsertProvider(type=UserVisitSqlProvider.class, method="insertSelective")
@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = int.class)
int insertSelective(UserVisit record);
int id = record.getId();