学习总结
一、引入mybatis-generator插件依赖
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许自动覆盖-->
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
二、编写mybatis-generator.xml配置文件
<!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="simple" targetRuntime="MyBatis3Simple">
<!--1、连接池的配置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/miaosha" userId="root" password="abraham12580"/>
<!--2、生成dataobject类的位置-->
<javaModelGenerator targetPackage="com.miaoshaProject.dataobject" targetProject="src/main/java"/>
<!--3、生成与数据库映射操作配置文件即mapper的xml文件的存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"/>
<!-- 4、生成dao类即dataObjectMapper的存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaProject.dao" targetProject="src/main/java"/>
<!--5、与mysql中的表相对应的java数据结构的类名-->
<table tableName="user_info" domainObjectName="UserDO"></table>
<table tableName="user_password" domainObjectName="UserPasswordDO"></table>
</context>
</generatorConfiguration>
三、执行mybatis-generator插件
- 新增configuration
run→edit configuration→+→Maven→Command line中填入mybatis-generator:generate→apply→OK
- run 新建的configuration
将当前run切换到刚刚创建的configuration,并运行。
- 结果
生成了与mysql交互的dao层接口(2个),与mysql中表对应的java数据结构(即dataObject)(2个),定义dao层接口方法的具体实现的xml配置文件(2个)