最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis直接執(zhí)行SQL的工具SqlMapper

 更新時(shí)間:2018年12月25日 14:05:29   作者:isea533  
今天小編就為大家分享一篇關(guān)于MyBatis直接執(zhí)行SQL的工具SqlMapper,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

可能有些人也有過(guò)類似需求,一般都會(huì)選擇使用其他的方式如Spring-JDBC等方式解決。

能否通過(guò)MyBatis實(shí)現(xiàn)這樣的功能呢?

為了讓通用Mapper更徹底的支持多表操作以及更靈活的操作,在2.2.0版本增加了一個(gè)可以直接執(zhí)行SQL的新類SqlMapper。

我們來(lái)了解一下SqlMapper。

SqlMapper提供的方法

SqlMapper提供了以下這些公共方法:

  • Map<String,Object> selectOne(String sql)
  • Map<String,Object> selectOne(String sql, Object value)
  • <T> T selectOne(String sql, Class<T> resultType)
  • <T> T selectOne(String sql, Object value, Class<T> resultType)
  • List<Map<String,Object>> selectList(String sql)
  • List<Map<String,Object>> selectList(String sql, Object value)
  • <T> List<T> selectList(String sql, Class<T> resultType)
  • <T> List<T> selectList(String sql, Object value, Class<T> resultType)
  • int insert(String sql)
  • int insert(String sql, Object value)
  • int update(String sql)
  • int update(String sql, Object value)
  • int delete(String sql)
  • int delete(String sql, Object value)

一共14個(gè)方法,這些方法的命名和參數(shù)和SqlSession接口的很像,只是基本上第一個(gè)參數(shù)都成了sql。

其中Object value為入?yún)?,入?yún)⑿问胶蚐qlSession中的入?yún)⒁粯樱瑤в腥雲(yún)⒌姆椒?,在使用時(shí)sql可以包含#{param}或${param}形式的參數(shù),這些參數(shù)需要通過(guò)入?yún)?lái)傳值。需要的參數(shù)過(guò)多的時(shí)候,參數(shù)可以使用Map類型。另外這種情況下的sql還支持下面這種復(fù)雜形式:

String sql = "<script>select * from sys_user where 1=1" + 
    "<if test=\"usertype != null\">usertype = #{usertype}</if></script>";

這種情況用的比較少,不多說(shuō)。

不帶有Object value的所有方法,sql中如果有參數(shù)需要手動(dòng)拼接成一個(gè)可以直接執(zhí)行的sql語(yǔ)句。

在selectXXX方法中,使用Class<T> resultType可以指定返回類型,否則就是Map<String,Object>類型。

實(shí)例化SqlMapper

SqlMapper構(gòu)造參數(shù)public SqlMapper(SqlSession sqlSession),需要一個(gè)入?yún)qlSession sqlSession,在一般系統(tǒng)中,可以按照下面的方式獲?。?/p>

SqlSession sqlSession = (...);//通過(guò)某些方法獲取sqlSession
//創(chuàng)建sqlMapper
SqlMapper sqlMapper = new SqlMapper(sqlSession);

如果使用的Spring,那么可以按照下面的方式配置<bean>:

<bean id="sqlMapper" class="com.github.abel533.sql.SqlMapper" scope="prototype">
 <constructor-arg ref="sqlSession"/>
</bean>

在Service中使用的時(shí)候可以直接使用@Autowired注入。

簡(jiǎn)單例子

在src/test/java目錄的com.github.abel533.sql包中包含這些方法的測(cè)試。

下面挑幾個(gè)看看如何使用。

selectList

//查詢,返回List<Map>
List<Map<String, Object>> list = sqlMapper.selectList("select * from country where id < 11");
//查詢,返回指定的實(shí)體類
List<Country> countryList = sqlMapper.selectList("select * from country where id < 11", Country.class);
//查詢,帶參數(shù)
countryList = sqlMapper.selectList("select * from country where id < #{id}", 11, Country.class);
//復(fù)雜點(diǎn)的查詢,這里參數(shù)和上面不同的地方,在于傳入了一個(gè)對(duì)象
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);

