Mybatis查询语句返回对象和泛型集合
EmpMapper映射接口:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package cn.et.mybatis.lesson03;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface EmpMapper {
/**
* 查询单条数据,
* 每一列的列名都会去Emp实体类中去匹配对应的属性
* 匹配时会把二边都转为小字母进行匹配
* 匹配成功就会调用Emp实体类中对象的set方法
*
* 如果列名和Emp的属性匹配不上,
* 1.为查询结果的列设置一个别名
* 2.将列名ename和属性ename1建立一个关系 单个属性建立关系
*
* column是不区分大小写的,property是区分大小写的
* @return
*/
@Results (
{
@Result (column= "ename" ,property= "ename1" ),
@Result (column= "empNo" ,property= "empNo1" ),
@Result (column= "sal" ,property= "sal1" ),
}
)
@Select ( "select * from emp where empno=#{0}" )
public Emp queryEmpByEmpNo(String empNo);
/**
* 查询出多条数据,每一条数据都是一个Emp对象
* 每一列的列名都会去Emp实体类中去匹配对应的属性
* 匹配时会把二边都转为小字母进行匹配
* 匹配成功就会调用Emp实体类中对象的set方法
* 如果没有一条数据匹配成功,则不会创建Emp对象
* @param empNo
* @return
*/
@Results (
{
@Result (column= "ename" ,property= "ename1" ),
@Result (column= "empNo" ,property= "empNo1" ),
@Result (column= "sal" ,property= "sal1" ),
}
)
@Select ( "select * from emp" )
public List<Emp> queryEmp();
}
|
测试类:
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
35
|
package cn.et.mybatis.lesson03;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
public class TestMybatis {
public static SqlSession getSession(){
String resource = "/cn/et/mybatis/lesson03/mybatis.xml" ;
InputStream inputStream = TestMybatis. class .getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//打开会话
SqlSession session = sqlSessionFactory.openSession();
return session;
}
public static void main(String[] args) {
SqlSession session = getSession();
EmpMapper emp = session.getMapper(EmpMapper. class );
Emp obj = emp.queryEmpByEmpNo( "8000" );
System.out.println(obj);
}
@Test
public void test(){
SqlSession session = getSession();
EmpMapper emp = session.getMapper(EmpMapper. class );
List<Emp> result = emp.queryEmp();
for (Emp emp2 : result) {
System.out.println(emp2);
}
}
}
|
xml映射-----------
dept_mapper.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? 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">
<!--
接口映射
namespace必需跟接口的全名一致
-->
< mapper namespace = "cn.et.mybatis.lesson03.resultEntityXml.DeptMapper" >
<!-- column是不区分大小写的,property是区分大小写的 -->
< resultMap type = "cn.et.mybatis.lesson03.resultEntityXml.Dept" id = "myDept" >
< result column = "deptno" property = "deptno1" />
< result column = "dname" property = "dname1" />
< result column = "loc" property = "loc1" />
</ resultMap >
< select id = "queryDept" resultMap = "myDept" >
select * from dept where deptno=#{0}
</ select >
</ mapper >
|
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package cn.et.mybatis.lesson03.resultEntityXml;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMybatis {
public static SqlSession getSession(){
String resource = "/cn/et/mybatis/lesson03/mybatis.xml" ;
InputStream inputStream = TestMybatis. class .getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//打开会话
SqlSession session = sqlSessionFactory.openSession();
return session;
}
public static void main(String[] args) {
SqlSession session = getSession();
DeptMapper dept = session.getMapper(DeptMapper. class );
Dept result = dept.queryDept( "10" );
System.out.println(result);
}
}
|
mybatis查询结果集有泛型属性时可能出现的问题
问题:
当接收结果为map或者对象的属性为泛型时:
1
2
3
4
5
|
@Data
public class GenericKeyValueVo<K,V> {
private K key;
private V value;
}
|
这时候如果直接将resultType指向对象全限定名称时,可能会出现问题。因为如果查询结果的某个字段大于1000会出现","如:1,000.56 。mybatis不会报错,因为这个对象的这个属性为泛型,可以接收。而当获取结果之后即使定义接收的变量类型为:
第二个属性也会存入String类型的值。后续再处理可能就会出现将string转为double数据类型转换错误。
解决方法:
定义一个resultMap,指明javaType
1
2
3
4
|
< resultMap id = "StrKeyDoubleValueMap" type = "com.meinergy.mkting.commons.entity.wholesale.vo.GenericKeyValueVo" >
< result column = "key" property = "key" javaType = "java.lang.String" />
< result column = "value" property = "value" javaType = "java.lang.Double" />
</ resultMap >
|
再用一个convert函数规范查询结果格式
1
|
convert(FORMAT(queryResult, decimal( 12 , 2 ))
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_38921377/article/details/73229895