MyBatis直接执行SQL的工具SqlMapper


当前第2页 返回上一页

selectList

//查询,返回List<Map>
List<Map<String, Object>> list = sqlMapper.selectList("select * from country where id < 11");
//查询,返回指定的实体类
List<Country> countryList = sqlMapper.selectList("select * from country where id < 11", Country.class);
//查询,带参数
countryList = sqlMapper.selectList("select * from country where id < #{id}", 11, Country.class);
//复杂点的查询,这里参数和上面不同的地方,在于传入了一个对象
Country country = new Country();
country.setId(11);
countryList = sqlMapper.selectList("<script>" +
    "select * from country " +
    "  <where>" +
    "    <if test=\"id != null\">" +
    "      id &lt; #{id}" +
    "    </if>" +
    "  </where>" +
    "</script>", country, Country.class);

selectOne

Map<String, Object> map = sqlMapper.selectOne("select * from country where id = 35");
map = sqlMapper.selectOne("select * from country where id = #{id}", 35);
Country country = sqlMapper.selectOne("select * from country where id = 35", Country.class);
country = sqlMapper.selectOne("select * from country where id = #{id}", 35, Country.class);

insert,update,delete

//insert
int result = sqlMapper.insert("insert into country values(1921,'天朝','TC')");
Country tc = new Country();
tc.setId(1921);
tc.setCountryname("天朝");
tc.setCountrycode("TC");
//注意这里的countrycode和countryname故意写反的
result = sqlMapper.insert("insert into country values(#{id},#{countrycode},#{countryname})"
             , tc);
//update
result = sqlMapper.update("update country set countryname = '天朝' where id = 35");
tc = new Country();
tc.setId(35);
tc.setCountryname("天朝");
int result = sqlMapper.update("update country set countryname = #{countryname}" + 
      " where id in(select id from country where countryname like 'A%')", tc);
//delete
result = sqlMapper.delete("delete from country where id = 35");
result = sqlMapper.delete("delete from country where id = #{id}", 35);

注意

通过上面这些例子应该能对此有个基本的了解,但是如果你使用参数方式,建议阅读下面的文章:

深入了解MyBatis参数

实现原理

最初想要设计这个功能的时候,感觉会很复杂,想的也复杂,需要很多个类,因此当时没有实现。

突发奇想,设计了现在的这种方式。并且有种强烈的感觉就是幸好昨天没有尝试去实现,因为昨天晚上思考这个问题的时候是晚上10点多,而今天晚上7点开始思考。我很庆幸在一个更清醒的状态下去写这段代码。

下面简单说思路和实现方式。

在写MyBatis分页插件的时候熟悉了MappedStatement类。

在写通用Mapper的时候熟悉了xml转SqlNode结构。

如果我根据SQL动态的创建一个MappedStatement,然后使用MappedStatement的id在sqlSession中执行不就可以了吗?

想到这一点,一切就简单了。

看看下面select查询创建MappedStatement的代码:

/**
 * 创建一个查询的MS
 * @param msId
 * @param sqlSource 执行的sqlSource
 * @param resultType 返回的结果类型
 */
private void newSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType) {
  MappedStatement ms = new MappedStatement.Builder(
      configuration, msId, sqlSource, SqlCommandType.SELECT)
    .resultMaps(new ArrayList<ResultMap>() {
      {
        add(new ResultMap.Builder(configuration,
            "defaultResultMap",
            resultType,
            new ArrayList<ResultMapping>(0)).build());
      }
    })
    .build();
  //缓存
  configuration.addMappedStatement(ms);
}

代码是不是很简单,这段代码的关键是参数sqlSource,下面是创建SqlSource的方法,分为两种。

一种是一个完整的sql,不需要参数的,可以直接执行的:

StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);

其中configuration从sqlSession中获取,sql就是用户传入到sql语句,是不是也很简单?

另一种是支持动态sql的,支持参数的SqlSource:

SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);

是不是也很简单?这个方法其实可以兼容上面的StaticSqlSource,这里比上面多了一个parameterType,因为这儿是可以传递参数的,另外languageDriver是从configuration中获取的。

是不是很简单?

我一开始也没想到MyBatis直接执行sql实现起来会这么的容易。

insert,delete,update方法的创建更容易,因为他们的返回值都是int,所以处理起来更简单,有兴趣的可以查看SqlMapper的源码。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接


标签:SQL

返回前面的内容

相关阅读 >>

sql查询数据过多内存溢出怎么办

mysql缓存的查询和清除命令使用详解

简述mysql explain 命令

sql server是免费的吗?

php实现单条sql执行多个数据的insert语句方法

利用sql server触发器实现表的历史修改痕迹记录

mysql执行事务的语法与流程详解

mysql常用查询命令操作大全

mysql的select 学习笔记

sql中 decode()函数简介

更多相关阅读请进入《sql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...