mybatisgenerator使用_MyBatis Generator,帮你少写50%代码的自动化工具,你用过吗?
GitHub地址
https://github.com/erlieStar/mybatis-generator-demo
介紹
MyBatis Generator的作用就是根據數據庫中的表結構,幫我們自動生成和表結構相同的實體類,mapper接口,包含基本增刪改查語句的XML文件,我以一個例子演示如何優雅的使用MyBatis Generator,我會把例子放在GitHub上,所以不用擔心配置看不全的問題。
這個工具目前在很多公司都會使用,真的是超級方便
造數據,新建一個Spring Boot項目
建一個學生表,插入四條數據
CREATE TABLE `student` (`id` int(11) NOT NULL COMMENT '學生id',`name` varchar(255) NOT NULL COMMENT '學生姓名',`gender` tinyint(2) DEFAULT NULL COMMENT '學生性別,1男生,0女生',`create_time` datetime DEFAULT NULL COMMENT '創建時間',PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;insert into student (id, name, gender) values (1, '張三', 1); insert into student (id, name, gender) values (2, '李四', 1); insert into student (id, name, gender) values (3, '王五', 1); insert into student (id, name, gender) values (4, '趙六', 1);建一個Spring Boot項目
配置maven插件,增加配置文件,運行插件
配置maven插件
<build><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.7</version><configuration><verbose>true</verbose><overwrite>true</overwrite></configuration></plugin></plugins> </build>在resources目錄下放配置文件,我這里放了2個文件,datasource.properties和generatorConfig.xml,其中datasource.properties中放了一個數據庫的配置,如用戶名和密碼之類的,generatorConfig.xml引用datasource.properties其中的配置,剩下的文件夾和類都是配置好后,運行插件自動生成的
運行插件的方法如下,點擊紅框部分即可
MyBatis Generator的運行方法有很多種,maven插件的方法最方便,因此不再介紹其他方法
生成的Student類和數據庫中的字段一樣,而StudentExample類是為了方便增刪改查而生成的,我演示一下用法,其實很雞肋,一般不用這個
@Repository public interface StudentMapper {// 生成的一部分,沒有全部列出來long countByExample(StudentExample example);int deleteByExample(StudentExample example);int deleteByPrimaryKey(Integer id);int insert(Student record);int insertSelective(Student record); }演示
我建一個測試類,來演示用法,IDEA中按住Ctrl+Shift后再按T鍵,自動生成一個測試類
,選中要測試的方法即可自動生成測試類,我就演示一下countByExample這個方法吧
查詢id>=1并且id<4的學生的數量,測試通過
@RunWith(SpringRunner.class) @SpringBootTest public class StudentMapperTest {@AutowiredStudentMapper studentMapper;@Testpublic void countByExample() throws Exception {StudentExample example = new StudentExample();StudentExample.Criteria criteria = example.createCriteria();// id >= 1criteria.andIdGreaterThanOrEqualTo(1);// id < 4criteria.andIdLessThan(4);assertEquals(3, studentMapper.countByExample(example));} }可以看到SQL的邏輯用Java代碼表示了,不容易維護。所以一般是配置不生成Example類
在generatorConfig.xml配置文件中配置項設為false即可,完整版看GitHub,這里省略了一部分配置
<table tableName="student" enableCountByExample="false" enableUpdateByExample="false"> </table>這樣生成的Mapper文件中就只包含最基礎的增刪改查,沒有這些亂七八糟的Example了
下面就是修改后生成的全部內容,是不是看著清爽多了
public interface StudentMapper {int deleteByPrimaryKey(Integer id);int insert(Student record);int insertSelective(Student record);Student selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(Student record);int updateByPrimaryKey(Student record); }如何定義Java類型和數據庫類型的映射關系
還是上面的例子,我生成的Student對象如下
public class Student {private Integer id;private String name;private Byte gender;private LocalDateTime createTime; }數據庫中的datetime被映射為LocalDateTime 類型(用起來比較方便),這是我配置的原因。默認的是、datetime被映射為Date類型
在generatorConfig.xml有2種配置的方式
第一種,這個是全局的,針對所有表
<javaTypeResolver><property name="useJSR310Types" value="true"/> </javaTypeResolver>第二種,這個只針對表中某一列
<table tableName="student" domainObjectName="Student"><columnOverride column="create_time" javaType="java.time.LocalDateTime"/> </table>這兩種方式還可以定義其他類型的映射方式,我就不再介紹了。知道方法查詢具體配置就行
后記
話說剛開始用MyBatis Generator的坑還是挺多的,要不就是在低版本的插件中,這個配置不起作用
<javaTypeResolver><property name="useJSR310Types" value="true"/> </javaTypeResolver>要不就是在最新的插件中,重新生成時,雖然配置了每次覆蓋XML文件,但是不會起作用(這個一般都會配置,假如是追加的方式,會造成運行2次插件,一個接口在XML中對應2個語句,導致啟動錯誤),還得配置作者為解決這個問題專門寫的插件,為了大家少走彎路,我寫了一個demo,各種配置寫的很詳細。基本上改改配置就能用,坑我都替大家填了,文章開頭就是GitHub地址,可以去里面拷配置
總結
以上是生活随笔為你收集整理的mybatisgenerator使用_MyBatis Generator,帮你少写50%代码的自动化工具,你用过吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis 使用jdbc_MyBat
- 下一篇: matlab plot函数_慧图大讲堂