zoukankan      html  css  js  c++  java
  • Mybatis3.0-[tp_28-29]-映射文件-resultMap_自定义结果集映射规则_及关联环境的搭建

    笔记要点
    出错分析与总结
    工程组织

    1.定义接口  EmployeeMapperPlus.java

    package com.dao;
    import com.bean.*;
    public interface EmployeeMapperPlus {
        public Employee getEmpById(Integer id);
    }

    2.定义映射文件    EmployeeMapperPlus.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">
    
    <mapper namespace="com.dao.EmployeeMapperPlus">
    
       <!--ResultMap ;自定义结果集映射规则;
            type: 自定义规则的Java类型;id: 唯一的标识,方便引用-->
        <resultMap id="MyEmp" type="com.bean.Employee">
            <!--指定主键列的封装规则,id定义主键,底层会有优化规则;
            column : 指定具体的那一列; property:指定的JavaBean对应的属性-->
            <id column="id" property="id"/>
            <!--定义普通列的封装规则-->
            <result column="last_name" property="lastName"/>
            <!--,其他不指定的列会自动封装;我们只要写ResultMap,就把剩下的映射全部都写上-->
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </resultMap>
    
        <!--public Employee getEmpById(Integer id);   注意进行更改为resultMap-->
        <select id="getEmpById" resultMap="MyEmp">
            select * from tbl_employee where id=#{id}
        </select>
        <!--场景1
    
        -->
    
    </mapper>

    (关闭了驼峰规则,验证自定义配置)


    3.编写测试代码  test_tp28

    public class test_tp28 {
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream=Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(inputStream);
        }
        @Test
        public void test06() throws Exception{
            //默认是不自动提交数据的,需要我们自己手动提交
            SqlSession openSession = getSqlSessionFactory().openSession();
            try {
                EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
                Employee empById = mapper.getEmpById(1);
                System.out.println(empById);
    
                //最后手动提交
                openSession.commit();
            }finally {
                openSession.close();
            }
        }
    
    }

    测试结果

    DEBUG 11-29 17:32:44,565 ==>  Preparing: select * from tbl_employee where id=?   (BaseJdbcLogger.java:145) 
    DEBUG 11-29 17:32:44,584 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
    DEBUG 11-29 17:32:44,597 <==      Total: 1  (BaseJdbcLogger.java:145) 
    Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'}
  • 相关阅读:
    使用JNI技术实现JAVA程序调用dll
    vim 技巧集
    Powershell: 计算机硬盘扩容
    jira:3、API 调用 (jira insight)
    Jira: 1、服务器搭建(测试环境)
    Powershell :AD 域操作之OU 导出、创建、 用户导出、创建、移动OU
    jira :数据库配置要求
    powershell:定期删除指定目录的文件
    Jira:2、创建API 访问Token
    证书:常见操作记录
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/10039929.html
Copyright © 2011-2022 走看看