SpringBoot(六)mybatis基本语法
今天开始正式编写博客的功能。需要使用到数据库的交互(增删改查等)
之前在做测试的时候没有正经写过myBatis的查询语句,我懒,基本上都是调用IDEA生成的文件中的方法,有返回值证明数据库联通了就完事了。
我在刚才使用IDEA生成了我数据库中几张表的方法,我看了一下,能用的方法好像不是很多。众所周知,我是一个干PHP的,在使用PHP的框架的时候,我就很不喜欢使用框架中自带的数据库查询寻方法。我都是清一色的原生sql来操作数据库。
再加上我第一次使用myBatis,我这里决定我使用到的sql全部手写。
我这里特指SpringBoot框架中的mybatis框架使用。
一:mybatis的xml文件基本格式
我这里使用IDEA生成的mybatis生成的xml文件做示例:
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#61aeee"><?xml version=<span style="color:#98c379">"1.0"</span> encoding=<span style="color:#98c379">"UTF-8"</span>?></span>
<span style="color:#61aeee"><!DOCTYPE <span style="color:#f92672">mapper</span> <span style="color:#f92672">PUBLIC</span> <span style="color:#98c379">"-//mybatis.org//DTD Mapper 3.0//EN"</span> <span style="color:#98c379">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span>
<<span style="color:#e06c75">mapper</span> <span style="color:#d19a66">namespace</span>=<span style="color:#98c379">"com.springbootblog.dao.ArticleDao"</span>>
<<span style="color:#e06c75">resultMap</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"BaseResultMap"</span> <span style="color:#d19a66">type</span>=<span style="color:#98c379">"com.springbootblog.pojo.Article"</span>>
<<span style="color:#e06c75">id</span> <span style="color:#d19a66">column</span>=<span style="color:#98c379">"id"</span> <span style="color:#d19a66">jdbcType</span>=<span style="color:#98c379">"INTEGER"</span> <span style="color:#d19a66">property</span>=<span style="color:#98c379">"id"</span> />
<<span style="color:#e06c75">result</span> <span style="color:#d19a66">column</span>=<span style="color:#98c379">"artlogo"</span> <span style="color:#d19a66">jdbcType</span>=<span style="color:#98c379">"VARCHAR"</span> <span style="color:#d19a66">property</span>=<span style="color:#98c379">"artlogo"</span> />
<<span style="color:#e06c75">result</span> <span style="color:#d19a66">column</span>=<span style="color:#98c379">"another"</span> <span style="color:#d19a66">jdbcType</span>=<span style="color:#98c379">"VARCHAR"</span> <span style="color:#d19a66">property</span>=<span style="color:#98c379">"another"</span> />
</<span style="color:#e06c75">resultMap</span>>
<<span style="color:#e06c75">resultMap</span> <span style="color:#d19a66">extends</span>=<span style="color:#98c379">"BaseResultMap"</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"ResultMapWithBLOBs"</span> <span style="color:#d19a66">type</span>=<span style="color:#98c379">"com.springbootblog.pojo.Article"</span>>
<<span style="color:#e06c75">result</span> <span style="color:#d19a66">column</span>=<span style="color:#98c379">"artdesc"</span> <span style="color:#d19a66">jdbcType</span>=<span style="color:#98c379">"LONGVARCHAR"</span> <span style="color:#d19a66">property</span>=<span style="color:#98c379">"artdesc"</span> />
</<span style="color:#e06c75">resultMap</span>>
<<span style="color:#e06c75">sql</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"Blob_Column_List"</span>>
artdesc
</<span style="color:#e06c75">sql</span>>
<<span style="color:#e06c75">delete</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"deleteByExample"</span> <span style="color:#d19a66">parameterType</span>=<span style="color:#98c379">"com.springbootblog.pojo.ArticleExample"</span>>
delete from xxx.article
<<span style="color:#e06c75">if</span> <span style="color:#d19a66">test</span>=<span style="color:#98c379">"_parameter != null"</span>>
<<span style="color:#e06c75">include</span> <span style="color:#d19a66">refid</span>=<span style="color:#98c379">"Example_Where_Clause"</span> />
</<span style="color:#e06c75">if</span>>
</<span style="color:#e06c75">delete</span>>
<<span style="color:#e06c75">insert</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"insert"</span> <span style="color:#d19a66">parameterType</span>=<span style="color:#98c379">"com.springbootblog.pojo.Article"</span>>
<<span style="color:#e06c75">selectKey</span> <span style="color:#d19a66">keyProperty</span>=<span style="color:#98c379">"id"</span> <span style="color:#d19a66">order</span>=<span style="color:#98c379">"AFTER"</span> <span style="color:#d19a66">resultType</span>=<span style="color:#98c379">"java.lang.Integer"</span>>
MariaDB
</<span style="color:#e06c75">selectKey</span>>
insert into xxx.article (artlogo, another, is_top,
sort_no, `status`, putime,
uptime, arttitle, art_cid,
labelstr, click_num, resourse_id,
email, is_show, artdesc
)
values (#<span style="color:#d19a66">{artlogo,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{another,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{isTop,jdbcType=INTEGER}</span>,
#<span style="color:#d19a66">{sortNo,jdbcType=INTEGER}</span>, #<span style="color:#d19a66">{status,jdbcType=BIT}</span>, #<span style="color:#d19a66">{putime,jdbcType=VARCHAR}</span>,
#<span style="color:#d19a66">{uptime,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{arttitle,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{artCid,jdbcType=INTEGER}</span>,
#<span style="color:#d19a66">{labelstr,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{clickNum,jdbcType=INTEGER}</span>, #<span style="color:#d19a66">{resourseId,jdbcType=INTEGER}</span>,
#<span style="color:#d19a66">{email,jdbcType=VARCHAR}</span>, #<span style="color:#d19a66">{isShow,jdbcType=INTEGER}</span>, #<span style="color:#d19a66">{artdesc,jdbcType=LONGVARCHAR}</span>
)
</<span style="color:#e06c75">insert</span>>
<<span style="color:#e06c75">update</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"updateByPrimaryKey"</span> <span style="color:#d19a66">parameterType</span>=<span style="color:#98c379">"com.springbootblog.pojo.Article"</span>>
update xxx.article
set artlogo = #<span style="color:#d19a66">{artlogo,jdbcType=VARCHAR}</span>,
another = #<span style="color:#d19a66">{another,jdbcType=VARCHAR}</span>
where id = #<span style="color:#d19a66">{id,jdbcType=INTEGER}</span>
</<span style="color:#e06c75">update</span>>
<span style="color:#b18eb1"><em><!-- 下边是我自己编写的sql --></em></span>
<<span style="color:#e06c75">select</span> <span style="color:#d19a66">id</span>=<span style="color:#98c379">"selectBySearchAndPage"</span> <span style="color:#d19a66">resultType</span>=<span style="color:#98c379">"java.lang.Long"</span> >
SELECT count(1) as num FROM article p1
LEFT JOIN article_info p2 ON p1.id = p2.article_id
LEFT JOIN art_category p3 ON p1.art_cid = p3.id
<<span style="color:#e06c75">where</span>>
<<span style="color:#e06c75">if</span> <span style="color:#d19a66">test</span>=<span style="color:#98c379">"search != null <span style="color:#61aeee">&</span> search != ''"</span>>
p1.arttitle LIKE "%search%"
</<span style="color:#e06c75">if</span>>
</<span style="color:#e06c75">where</span>>
</<span style="color:#e06c75">select</span>>
</<span style="color:#e06c75">mapper</span>></span></span>
1:xml文件声明:
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#61aeee"><?xml version=<span style="color:#98c379">"1.0"</span> encoding=<span style="color:#98c379">"UTF-8"</span>?></span>
<span style="color:#61aeee"><!DOCTYPE <span style="color:#f92672">mapper</span> <span style="color:#f92672">PUBLIC</span> <span style="color:#98c379">"-//mybatis.org//DTD Mapper 3.0//EN"</span> <span style="color:#98c379">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span></span>
这个没什么可说的,按照这个写就可以了。
二:标签分类
l 定义SQL语句
原文地址:https://blog.csdn.net/qq_39708228/article/details/143677445
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!