- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、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>
|
2、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();
}
|
第四步:在大配置引入小配置 。
3、提取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>
|
4、Alias别名 。
解析:在大配置上写,这样的话在小配置就可以引用别名了 。
1
2
3
4
|
<!-- Alias别名 小配置里面的type的属性值改成别名-->
<typeAliases>
<typeAlias type=
"cn.resultMap.enetity.Emp"
alias=
"emp"
/>
</typeAliases>
|
5、动态操作 。
解析:用于实现动态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 。
最后此篇关于详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作的文章就讲到这里了,如果你想了解更多关于详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在 csv 上使用 hadoop 来分析一些数据。我使用sql/mysql(不确定)来分析数据,现在陷入了僵局。 我花了好几个小时在谷歌上搜索,却没有找到任何相关的东西。我需要一个查询,在该查询中
我正在为 Bootstrap 网格布局的“简单”任务而苦苦挣扎。我希望在大视口(viewport)上有 4 列,然后在中型设备上有 2 列,最后在较小的设备上只有 1 列。 当我测试我的代码片段时,似
对于这个令人困惑的标题,我深表歉意,我想不出这个问题的正确措辞。相反,我只会给你背景信息和目标: 这是在一个表中,一个人可能有也可能没有多行数据,这些行可能包含相同的 activity_id 值,也可
具有 3 列的数据库表 - A int , B int , C int 我的问题是: 如何使用 Sequelize 结果找到 A > B + C const countTasks = await Ta
我在通过以下功能编写此查询时遇到问题: 首先按第 2 列 DESC 排序,然后从“不同的第 1 列”中选择 只有 Column1 是 DISTINCT 此查询没有帮助,因为它首先从第 1 列中进行选择
使用 Bootstrap 非常有趣和有帮助,目前我在创建以下需求时遇到问题。 “使用 bootstrap 在桌面上有 4 列,在平板电脑上有 2 列,在移动设备上有 1 列”谁能告诉我正确的结构 最佳
我是 R 新手,正在问一个非常基本的问题。当然,我在尝试从所提供的示例中获取指导的同时做了功课here和 here ,但无法在我的案例中实现这个想法,即可能是由于我的问题中的比较维度更大。 我的实
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个 df , delta1 delta2 0 -1 2 0 -1 0 0 0 我想知道如何分配 delt
您好,我想知道是否可以执行以下操作。显然,我已经尝试在 phpMyAdmin 中运行它,但出现错误。也许还有另一种方式来编写此查询。 SELECT * FROM eat_eat_restaurants
我有 2 个列表(标题和数据值)。我想要将数据值列 1 匹配并替换为头文件列 1,以获得与 dataValue 列 1 和标题值列 2 匹配的值 头文件 TotalLoad,M0001001 Hois
我有两个不同长度的文件,file2 是一个很大的引用文件,我从中提取文件 1 的数据。 我有一行 awk,我通常会对其进行调整以在我的文件中进行查找和替换,但它总是在同一列中进行查找和替换。 所以对于
假设我有两个表,如下所示。 create table contract( c_ID number(1) primary key, c_name varchar2(50) not
我有一个带有 varchar 列的 H2 表,其检查约束定义如下: CONSTRAINT my_constraint CHECK (varchar_field <> '') 以下插入语句失败,但当我删
这是最少量的代码,可以清楚地说明我的问题: One Two Three 前 2 个 div 应该是 2 个左列。第三个应该占据页面的其余部分。最后,我将添加选项来隐藏和
在 Azure 中的 Log Analytics 中,我为 VM Heartbeat 选择一个预定义查询,我在编辑器中运行查询正常,但当我去创建警报时,我不断收到警报“查询未返回 TimeGenera
在 Azure 中的 Log Analytics 中,我为 VM Heartbeat 选择一个预定义查询,我在编辑器中运行查询正常,但当我去创建警报时,我不断收到警报“查询未返回 TimeGenera
今天我开始使用 JexcelApi 并遇到了这个:当您尝试从特定位置获取元素时,不是像您通常期望的那样使用sheet.getCell(row,col),而是使用sheet.getCell(col,ro
我有一个包含 28 列的数据库。第一列是代码,第二列是名称,其余是值。 public void displayData() { con.Open(); MySqlDataAdapter
我很沮丧:每当我缩小这个网页时,一切都变得一团糟。我如何将网页居中,以便我可以缩小并且元素不会被错误定位。 (它应该是 2 列,但所有内容都合并为 1)我试过 但由于某种原因,这不起作用。 www.o
我是一名优秀的程序员,十分优秀!