注意

通過(guò)上面這些例子應(yīng)該能對(duì)此有個(gè)基本的了解,但是如果你使用參數(shù)方式,建議閱讀下面的文章:

深入了解MyBatis參數(shù)

實(shí)現(xiàn)原理

最初想要設(shè)計(jì)這個(gè)功能的時(shí)候,感覺(jué)會(huì)很復(fù)雜,想的也復(fù)雜,需要很多個(gè)類,因此當(dāng)時(shí)沒(méi)有實(shí)現(xiàn)。

突發(fā)奇想,設(shè)計(jì)了現(xiàn)在的這種方式。并且有種強(qiáng)烈的感覺(jué)就是幸好昨天沒(méi)有嘗試去實(shí)現(xiàn),因?yàn)樽蛱焱砩纤伎歼@個(gè)問(wèn)題的時(shí)候是晚上10點(diǎn)多,而今天晚上7點(diǎn)開始思考。我很慶幸在一個(gè)更清醒的狀態(tài)下去寫這段代碼。

下面簡(jiǎn)單說(shuō)思路和實(shí)現(xiàn)方式。

在寫MyBatis分頁(yè)插件的時(shí)候熟悉了MappedStatement類。

在寫通用Mapper的時(shí)候熟悉了xml轉(zhuǎn)SqlNode結(jié)構(gòu)。

如果我根據(jù)SQL動(dòng)態(tài)的創(chuàng)建一個(gè)MappedStatement,然后使用MappedStatement的id在sqlSession中執(zhí)行不就可以了嗎?

想到這一點(diǎn),一切就簡(jiǎn)單了。

看看下面select查詢創(chuàng)建MappedStatement的代碼:

/**
 * 創(chuàng)建一個(gè)查詢的MS
 * @param msId
 * @param sqlSource 執(zhí)行的sqlSource
 * @param resultType 返回的結(jié)果類型
 */
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);
}

代碼是不是很簡(jiǎn)單,這段代碼的關(guān)鍵是參數(shù)sqlSource,下面是創(chuàng)建SqlSource的方法,分為兩種。

一種是一個(gè)完整的sql,不需要參數(shù)的,可以直接執(zhí)行的:

StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);

其中configuration從sqlSession中獲取,sql就是用戶傳入到sql語(yǔ)句,是不是也很簡(jiǎn)單?

另一種是支持動(dòng)態(tài)sql的,支持參數(shù)的SqlSource:

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

是不是也很簡(jiǎn)單?這個(gè)方法其實(shí)可以兼容上面的StaticSqlSource,這里比上面多了一個(gè)parameterType,因?yàn)檫@兒是可以傳遞參數(shù)的,另外languageDriver是從configuration中獲取的。

是不是很簡(jiǎn)單?

我一開始也沒(méi)想到MyBatis直接執(zhí)行sql實(shí)現(xiàn)起來(lái)會(huì)這么的容易。

insert,delete,update方法的創(chuàng)建更容易,因?yàn)樗麄兊姆祷刂刀际莍nt,所以處理起來(lái)更簡(jiǎn)單,有興趣的可以查看SqlMapper的源碼。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論

湛江市| 榆林市| 高雄县| 兴山县| 通海县| 江都市| 大宁县| 衡水市| 新晃| 定襄县| 洛隆县| 黄骅市| 玛纳斯县| 大宁县| 河南省| 琼海市| 鹤岗市| 易门县| 马山县| 丘北县| 广宁县| 康马县| 环江| 宿州市| 晋城| 桦南县| 武乡县| 陆河县| 泽州县| 宁蒗| 郑州市| 固原市| 芒康县| 南华县| 阿拉善左旗| 青河县| 汽车| 伊金霍洛旗| 建德市| 东方市| 平江县|