亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于MyBatis各種SQL操作詳解

 更新時(shí)間:2023年05月17日 11:32:33   作者:夏志121  
這篇文章主要介紹了關(guān)于MyBatis各種SQL操作詳解,MyBatis 是一款優(yōu)秀的半自動(dòng)的ORM持久層框架,它支持自定義 SQL、存儲(chǔ)過程以及高級(jí)映射,需要的朋友可以參考下

一、查詢一個(gè)實(shí)體類對(duì)象

映射方法:User getUserById(@Param("id") int id);

映射文件:

<select id="getUserById" resultType="User">
    select * from t_user where id = #{id}
</select>

二、查詢一個(gè)List集合

映射方法:List<User> getAllUser();

映射文件

<select id="getAllUser" resultType="User">
    select * from t_user
</select>

注意:當(dāng)查詢的數(shù)據(jù)為多條時(shí),不能使用實(shí)體類作為返回值,否則會(huì)拋出異常 TooManyResultsException;但是若查詢的數(shù)據(jù)只有一條,可以使用實(shí)體類或集合作為返回值

三、查詢單個(gè)數(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ù)都對(duì)應(yīng)一個(gè)Map集合,然后再利用List集合將這些Map集合 組織起來
  • 方式二中每條查出來的數(shù)據(jù)都存放在一個(gè)Map集合中,但是這個(gè)Map集合的鍵由映射方 法上方的@MapKey注解指定,而Map集合的值又是另外一個(gè)Map集合,作為值的Map 集合中鍵對(duì)應(yīng)表中字段名,值對(duì)應(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)?{}會(huì)被解析成?,這個(gè)問號(hào)會(huì)被當(dāng)成字符串的一 部分造成參數(shù)獲取失敗

六、批量刪除

映射方法:void deleteSomeUser(@Param("ids") String ids);

映射文件:

<delete id="deleteSomeUser">
    delete from t_user where id in(${ids})
</delete>

注意:這里獲取參數(shù)的方式是${},因?yàn)?{}會(huì)自動(dòng)添加引號(hào),如果使用#{}的方式會(huì)造成SQL語句解 析成 delete from t_user where id in('ids') 從而報(bào)錯(cuò)

七、動(dòng)態(tài)設(shè)置表名

映射方法:List<User> getUserList(@Param("table") String table);

映射文件:

<select id="getUserList" resultType="User">
    select * from ${table}
</select>

注意:這里使用${}是因?yàn)槭褂?{}時(shí)會(huì)自動(dòng)添加引號(hào),而表名不允許添加表名

八、執(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>

測(cè)試方法:

@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對(duì)象中可以看到自增的id,如果配置文件中不使    用useGeneratedKeys和keyProperty,則id仍然是null
    }

注意:這里的useGeneratedKeys設(shè)置使用自增主鍵為true,keyProperty是將獲取的主鍵值賦給實(shí)體對(duì)象中的某個(gè)屬性。這樣,在添加這個(gè)實(shí)體對(duì)象后,自增的主鍵也能在實(shí)體對(duì)象中獲得,而不需要進(jìn)行查詢

到此這篇關(guān)于關(guān)于MyBatis各種SQL操作詳解的文章就介紹到這了,更多相關(guān)MyBatis的SQL操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論