最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解MyBatis的getMapper()接口、resultMap標(biāo)簽、Alias別名、 盡量提取sql列、動(dòng)態(tài)操作

 更新時(shí)間:2016年08月30日 09:53:58   作者:葉子。  
這篇文章主要介紹了詳解MyBatis的getMapper()接口、resultMap標(biāo)簽、Alias別名、 盡量提取sql列、動(dòng)態(tài)操作的相關(guān)資料,需要的朋友可以參考下

一、getMapper()接口

  解析:getMapper()接口 IDept.class定義一個(gè)接口,

     掛載一個(gè)沒有實(shí)現(xiàn)的方法,特殊之處,借樓任何方法,必須和小配置中id屬性是一致的

     通過代理:生成接口的實(shí)現(xiàn)類名稱,在MyBatis底層維護(hù)名稱$$Dept_abc,selectDeptByNo()

     相當(dāng)于是一個(gè)強(qiáng)類型

Eg

  第一步:在cn.happy.dao中定義一個(gè)接口   

package cn.happy.dao;
import java.util.List;
import cn.happy.entity.Dept;
public interface IDeptDao {
//查看全部---------getAllDept要和小配置里面的id一樣
public List<Dept> getAllDept();
}

  第二步:IDept.xml配置小配置

  解析:select里面的Id屬性要和接口里面的接口方法名一樣;mapper的namespace屬性包名是cn.happy.dao.IDeptDao接口

<?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.happy.dao.IDeptDao">
<select id="getAllDept" resultType="cn.happy.entity.Dept">
select * from Dept 
</select>
</mapper>

  第三步:測試類

  解析:查看全部信息有兩種方法

     1)session.selectList("cn.happy.dao.IDeptDao.getAllDept");-------實(shí)體類.小配置里面的Id名稱============字符串

     2)IDeptDao mapper = session.getMapper(IDeptDao.class);相當(dāng)于實(shí)現(xiàn)類,getMapper是一個(gè)強(qiáng)類型

// 01查看全部信息getMapper()接口類的方法名要和小配置的id一樣
@Test
public void testSelectAll() {
SqlSession session = factory.openSession();
//用的是弱類型========實(shí)體類.小配置里面的Id名稱============字符串
/*List<Dept> list = session.selectList("cn.happy.dao.IDeptDao.getAllDept");
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}*/
// 用getMapper方法HIbernate幫我們在內(nèi)存中代理出一個(gè)接口的實(shí)現(xiàn)類======相當(dāng)于強(qiáng)類型
//mapper是一個(gè)實(shí)現(xiàn)類對象
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> list = mapper.getAllDept();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}

  第四步:全文統(tǒng)一用一個(gè)大配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Alias別名 小配置里面的type的屬性值改成別名-->
<typeAliases>
<typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="sa" />
<property name="password" value="1" />
</dataSource>
</environment>
</environments>
<!--映射文件:描述某個(gè)實(shí)體和數(shù)據(jù)庫表的對應(yīng)關(guān)系 -->
<mappers>
<mapper resource="cn/resultMap/enetity/Emp.xml" />
</mappers>
</configuration>

二、resultMap標(biāo)簽

    解析:使用的場景是當(dāng)實(shí)體類的屬性與數(shù)據(jù)庫不匹配的時(shí)候需要用到resultMap實(shí)體類和數(shù)據(jù)庫的屬性必須一致。(之前用的是實(shí)體類)

Eg檢索所有員工,以及隸屬部門

  第一步:創(chuàng)建一個(gè)接口

package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//檢索所有員工,以及隸屬部門
public List<Emp> getAllEmps();
}

   第二步:配置小配置里面的屬性

  解析: 員工角度 多的一方,嵌入一的一方的各個(gè)屬性請使用association 是關(guān)聯(lián)(如果去掉association的話就是基礎(chǔ)的resultMap)

<?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.resultMap.dao.IEmpDao">
<resultMap type="cn.resultMap.enetity.Emp" id="empMap">
<id property="empId" column="EMPID"/>
<result property="empName" column="EMPNAME"/>
<result property="empCity" column="EMPCITY"/>
<!-- 員工角度 多的一方,嵌入一的一方的各個(gè)屬性請使用association -->
<association property="dept" javaType="cn.resultMap.enetity.Dept">
<result property="deptName" column="DEPTNAME"/>
<result property="deptNo" column="DEPTNO"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="empMap">
select e.*,d.* from Emp e,Dept d
where e.deptNo=d.deptNo
</select>
</mapper>

第三步:測試類

