zoukankan      html  css  js  c++  java
  • MyBatis探究-----为实体类Bean取别名,配置typeAliases

       1.单个实体类设置别名

    1.1 不使用alias

    <typeAliases>
            <!-- typeAlias:为某个java类型起别名 ; type:指定要起别名的类型全类名; 默认别名就是类名小写(大小写都可以) -->
            <typeAlias type="com.mybatis.entity.Employee" />
        </typeAliases>
    <?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.mybatis.dao.EmployeeMapper">
        <insert id="addEmployee" parameterType="Employee">
            insert into
            t_employee(empId,empName,empSex,empAge)
            values(#{empId},#{empName},#{empSex},#{empAge})
        </insert>
    </mapper>

    1.2 使用alias

    <typeAliases>
            <!-- alias:指定新的别名-->
            <typeAlias type="com.mybatis.entity.Employee" alias="emp" />
        </typeAliases>
    <?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.mybatis.dao.EmployeeMapper">
        <insert id="addEmployee" parameterType="emp">
            insert into
            t_employee(empId,empName,empSex,empAge)
            values(#{empId},#{empName},#{empSex},#{empAge})
        </insert>
    </mapper>

       2.多个实体类设置别名

    2.1 不使用注解@Alias

        <typeAliases>
            <!--package:为某个包下的所有类批量起别名 name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写),) -->
            <package name="com.mybatis.entity" />
        </typeAliases>
    <?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.mybatis.dao.EmployeeMapper">
        <insert id="addEmployee" parameterType="Employee">
            insert into
            t_employee(empId,empName,empSex,empAge)
            values(#{empId},#{empName},#{empSex},#{empAge})
        </insert>
    </mapper>

    2.2 使用注解@Alias

    <typeAliases>
            <!--package:为某个包下的所有类批量起别名 name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写),) 
                 批量起别名的情况下,使用@Alias注解为某个类型指定新的别名-->
            <package name="com.mybatis.entity" />
        </typeAliases>
    <?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.mybatis.dao.EmployeeMapper">
        <insert id="addEmployee" parameterType="emp">
            insert into
            t_employee(empId,empName,empSex,empAge)
            values(#{empId},#{empName},#{empSex},#{empAge})
        </insert>
    </mapper>
    package com.mybatis.entity;
    
    import org.apache.ibatis.type.Alias;
    
    /**
     * 雇员类
     * 
     * @author yyx 2019年3月23日
     */
    @Alias("emp")
    public class Employee {
        /**
         *雇员ID 
         */
        private String empId;
        /**
         * 雇员姓名
         */
        private String empName;
        /**
         * 雇员性别: 0 女,1 男
         */
        private Integer empSex;
        /**
         * 雇员年龄
         */
        private Integer empAge;
    
        public Employee() {
            super();
        }
    
        public Employee(String empId, String empName, Integer empSex, Integer empAge) {
            super();
            this.empId = empId;
            this.empName = empName;
            this.empSex = empSex;
            this.empAge = empAge;
        }
    
        public String getEmpId() {
            return empId;
        }
    
        public void setEmpId(String empId) {
            this.empId = empId;
        }
    
        public String getEmpName() {
            return empName;
        }
    
        public void setEmpName(String empName) {
            this.empName = empName;
        }
    
        public Integer getEmpSex() {
            return empSex;
        }
    
        public void setEmpSex(Integer empSex) {
            this.empSex = empSex;
        }
    
        public Integer getEmpAge() {
            return empAge;
        }
    
        public void setEmpAge(Integer empAge) {
            this.empAge = empAge;
        }
    
    }
  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10589614.html
Copyright © 2011-2022 走看看