1 准备逆向工程的项目
2 在pom文件中引入mbg的依赖+插件
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
</plugin>
3 在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>
<context id="MySQLTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- mvn mybatis-generator:generate 配置数据库位置 ,配置虚拟机上的mysql ip地址;不采用安全协议连接,否则无法逆向生成 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.1.1:3306/scw??useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaBean生成在哪里 -->
<javaModelGenerator
targetPackage="com.atguigu.springboot.ssm.bean"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMap sql映射文件(xml mapper文件) -->
<sqlMapGenerator targetPackage="mybatis.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClient:java接口生成的地方 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.springboot.ssm.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="" tableName="%"></table>
<!-- <table schema="TPermission" tableName="t_permission"></table> -->
</context>
</generatorConfiguration>
4 执行逆向工程
5. 在项目的主程序类名上添加MapperScan注解:扫描所有的Mapper接口
//指定mapper接口所在的包,主程序会自动将该包下的所有的接口当成组件和mapper映射文件对应创建对象
@MapperScan(basePackages = "com.atguigu.springboot.ssm.mapper")
@SpringBootApplication
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
网友评论