可能有些人也有过类似需求,一般都会选择使用其他的方式如Spring-JDBC等方式解决。
能否通过MyBatis实现这样的功能呢?
为了让通用Mapper更彻底的支持多表操作以及更灵活的操作,在2.2.0版本增加了一个可以直接执行SQL的新类SqlMapper
。
通过这篇博客,我们来了解一下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个方法,这些方法的命名和参数和SqlSession
接口的很像,只是基本上第一个参数都成了sql。
其中Object value
为入参,入参形式和SqlSession
中的入参一样,带有入参的方法,在使用时sql可以包含#{param}
或${param}
形式的参数,这些参数需要通过入参来传值。需要的参数过多的时候,参数可以使用Map
类型。另外这种情况下的sql还支持下面这种复杂形式:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">String sql = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<script>select * from sys_user where 1=1"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<if test=\"usertype != null\">usertype = #{usertype}</if></script>"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
这种情况用的比较少,不多说。
不带有Object value
的所有方法,sql中如果有参数需要手动拼接成一个可以直接执行的sql语句。
在selectXXX
方法中,使用Class<T> resultType
可以指定返回类型,否则就是Map<String,Object>
类型。
SqlMapper
SqlMapper
构造参数public SqlMapper(SqlSession sqlSession)
,需要一个入参SqlSession sqlSession
,在一般系统中,可以按照下面的方式获取:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">SqlSession sqlSession = (...);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//通过某些方法获取sqlSession</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建sqlMapper</span> SqlMapper sqlMapper = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SqlMapper(sqlSession);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
如果使用的Spring,那么可以按照下面的方式配置<bean>
:
<code class="language-xml hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">bean</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"sqlMapper"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">class</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"com.github.abel533.sql.SqlMapper"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">scope</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"prototype"</span>></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">constructor-arg</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">ref</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"sqlSession"</span>/></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">bean</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
在Service中使用的时候可以直接使用@Autowired
注入。
在src/test/java
目录的com.github.abel533.sql
包中包含这些方法的测试。
下面挑几个看看如何使用。
selectList
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查询,返回List<Map></span> List<Map<String, Object>> list = sqlMapper.selectList(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id < 11"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查询,返回指定的实体类</span> List<Country> countryList = sqlMapper.selectList(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id < 11"</span>, Country.class); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查询,带参数</span> countryList = sqlMapper.selectList(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id < #{id}"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>, Country.class); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//复杂点的查询,这里参数和上面不同的地方,在于传入了一个对象</span> Country country = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Country(); country.setId(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>); countryList = sqlMapper.selectList(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<script>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country "</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" <where>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" <if test=\"id != null\">"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" id < #{id}"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" </if>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" </where>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</script>"</span>, country, Country.class);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
selectOne
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">Map<String, Object> map = sqlMapper.selectOne(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id = 35"</span>); map = sqlMapper.selectOne(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id = #{id}"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); Country country = sqlMapper.selectOne(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id = 35"</span>, Country.class); country = sqlMapper.selectOne(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select * from country where id = #{id}"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>, Country.class);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
insert,update,delete
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//insert</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result = sqlMapper.insert(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"insert into country values(1921,'天朝','TC')"</span>); Country tc = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Country(); tc.setId(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1921</span>); tc.setCountryname(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"天朝"</span>); tc.setCountrycode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TC"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//注意这里的countrycode和countryname故意写反的</span> result = sqlMapper.insert(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"insert into country values(#{id},#{countrycode},#{countryname})"</span> , tc); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//update</span> result = sqlMapper.update(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"update country set countryname = '天朝' where id = 35"</span>); tc = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Country(); tc.setId(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); tc.setCountryname(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"天朝"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result = sqlMapper.update(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"update country set countryname = #{countryname}"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" where id in(select id from country where countryname like 'A%')"</span>, tc); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//delete</span> result = sqlMapper.delete(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"delete from country where id = 35"</span>); result = sqlMapper.delete(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"delete from country where id = #{id}"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
通过上面这些例子应该能对此有个基本的了解,但是如果你使用参数方式,建议阅读下面的文章:
深入了解MyBatis参数
2015-03-09:最初想要设计这个功能的时候,感觉会很复杂,想的也复杂,需要很多个类,因此当时没有实现。
2015-03-10:突发奇想,设计了现在的这种方式。并且有种强烈的感觉就是幸好昨天没有尝试去实现,因为昨天晚上思考这个问题的时候是晚上10点多,而今天(10号)是晚上7点开始思考。我很庆幸在一个更清醒的状态下去写这段代码。
下面简单说思路和实现方式。
在写MyBatis分页插件的时候熟悉了MappedStatement
类。
在写通用Mapper的时候熟悉了xml
转SqlNode
结构。
如果我根据SQL动态的创建一个MappedStatement
,然后使用MappedStatement
的id
在sqlSession
中执行不就可以了吗?
想到这一点,一切就简单了。
看看下面select查询创建MappedStatement
的代码:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 创建一个查询的MS * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> msId *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> sqlSource 执行的sqlSource *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> resultType 返回的结果类型 */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">newSelectMappedStatement</span>(String msId, SqlSource sqlSource, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> Class<?> resultType) { MappedStatement ms = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MappedStatement.Builder( configuration, msId, sqlSource, SqlCommandType.SELECT) .resultMaps(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<ResultMap>() { { add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ResultMap.Builder(configuration, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"defaultResultMap"</span>, resultType, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<ResultMapping>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)).build()); } }) .build(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//缓存</span> configuration.addMappedStatement(ms); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>
代码是不是很简单,这段代码的关键是参数sqlSource
,下面是创建SqlSource
的方法,分为两种。
一种是一个完整的sql,不需要参数的,可以直接执行的:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">StaticSqlSource sqlSource = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StaticSqlSource(configuration, sql);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
其中configuration
从sqlSession
中获取,sql
就是用户传入到sql语句,是不是也很简单?
另一种是支持动态sql的,支持参数的SqlSource
:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
是不是也很简单?这个方法其实可以兼容上面的StaticSqlSource
,这里比上面多了一个parameterType
,因为这儿是可以传递参数的,另外languageDriver
是从configuration
中获取的。
是不是很简单?
我一开始也没想到MyBatis直接执行sql实现起来会这么的容易。
insert,delete,update
方法的创建更容易,因为他们的返回值都是int
,所以处理起来更简单,有兴趣的可以去通用Mapper下的包com.github.abel533.sql
中查看SqlMapper
的源码。