MyBatis 效率提升

一般开发的时候,使用 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗星涌动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值