Mybatis查询语句返回对象和泛型集合
来源:互联网 发布:新开淘宝店铺排名 编辑:程序博客网 时间:2024/06/06 10:04
EmpMapper映射接口:
- 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);
- }
- }
- }
xml映射-----------
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">
-
-
- <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);
-
- }
-
- }