關(guān)于MyBatis各種SQL操作詳解
一、查詢一個實(shí)體類對象
映射方法:User getUserById(@Param("id") int id);
映射文件:
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>二、查詢一個List集合
映射方法:List<User> getAllUser();
映射文件
<select id="getAllUser" resultType="User">
select * from t_user
</select>注意:當(dāng)查詢的數(shù)據(jù)為多條時(shí),不能使用實(shí)體類作為返回值,否則會拋出異常 TooManyResultsException;但是若查詢的數(shù)據(jù)只有一條,可以使用實(shí)體類或集合作為返回值
三、查詢單個數(shù)據(jù)
映射方法:int getCount();
映射文件:
<select id="getCount" resultType="java.lang.Integer">
select count(id) from t_user
</select>四、查詢一條數(shù)據(jù)及多條數(shù)據(jù)到map集合
查詢一條數(shù)據(jù)到map集合
映射方法:Map<String,Object> getUserToMap(@Param("id") int id);
映射文件:
<select id="getUserToMap" resultType="java.util.Map">
select * from t_user where id = #{id}
</select>注意:將一條數(shù)據(jù)查詢到map集合中時(shí),map的鍵是表中的字段名,map的值是表中的數(shù)據(jù)
查詢多條數(shù)據(jù)到map集合
方式一:
映射方法:List<Map<String,Object>> getAllUserToMap();
映射文件:
<select id="getAllUserToMap" resultType="java.util.Map">
select * from t_user
</select>方式二:
映射方法:
@MapKey("id") Map<String,Object> getAllUserToMap();
映射文件:
<select id="getAllUserToMap" resultType="java.util.Map">
select * from t_user
</select>注意:
- 方式一中每條查出來的數(shù)據(jù)都對應(yīng)一個Map集合,然后再利用List集合將這些Map集合 組織起來
- 方式二中每條查出來的數(shù)據(jù)都存放在一個Map集合中,但是這個Map集合的鍵由映射方 法上方的@MapKey注解指定,而Map集合的值又是另外一個Map集合,作為值的Map 集合中鍵對應(yīng)表中字段名,值對應(yīng)表中數(shù)據(jù)
五、模糊查詢
映射方法:List<User> getUserByLike(@Param("mohu") String mohu);
映射文件:
<select id="getUserByLike" resultType="User">
<!--方式1-->
select * from t_user where username like '%${mohu}%'
<!--方式2-->
select * from t_user where username like concat("%",#{mohu},"%")
<!--方式3-->
select * from t_user where username like "%"#{mohu}"%"
</select>注意:不能使用 like '%#{mohu}%' 的方式,因?yàn)?{}會被解析成?,這個問號會被當(dāng)成字符串的一 部分造成參數(shù)獲取失敗
六、批量刪除
映射方法:void deleteSomeUser(@Param("ids") String ids);
映射文件:
<delete id="deleteSomeUser">
delete from t_user where id in(${ids})
</delete>注意:這里獲取參數(shù)的方式是${},因?yàn)?{}會自動添加引號,如果使用#{}的方式會造成SQL語句解 析成 delete from t_user where id in('ids') 從而報(bào)錯
七、動態(tài)設(shè)置表名
映射方法:List<User> getUserList(@Param("table") String table);
映射文件:
<select id="getUserList" resultType="User">
select * from ${table}
</select>注意:這里使用${}是因?yàn)槭褂?{}時(shí)會自動添加引號,而表名不允許添加表名
八、執(zhí)行添加功能時(shí)獲取自增的主鍵
映射方法:void insertUser(User user);
映射文件:
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{username},#{password},#{age},#
{gender},#{email})
</insert>測試方法:
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SpecialSQLMapper mapper = sqlSession.getMapper(SpecialSQLMapper.class);
User user = new User(null,"老六","1234567",36,"男","laoliu@qq.com");
mapper.insertUser(user);
System.out.println(user);//在這一步中打印出的User對象中可以看到自增的id,如果配置文件中不使 用useGeneratedKeys和keyProperty,則id仍然是null
}注意:這里的useGeneratedKeys設(shè)置使用自增主鍵為true,keyProperty是將獲取的主鍵值賦給實(shí)體對象中的某個屬性。這樣,在添加這個實(shí)體對象后,自增的主鍵也能在實(shí)體對象中獲得,而不需要進(jìn)行查詢
到此這篇關(guān)于關(guān)于MyBatis各種SQL操作詳解的文章就介紹到這了,更多相關(guān)MyBatis的SQL操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc 和ajax異步交互完整實(shí)例代碼
本篇文章主要介紹了spring mvc 和ajax異步交互完整實(shí)例代碼,簡單的AJAX+SpringMVC的異步交互小例子,有興趣的可以了解一下。2017-02-02
Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作
這篇文章主要介紹了Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
springcloud feign docker上無法通訊的問題及解決
這篇文章主要介紹了springcloud feign docker上無法通訊的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull)
這篇文章主要介紹了Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull),數(shù)據(jù)庫的字段命名方式為使用下劃線連接,對應(yīng)的實(shí)體類應(yīng)該是駝峰命名方式,而我使用的是和數(shù)據(jù)庫同樣的命名方式,需要的朋友可以參考下2022-01-01

