国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 詳解MyBatis的getMapper()接口、resultMap標簽、Alias別名、 盡量提取sql列、動態操作

詳解MyBatis的getMapper()接口、resultMap標簽、Alias別名、 盡量提取sql列、動態操作

2020-06-08 12:21葉子。 JAVA教程

這篇文章主要介紹了詳解MyBatis的getMapper()接口、resultMap標簽、Alias別名、 盡量提取sql列、動態操作的相關資料,需要的朋友可以參考下

一、getMapper()接口

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

     掛載一個沒有實現的方法,特殊之處,借樓任何方法,必須和小配置中id屬性是一致的

     通過代理:生成接口的實現類名稱,在MyBatis底層維護名稱$$Dept_abc,selectDeptByNo()

     相當于是一個強類型

Eg

  第一步:在cn.happy.dao中定義一個接口   

?
1
2
3
4
5
6
7
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接口

?
1
2
3
4
5
6
7
8
9
<?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");-------實體類.小配置里面的Id名稱============字符串

     2)IDeptDao mapper = session.getMapper(IDeptDao.class);相當于實現類,getMapper是一個強類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 01查看全部信息getMapper()接口類的方法名要和小配置的id一樣
@Test
public void testSelectAll() {
SqlSession session = factory.openSession();
//用的是弱類型========實體類.小配置里面的Id名稱============字符串
/*List<Dept> list = session.selectList("cn.happy.dao.IDeptDao.getAllDept");
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}*/
// 用getMapper方法HIbernate幫我們在內存中代理出一個接口的實現類======相當于強類型
//mapper是一個實現類對象
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> list = mapper.getAllDept();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}

  第四步:全文統一用一個大配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>
<!--映射文件:描述某個實體和數據庫表的對應關系 -->
<mappers>
<mapper resource="cn/resultMap/enetity/Emp.xml" />
</mappers>
</configuration>

二、resultMap標簽

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

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

  第一步:創建一個接口

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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"/>
<!-- 員工角度 多的一方,嵌入一的一方的各個屬性請使用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>

第三步:測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
//resultMap:實體的屬性名和表的字段名保證一致用resultMap
//如果報NullException查看小配置的映射關聯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標簽簡化代碼量在小配置里面寫

?
1
2
3
4
5
6
7
8
9
<!-- SQl標簽的使用 -->
<sql id="columns">
d.deptNo,d.deptName
</sql>
<!-- SQl標簽的使用 -->
<select id="getAllEmps" resultMap="empMap">
select e.*,<include refid="columns"/>from Emp e,Dept d
where e.deptNo=d.deptNo
</select>

四、Alias別名

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

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

五、動態操作

解析:用于實現動態SQL的元素主要有:

?
1
2
3
4
if
   choose(when,otherwise)
   where
   set

Eg  查看在北京城市的人員

  第一步:接口

?
1
2
3
4
5
6
7
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" ?>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!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"/>
<!-- 員工角度 多的一方,嵌入一的一方的各個屬性請使用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>
<!--查詢動態查詢 -->
<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>

第三步:測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//動態查詢
@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    修改部門信息

  第一步:接口

  第二步:小配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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>
<!--修改動態查詢 -->
<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>

  第三步:測試 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 動態修改
* */
@Test
public void testUpdate(){
SqlSession session=factory.openSession();
Dept dept=new Dept();
dept.setDeptName("財務部");
dept.setDeptNo(1);
int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);
session.commit();
System.out.println(count);
session.close();
}

以上所述是小編給大家介紹的詳解MyBatis的getMapper()接口、resultMap標簽、Alias別名、 盡量提取sql列、動態操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.cnblogs.com/yejiaojiao/archive/2016/08/29/5818002.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品小视频 | 亚洲理论电影 | 特黄特色大片在线观看视频网站 | 久久精品国产99 | 欧美中文一区二区三区 | 色135综合网 | 日韩天堂 | 一本一道久久a久久精品综合 | 成人精品电影 | 欧美视频精品 | 在线永久免费观看黄网站 | av在线免费观看网站 | 久久99精品久久久久久国产越南 | 亚洲欧美日韩精品久久奇米色影视 | 成人影院在线 | 国产高清精品一区二区三区 | 中文字幕在线观看视频一区 | 99pao成人国产永久免费视频 | 亚洲精品二区 | 国产成人精品一区二区三区四区 | 欧美午夜一区二区三区免费大片 | 亚洲一区中文字幕在线观看 | 日韩精品 | 成人午夜精品久久久久久久蜜臀 | 午夜精品一区二区三区在线视频 | 亚洲精彩视频在线 | 福利片一区二区 | 久久黄网站 | 亚洲第一视频 | 日韩在线一区二区三区免费视频 | 久久视频国产 | 一区二区日韩精品 | 成人中文字幕在线观看 | 在线观看日韩av | 91精品国产91久久久久久最新 | 成人av电影在线观看 | 国内精品久久久久久中文字幕 | 久久久久久久久久久免费视频 | 亚洲国产高清视频 | 黄色三级网站在线观看 | 午夜操操操 |