• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

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;
    }

}

 

posted @ 2019-03-24 22:04  风拂晚柳  阅读(1568)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3