学习了b站一位小哥的mybatis逆向工程,这里写一个笔记,视频地址:https://www.bilibili.com/video/BV16E411q7aP?from=search&seid=2547198468275404373
mybatis逆向工程
mybatis逆向工程官网地址:http://mybatis.org/generator/index.html
1、创建一个普通的maven项目,导入pom依赖
<dependencies> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--数据库连接池 驱动--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <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> <build> <!--逆向工程的maven插件--> <!--注意这个plugin的位置,是在build下的第一级子标签--> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--指定配置文件的名字位置--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> </dependencies> </plugin> </plugins> <finalName>ssm-crud</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_fo <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build>
2、编写dao层跟ben的包
3、编写properties配置文件:/resources/dbconfig.properties
jdbc.jdbcUrl=jdbc:mysql://182.92.209.212:3306/mybatis_plus?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=密码 # 通用Mapper固定配置(不需要修改) mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin mapper.Mapper=tk.mybatis.mapper.common.Mapper mapper.forceAnnotation=true # 生成文件保存位置(修改自己的包名) targetModelPackage=com.zhixi.bean targetXMLPackage=com.zhixi.dao targetMapperPackage=com.zhixi.dao targetJavaProject=src/main/java targetResourcesProject=src/main/java
4、编写逆向工程配置文件:/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> <properties resource="dbconfig.properties"/> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!--支持序列化--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!-- <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> <property name="forceAnnotation" value="${mapper.forceAnnotation}" /> </plugin> --> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.jdbcUrl}" userId="${jdbc.user}" password="${jdbc.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--全部表参与逆向工程--> <!--以下example为false,表示不会生成example类,否则将自动生成example类--> <table schema="" tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!--指定某些表参与逆向工程--> <!-- <table tableName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> --> </context> </generatorConfiguration>
5、编写mybatis的配置文件:/resources/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <!--包名需要自己改--> <package name="com.zhixi"/> </typeAliases> </configuration>
6、生成代码