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 {
- /**
- * 查询单条数据,
- * 每一列的列名都会去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();
- }
测试类:
- 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">
- <!-- 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);
- }
- }