//resultMap:實(shí)體的屬性名和表的字段名保證一致用resultMap
//如果報(bào)NullException查看小配置的映射關(guān)聯(lián)resultMap是否配置
@Test
public void testAllEmp(){
SqlSession session=factory.openSession();
IEmpDao mapper = session.getMapper(IEmpDao.class);
List<Emp> allEmps = mapper.getAllEmps();
for (Emp emp : allEmps) {
System.out.println(emp.getEmpName()+"\t隸屬部門"+emp.getDept().getDeptName());
}
session.close();
}

第四步:在大配置引入小配置

三、提取sql列

  解析:Sql標(biāo)簽簡化代碼量在小配置里面寫

<!-- SQl標(biāo)簽的使用 -->
<sql id="columns">
d.deptNo,d.deptName
</sql>
<!-- SQl標(biāo)簽的使用 -->
<select id="getAllEmps" resultMap="empMap">
select e.*,<include refid="columns"/>from Emp e,Dept d
where e.deptNo=d.deptNo
</select>

四、Alias別名

    解析:在大配置上寫,這樣的話在小配置就可以引用別名了  

<!-- Alias別名 小配置里面的type的屬性值改成別名-->
<typeAliases>
<typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/>
</typeAliases>

五、動(dòng)態(tài)操作

解析:用于實(shí)現(xiàn)動(dòng)態(tài)SQL的元素主要有:

 if
    choose(when,otherwise)
    where 
    set 

Eg  查看在北京城市的人員

  第一步:接口

package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//檢索所有員工,以及隸屬部門
public List<Emp> getAllEmps();
}

  第二步:小配<?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.resultMap.dao.IEmpDao">
<resultMap type="cn.resultMap.enetity.Emp" id="empMap">
<id property="empId" column="EMPID"/>
<result property="empName" column="EMPNAME"/>
<result property="empCity" column="EMPCITY"/>
<!-- 員工角度 多的一方,嵌入一的一方的各個(gè)屬性請使用association -->
<association property="dept" javaType="cn.resultMap.enetity.Dept">
<result property="deptName" column="DEPTNAME"/>
<result property="deptNo" column="DEPTNO"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="empMap">
select e.*,d.* from Emp e,Dept d
where e.deptNo=d.deptNo
</select>
<!--查詢動(dòng)態(tài)查詢 -->
<select id="testAllEmpBuSelect" parameterType="cn.resultMap.enetity.Emp" resultType="cn.resultMap.enetity.Emp">
select * from Emp
<where>
<if test="empId!=null">
and empId=#{empId}
</if>
<if test="empName!=null">
and empName=#{empName}
</if>
<if test="empCity!=null">
and empCity=#{empCity}
</if>
</where>
</select>
</mapper>

第三步:測試

//動(dòng)態(tài)查詢
@Test
public void testSelect(){
SqlSession session=factory.openSession();
Emp emp=new Emp();
//emp.setEmpName("331");
emp.setEmpCity("sh");
List<Emp> list = session.selectList("cn.resultMap.dao.IEmpDao.testAllEmpBuSelect",emp);
for (Emp emps : list) {
System.out.println(emps.getEmpName());
}
session.close();
}

第四步:在大配置引入小配置

Eg    修改部門信息

  第一步:接口

  第二步:小配置

<?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.resultMap.dao.IDeptDao">
<resultMap type="cn.happy.entity.Dept" id="deptResultMap">
<id property="deptNo" column="deptNo"/>
<result property="deptName" column="deptName"/>
</resultMap>
<select id="getAllDept" resultMap="deptResultMap">
select d.*,e.* from Dept d,Emp e
where d.deptNo=e.deptNo and d.deptNo=#{deptNo}
</select>
<!--修改動(dòng)態(tài)查詢 -->
<select id="testUpdate" parameterType="int" resultType="cn.resultMap.enetity.Dept">
update dept
<set>
<if test="deptNo!=null">
deptNo=#{deptNo},
</if>
<if test="deptName!=null">
deptName=#{deptName},
</if>
</set>
where deptNo=#{deptNo}
</select>
</mapper> 

  第三步:測試 

/**
* 動(dòng)態(tài)修改
* */
@Test
public void testUpdate(){
SqlSession session=factory.openSession();
Dept dept=new Dept();
dept.setDeptName("財(cái)務(wù)部");
dept.setDeptNo(1);
int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);
session.commit();
System.out.println(count);
session.close();
}

