mybatis代码生成器mybatis-generator的配置和使用
- 注:项目介绍
- 编译器:intellij idea
- 项 目:springboot项目
讲道理现在mybatis-plus出来了。省去了再写许多繁琐的xml文件。也大大简化了开发压力。类似于springdata-jpa(也挺好用的)。mybatis-plus也有相关的代码生成器。后面有时间博主再去踩一下回来再整理。
首先我们有一个建好的现成项目
可以看到什么都还没有加进去,那我们就从连接数据库到代码自动生成演示一下。
使用mybatis-generator
1、首先我们要添加一个配置文件,这个也是最关键的文件。
配置文件代码:mybatis-generator-cfg.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="application.yml"></properties> <classpathentry location="f:\apache-maven-3.5.4\localhouse\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/> <context id="test" targetruntime="mybatis3"> <plugin type="org.mybatis.generator.plugins.equalshashcodeplugin"></plugin> <plugin type="org.mybatis.generator.plugins.serializableplugin"></plugin> <plugin type="org.mybatis.generator.plugins.tostringplugin"></plugin> <commentgenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressdate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressallcomments" value="false" /> </commentgenerator> <!--数据库链接url,用户名、密码 --> <jdbcconnection driverclass="com.mysql.jdbc.driver" connectionurl="jdbc:mysql://localhost/demo" userid="root" password="123456"> </jdbcconnection> <javatyperesolver> <!-- this property is used to specify whether mybatis generator should force the use of java.math.bigdecimal for decimal and numeric fields, --> <property name="forcebigdecimals" value="false" /> </javatyperesolver> <!-- targetpakage是即将生成的目录,targetproject是对应的前缀目录。可根据自己需求生到对应目录。下次运行会直接默认覆盖原来位置的文件 --> <!-- 生成模型的包名和位置 映射实体类的位置 --> <javamodelgenerator targetpackage="com.sbproject.sb.common.model" targetproject="src/main/java"> <property name="enablesubpackages" value="true" /> <property name="trimstrings" value="true" /> </javamodelgenerator> <!-- 生成映射文件的包名和位置 mapper.xml --> <sqlmapgenerator targetpackage="com.sbproject.sb.common.dao.mapper" targetproject="src/main/java"> <property name="enablesubpackages" value="true" /> </sqlmapgenerator> <!-- 生成dao的包名和位置 mapper接口--> <javaclientgenerator type="xmlmapper" targetpackage="com.sbproject.sb.common.dao" targetproject="src/main/java"> <property name="enablesubpackages" value="true" /> </javaclientgenerator> <!-- 要生成哪些表 orders是我的表名,orders是生成的类名,比如我的映射类为order,映射接口ordermapper, 映射文件为ordermapper.xml,可以添加多个表,里面的几个配置大概意思就是是否允许生成example文件和支持selectbyexample。用过mybatis的应该知道selectbyexample,对于一些简单查询用这个还是比较方便的。哈哈、话有点多,记得删除 --> <table tablename="orders" domainobjectname="orders" enablecountbyexample="true" enableupdatebyexample="true" enabledeletebyexample="true" enableselectbyexample="true" selectbyexamplequeryid="true"></table> <!-- 要生成哪些表 --> <table tablename="products" domainobjectname="products" enablecountbyexample="true" enableupdatebyexample="true" enabledeletebyexample="true" enableselectbyexample="true" selectbyexamplequeryid="true"></table> </context> </generatorconfiguration>
2、其次就是pom里面添加配置,加载plugins里面!
注:红圈的路径对应的就是刚才添加的配置文件。
<plugin> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version>1.3.5</version> <configuration> <configurationfile>src/main/resources/mybatis-generator/mybatis-generator-cfg.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.5</version> </dependency> </dependencies> </plugin>
3、添加运行配置、运行文件(对了记得吧application.properties后缀改为yml。
不然会找不到yml。或者在之前的那个配置文件把yml改为properties,都可以。)
懒人专用:请copy
mybatis-generator:generate -e
4、选中刚才配置的直接运行就ok了。是不是很简单。
由于mybatis要写很多很多的xml文件、和sql语句。这个代码生成器会直接生成诸多常用的增删查改。
看到以下提示、说明你很幸运,直接成功了。你可真是太优秀了!博主开始用的时候可是遇到了太多的坑了。
我们再到刚才配置生成的路径下看看文件。已经帮我们直接生成了我们想要的基础文件。
随便例举一个mapper接口吧, 为了让大家更直观的看到、我把后面自动生成的注释删了。
注释的作用主要用于表格变动之类需要再次生成时识别是否为自动生成的代码(比如第一个countbyexample)。会默认覆盖自动生成的代码。
没有注释的会保留下来。:
public interface ordersmapper { /** * this method was generated by mybatis generator. * this method corresponds to the database table orders * * @mbg.generated */ long countbyexample(ordersexample example); int deletebyexample(ordersexample example); int deletebyprimarykey(string id); int insert(orders record); int insertselective(orders record); list<orders> selectbyexample(ordersexample example); orders selectbyprimarykey(string id); int updatebyexampleselective(@param("record") orders record, @param("example") ordersexample example); int updatebyexample(@param("record") orders record, @param("example") ordersexample example); int updatebyprimarykeyselective(orders record); int updatebyprimarykey(orders record); }
重点
以下提几点需要注意的问题。
1、注意mysql的版本问题,不能超过5 。
博主遇到过问题超过五就报错。太久了忘了记录了。跟我一样选一样默认依赖。
新建springboot项目后mysql-connector默认是8.几。
所以加一个版本号就行。
2、配置文件里面的连接依赖和项目配置的依赖路径一致。
不然也会报错。
可直接右键jar包->find in path -> copy路径到右边配置文件对应位置即可。
3、还是开始提的pom里面的generator.xml路径一定要配对。
最后就附上我的pom.xml文件吧
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.0.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.springbootmybatis</groupid> <artifactid>sbm</artifactid> <version>0.0.1-snapshot</version> <name>sbm</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-commons</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web-services</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.46</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-commons</artifactid> <version>2.1.5.release</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version>1.3.5</version> <configuration> <configurationfile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml</configurationfile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> </executions> <dependencies> <dependency> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-core</artifactid> <version>1.3.5</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
注:此博客基础博客。博主比较菜。常用的是ssm,这个是博主搭建springboot测试项目的时候记录的。可能不是很规范,望见谅。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论