一般开发的时候,使用 Mybatis 时经常都要开发人员自己手动编写数据表实体,Mapper 文件及其对应的 xml 文件。
如果该数据库操作比较复杂,那么这么做倒也确实必要,但如果只是简单的 CRUD 操作,频繁的编写大致相同的 SQL 语句,既费时费力,又影响情绪,实为不妥。
那么,有没有这么一种工具可以帮我们完成这些重复性强的无意义劳动呢?
有的!Mybatis Generator 就是这么强大,只要你对配置文件稍作修改,数据表实体、Mapper 及对应的 xml 文件都会一键帮你生成。
1
导入依赖
首先,我们需要先导入相应的 POM 依赖配置:
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator.version}</version></dependency>
2
修改配置文件
在 generatorConfig.xml 中对 MybatisGenerator 相关信息进行配置:
<!--数据库链接地址账号密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/exam_out" userId="root" password="root"> </jdbcConnection> <!--生成pojo类存放位置 --> <javaModelGenerator targetPackage="com.how2java.tmall.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator><!--生成xml映射文件存放位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator><!--生成mapper类存放位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.how2java.tmall.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator><!--生成对应表及类名 --> <table tableName="category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true" /> <property name="useActualColumnNames" value="true" /> <generatedKey column="id" sqlStatement="JDBC" /> </table> <table>...</table>
编写一个主方法使 generatorConfig.xml 的配置信息生效:
InputStream is= MybatisGenerator.class.getClassLoader().getResource("generatorConfig.xml").openStream();ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(is);is.close();DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);
至此,所有的准备工作都已经完成了。效果(generator 生成):

3
CRUD 测试
增加用户
public void addUser(User user) { usermapper.insert(user);}
对应的 SQL 语句(两种插入语句:填写全部/部分信息):

根据 Id 查找用户
public User findById(int id) { return usermapper.selectByPrimaryKey(id); }
对应的 SQL 语句:

根据其他信息查找用户(自定义,可选择性强)
public List<User> findByName(String name) { UserExample userExample=new UserExample(); userExample.createCriteria().andNameEqualTo(name); List<User> users= usermapper.selectByExample(userExample); return users;} // 根据名字查找用户
public List<Subject> list() { SubjectExample subjectExample=new SubjectExample(); subjectExample.setOrderByClause("id ASC"); return subjectMapper.selectByExample(subjectExample);} // 输出全部数据
对应的 SQL 语句:

删除操作
public void deleteStaff(int id) { staffMapper.deleteByPrimaryKey(id); }
对应的 SQL 语句:

更新操作
public void updateStaff(Staff staff) { staffMapper.updateByPrimaryKeySelective(staff);}
对应的 SQL 语句(分两种:全部/部分信息更新):

源码链接:
https://github.com/anxinghei/Springboot_newman/tree/master/peopel
https://github.com/anxinghei/xianyu

开发中使用Mybatis进行简单CRUD操作时,手动编写数据表实体、Mapper文件及xml文件费时费力。Mybatis Generator可解决这一问题,只需修改配置文件,就能一键生成相关文件。本文介绍了导入依赖、修改配置文件的步骤,并进行了CRUD测试。

1756

被折叠的 条评论
为什么被折叠?



