Python 官方文档:入门教程 => 点击学习
mybatis查询语句返回对象和泛型集合 EmpMapper映射接口: package cn.et.mybatis.lesson03; import java.util.Lis
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 {
@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);
@Results(
{
@Result(column="ename",property="ename1"),
@Result(column="empNo",property="empNo1"),
@Result(column="sal",property="sal1"),
}
)
@Select("select * from emp")
public List<Emp> queryEmp();
}
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);
}
}
}
dept_mapper.xml:
<?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>
测试类:
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);
}
}
当接收结果为map或者对象的属性为泛型时:
@Data
public class GenericKeyValueVo<K,V> {
private K key;
private V value;
}
这时候如果直接将resultType指向对象全限定名称时,可能会出现问题。因为如果查询结果的某个字段大于1000会出现","如:1,000.56 。mybatis不会报错,因为这个对象的这个属性为泛型,可以接收。而当获取结果之后即使定义接收的变量类型为:
第二个属性也会存入String类型的值。后续再处理可能就会出现将string转为double数据类型转换错误。
定义一个resultMap,指明javaType
<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函数规范查询结果格式
convert(FORMAT(queryResult, decimal(12, 2))
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Mybatis查询语句返回对象和泛型集合的操作
本文链接: https://lsjlt.com/news/131501.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0