介紹在MyBatis中如何操作sql文的常用基礎方法包括的INSERT、UPDATE、DELETE定義,MyBatis怎么使用if 判斷條件來生成需要的業務sql文,查詢條件中in like 使用技巧和注意事項,foreach 回圈的應用,
專案運行環境配置看Springboot + MyBatis入門培訓 1 專案運行環境配置
MyBatis 中的INSERT、UPDATE、DELETE操作
在我們使用到新增,修改,洗掉等業務的時候,需要用到資料庫中的SQL陳述句INSERT、UPDATE、DELETE來操作資料庫,這些陳述句需要寫在MyBatis 下sql文xml對應的xml元素屬性中,再有業務介面定義這幾個業務的方法裝入到 spring 容器中來,
UserSql.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.core.my.app.dao.UserDao">
<insert id="saveuser">
INSERT INTO user(id, user)
VALUES(#{id}, #{name})
</insert>
<update id="upuser">
UPDATE user
SET user=#{name}
where id=#{id}
</update>
<delete id="deuser">
delete from user
where id=#{id}
</delete>
</mapper>
spring UserDao容器類定
@Component
@Mapper
public interface UserDao {
//添加
int saveuser(Map map);
//修改
int upuser(Map map);
//洗掉
int deuser(Map map);
}
測驗定義MyBatisTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes=OnApp.class)
public class MyBatisTest {
@Autowired
UserDao userdao;
@Test
public void TestUserList(){
//定義sql文中的引數
Map map=new HashMap();
//定義user表id屬性
map.put("id",2);
//定義user表user屬性
map.put("name", "zhtbs");
//洗掉業務
userdao.deuser(map);
//添加
userdao.saveuser(map);
map.put("name", "zht114001");
//修改
userdao.upuser(map);
}
}
運行測驗類后后臺列印出來以下資訊內容
-----------洗掉完成-----------------
==> Preparing: delete from user where id=?
==> Parameters: 2(Integer)
<== Updates: 1
-----------新增完成-----------------
==> Preparing: INSERT INTO user(id, user) VALUES(?, ?)
==> Parameters: 2(Integer), zhtbs(String)
<== Updates: 1
-----------修改完成-----------------
==> Preparing: UPDATE user SET user=? where id=?
==> Parameters: zht114001(String), 2(Integer)
<== Updates: 1
MyBatis 查詢條件 元素
-
< if test=" 判斷條件 "> 在xml中的if元素寫判斷邏輯,
-
_parameter 查詢條件為空判斷默認判斷,
sql文xml中 通過 _parameter 屬性判斷查詢條件是否為空,如果不為空生成對應條件的查詢sql陳述句內容,
sql.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.core.my.app.dao.UserDao">
<select id="UserList" resultType="map">
select * from user
<where>
<if test="_parameter != null"> ID = #{id}</if>
</where>
</select>
</mapper>
測驗定義MyBatisTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes=OnApp.class)
public class MyBatisTest {
@Autowired
UserDao userdao;
@Test
public void TestUserList(){
Map map=new HashMap();
map.put("id",2);//查詢條件是2
List<Map> list=userdao.UserList(map);
for(Map map1:list){
System.out.println(map1);
}
}
}
運行結果
==> Preparing: select * from user WHERE id = ?
==> Parameters: 2(Integer)
<== Columns: id, user
<== Row: 2, zht114001
<== Total: 1
查詢 in like 與${引數}與#{引數}使用
在 sql文xml中 使用查詢 in like 查詢條件需要將引數設定為${引數}模式,這種模式是MyBatis 執行sql文的時候直接明文顯示,生成原始sql文執行通過jdbc執行,
如果使用#{引數},MyBatis 執行的sql文引數為?號,在jdbc方法中執行,
-
${引數} 動態sql文 通常用于動態sql文生成
-
sql文中有 like 關鍵字一定要使用${引數} 傳遞引數內容
sql.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.core.my.app.dao.UserDao">
<select id="UserList" resultType="map">
select * from user
<where>
<if test="_parameter != null">
id in (#{id}) and user like '%${name}%'
</if>
</where>
</select>
測驗定義MyBatisTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes=OnApp.class)
public class MyBatisTest {
@Autowired
UserDao userdao;
@Test
public void TestUserList(){
Map map=new HashMap();
map.put("id",2);//查詢條件是
map.put("name", "zht");//模糊查詢
List<Map> list=userdao.UserList(map);
for(Map map1:list){
System.out.println(map1);
}
}
}
運行結果
==> Preparing: select * from user
WHERE id in (?)
and user like '%zht%'
==> Parameters: 2(Integer)
<== Columns: id, user
<== Row: 2, zht
<== Total: 1
MyBatis 回圈遍歷 foreach
當像sql文xml中的需要使用集合(list)的時候,會用到MyBatis xml中的foreach標簽元素回圈遍歷集合中的資訊內容,
- foreach 集合回圈體
- collection 集合物件參考
- item 集合中元素的參考
- index 回圈次數
- open 回圈字串內容設定 例如 “(” separator="," close=")"> separator隔斷, close結束字符
在foreach 回圈體中 參考集合中的元素物件 #{item.id} 生成sql文字串,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.core.my.app.dao.UserDao">
<select id="UserForList" resultType="map">
select * from user
<where>
<if test="list != null and list.size() > 0">
ID IN
<foreach item="item"
index="index"
collection="list"
open="(" separator="," close=")">
#{item.id}
</foreach>
</if>
</where>
</select>
</mapper>
UserDao 集合查詢方法設定
@Component
@Mapper
public interface UserDao {
List<Map> UserForList(List list);
}
測驗定義MyBatisTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes=OnApp.class)
public class MyBatisTest {
@Autowired
UserDao userdao;
@Test
public void TestUserForList(){
Map map=new HashMap();
map.put("id",1);
Map map1=new HashMap();
map1.put("id",2);
List list=new ArrayList();
list.add(map);
list.add(map1);
userdao.UserForList(list);
}
}
將id資訊裝入map,再將這些map物件裝入到list集合中,用這個集合list作為查詢條件,生產sql文內容,
運行結果
==> Preparing: select * from user WHERE ID IN ( ? , ? )
==> Parameters: 1(Integer), 2(Integer)
<== Columns: id, user
<== Row: 1, username
<== Row: 2, zht
<== Total: 2
Sql文轉義字符
在sql文xml會有一些特殊字符需要在到轉義字符或者xml語法來轉義這些特殊字符
| 內容 | xml | 轉義 |
|---|---|---|
| 大于 | <![CDATA[ 條件 < #{引數}]]> | > |
| 小于 | <![CDATA[ 條件 > #{引數}]]> | < |
| 大于等于 | <![CDATA[ 條件 <= #{引數}]]> | >= |
| 小于等于 | <![CDATA[ 條件 >= #{引數}]]> | <= |
| 和 | <![CDATA[ & ]]> | & |
| 單引號 | <![CDATA[ ' ]]> | ' |
| 雙引號 | <![CDATA[ " ]]> | " |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289564.html
標籤:java
