企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
第一步:引入maven依赖包,包括mybatis相关依赖包和mysql驱动包 ~~~ <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> ~~~ 第二步: 配置mybatis.generator依赖,这里文件位置是src/main/resources/mybatis-generator/generatorConfig.xml ~~~ <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <!-- generator 工具配置文件的位置 --> <configurationFile>src/main/resources/mybatis-generator/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> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> </dependencies> </plugin> ~~~ 第三步:保证application.yml里面有数据库连接的配置。 ![](https://img.kancloud.cn/03/81/03815c53914a6e32254f194d2cfcea5a_1178x427.png) ~~~ logging: level: com.kimgao.bootlauch: debug ~~~ generatorConfiguration文件内容为,修改里面文件路径为自己项目路径,确保路径存在且正确 ~~~ <?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> <!-- <classPathEntry location="C:/Users/pc/.m2/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar"/> --> <context id="my" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 阻止生成自动注释 --> <commentGenerator> <property name="javaFileEncoding" value="UTF-8"/> <property name="suppressDate" value="false" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接地址账号密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/wyh" userId="root" password="root" /> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成Model类存放位置 --> <javaModelGenerator targetPackage="com.kimgao.bootlauch.generator" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件存放位置 --> <sqlMapGenerator targetPackage="com.kimgao.bootlauch.generator" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成Dao类存放位置 --> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.kimgao.bootlauch.generator" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置需要生成的表 --> <table tableName="article" domainObjectName="Article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <generatedKey column="id" sqlStatement="mysql" identity="true"/> </table> </context> </generatorConfiguration> ~~~ ![](https://img.kancloud.cn/95/07/9507c961f0fb33d466812d83291f6a01_1919x887.png) 执行完毕后会发现已经生成好对应的文件 第四步:配置mapper扫描路径 @MapperScan(basePackages = {"com.kimgao.bootlauch.generator"}) ~~~ @SpringBootApplication @MapperScan(basePackages = {"com.kimgao.bootlauch.generator"}) public class BootLauchApplication { public static void main(String[] args) { SpringApplication.run(BootLauchApplication.class, args); } } ~~~ 因为spring boot默认在resources下找mapper.xml文件,所以需要在pom.xml文件中配置使其失效并按照我们要求的路径寻找。否则会报错 ~~~ <resources> <resource> <directory>src/main/java</directory> <filtering>false</filtering> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> </resource> </resources> ~~~