以上所述是小編給大家介紹的詳解MyBatis的getMapper()接口、resultMap標(biāo)簽、Alias別名、 盡量提取sql列、動(dòng)態(tài)操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringMVC中的ResourceUrlProviderExposingInterceptor詳解

    SpringMVC中的ResourceUrlProviderExposingInterceptor詳解

    這篇文章主要介紹了SpringMVC中的ResourceUrlProviderExposingInterceptor詳解,ResourceUrlProviderExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請求添加一個(gè)屬性,需要的朋友可以參考下
    2023-12-12
  • 基于Java SWFTools實(shí)現(xiàn)把pdf轉(zhuǎn)成swf

    基于Java SWFTools實(shí)現(xiàn)把pdf轉(zhuǎn)成swf

    這篇文章主要介紹了基于Java SWFTools實(shí)現(xiàn)把pdf轉(zhuǎn)成swf,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 通過Java實(shí)現(xiàn)對PDF頁面的詳細(xì)設(shè)置

    通過Java實(shí)現(xiàn)對PDF頁面的詳細(xì)設(shè)置

    這篇文章主要介紹了通過Java實(shí)現(xiàn)對PDF頁面的詳細(xì)設(shè)置,下面的示例將介紹通過Java編程來對PDF頁面進(jìn)行個(gè)性化設(shè)置的方法,包括設(shè)置頁面大小、頁邊距、紙張方向、頁面旋轉(zhuǎn)等,需要的朋友可以參考下
    2019-07-07
  • Java實(shí)現(xiàn)日志文件監(jiān)聽并讀取相關(guān)數(shù)據(jù)的方法實(shí)踐

    Java實(shí)現(xiàn)日志文件監(jiān)聽并讀取相關(guān)數(shù)據(jù)的方法實(shí)踐

    本文主要介紹了Java實(shí)現(xiàn)日志文件監(jiān)聽并讀取相關(guān)數(shù)據(jù)的方法實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 雙token實(shí)現(xiàn)token超時(shí)策略示例

    雙token實(shí)現(xiàn)token超時(shí)策略示例

    用于restful的app應(yīng)用無狀態(tài)無sesion登錄示例,需要的朋友可以參考下
    2014-02-02
  • SpringMVC+ZTree實(shí)現(xiàn)樹形菜單權(quán)限配置的方法

    SpringMVC+ZTree實(shí)現(xiàn)樹形菜單權(quán)限配置的方法

    本篇文章主要介紹了SpringMVC+ZTree實(shí)現(xiàn)樹形菜單權(quán)限配置的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Flutter實(shí)現(xiàn)文本組件、圖標(biāo)及按鈕組件的代碼

    Flutter實(shí)現(xiàn)文本組件、圖標(biāo)及按鈕組件的代碼

    這篇文章主要介紹了Flutter實(shí)現(xiàn)文本組件、圖標(biāo)及按鈕組件的代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • 從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程

    從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程

    這篇文章主要為大家介紹了從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)

    SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)

    在目前的企業(yè)級應(yīng)用開發(fā)中,前后端分離是趨勢,但是視圖層技術(shù)還占有一席之地。Spring Boot 對視圖層技術(shù)提供了很好的支持,福安防推薦使用的模板引擎是Thymeleaf,不過想FreeMarker也支持,JSP技術(shù)在這里并不推薦使用
    2022-08-08
  • JAVACORE與HEAPDUMP生成方法

    JAVACORE與HEAPDUMP生成方法

    JavaCore文件主要保存的是Java應(yīng)用各線程在某一時(shí)刻的運(yùn)行的位置,即JVM執(zhí)行到哪一個(gè)類、哪一個(gè)方法、哪一個(gè)行上,它是一個(gè)文本文件,打開后可以看到每一個(gè)線程的執(zhí)行棧,以stack?trace的顯示,本文介紹JAVACORE與HEAPDUMP生成大法,感興趣的朋友跟隨小編一起看看吧
    2024-08-08

最新評論

绥芬河市| 河东区| 星子县| 思茅市| 新竹市| 新邵县| 秦皇岛市| 神木县| 宝山区| 任丘市| 长宁区| 曲阳县| 赤峰市| 壶关县| 五原县| 夏津县| 温州市| 二连浩特市| 高密市| 建平县| 新余市| 禹城市| 屏东县| 公安县| 永新县| 中西区| 天峻县| 陈巴尔虎旗| 无极县| 遂川县| 太湖县| 阿勒泰市| 抚松县| 剑阁县| 高雄县| 邢台县| 京山县| 普安县| 静宁县| 浪卡子县| 迁安市|