mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)
1. 下载逆向工程,配置Maven pom.xml 文件
SpringMVCBasic
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
1. 生成代码配置文件
在maven项目下的src/main/resources 目录下新建generatorConfig.xml和generator.properties配置文件
generator.properties
jdbc.driverLocation=C:\\Users\\Yvettee\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.18\\mysql-connector-java-5.1.18.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.userId=root
jdbc.password=root
generatorConfig.xml
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
targetProject="src/main/java">
targetProject="src/main/java">
targetProject="src/main/java" type="XMLMAPPER">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
2. 在Intellij IDEA添加一个“Run运行”选项
使用maven运行mybatis-generator-maven-plugin插件
添加插件1
新添加Maven运行选项
3. 在name和Commond line分别填上如上图所示,再点击Apply和OK。
image.png
4. 最后再运行generate
点击generate
5. 结果
结果