1、当然是创建一个新生的springboot项目,步骤就不贴了......
2、引入下面这玩意儿,它是生成代码的插件
<!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>
3、resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件,在里面写相关配置,然后添加下面这个......
mybatis: # mapper映射xml文件的所在路径 mapper-locations: classpath:mapping/*.xml # 对应实体类的路径 type-aliases-package: com.wzp.wzx.* # 驼峰命名规范 configuration: map-underscore-to-camel-case: true
4、重点来了,就是下面这个东西,当然,什么创建数据库啊啥的我就不写了......
<?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="E:\dataSourceDriver\mysql-connector-java-5.1.6.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="root"></jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.wzp.wzx.admin.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="mapper" targetPackage="com.wzp.wzx.admin.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="Admin" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
注意:上面文件里面相应的位置根据自己的项目去更改,然后下面这玩意儿要下载,去网上搜一下下载下来
<classPathEntry location="E:\dataSourceDriver\mysql-connector-java-5.1.6.jar"/>
4.1、点击Edit Configurations
4.2、添加运行配置
4.3、点击运行
4.4、最后生成的文件目录
4.5、打开类SpringbootApplication.java,我们需要添加一个注解
@MapperScan("com.winter.mapper") //将项目中对应的mapper类的路径加进来就可以了
5、结语
测试我就没有贴出来了,可以自己写一个Controller测一下,文章中还有很多也没有贴,只是贴了稍微重要的,比如对于生成后的文件内容这些就没有贴了(毕竟每个人的数据库表不一样,内容就不一样的......)