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;
} }
MyBatis探究-----为实体类Bean取别名,配置typeAliases的更多相关文章
- Mybatis自动生成实体类和实体映射工具
		Mybatis Mysql生成实体类 用到的Lib包: mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.30.jar 1. 创建一个文 ... 
- 使用Spring实例化Bean的方法以及Bean取别名
		一.通过构造方法实例化Bean bean中加构造方法 public class Bean1 { public Bean1() { System.out.println("Bean1构造方法. ... 
- 利用org.mybatis.generator生成实体类
		springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖: 2.编写generatorConfig.xml文件 ( ... 
- Mybatis自动生成实体类
		Maven自动生成实体类需要的jar包 一.pom.xml中 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ... 
- mybatis 一对一  映射实体类、嵌套查询
		一对一 在SysUser 类中增加SysRole字段.1.sql语句将role.role_name映射到role.roleName上. 2.还可以在XML 映射文件中配置结果映射.<result ... 
- Mybatis自动生成实体类,映射文件,dao
		http://www.mybatis.org/generator/index.html 方法一:eclipse插件式 1.下载 mybatis-generator-core-1.3.2.jar 解压后 ... 
- maven 工程mybatis自动生成实体类
		generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ge ... 
- Mybatis自动生成实体类、dao接口和mapping映射文件
		由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类.dao接口以及它的映射文件,然后直接拷贝到 ... 
- 使用springboot mybatis 查询时实体类中的驼峰字段值为null
		看到返回结果以后主要分析了一下情况: 实体类的get.set方法确实 mapper.xml文件中的resultMap.resultType等原因导致 数据库中数据存在问题 经过检查与验证发现以上都不存 ... 
随机推荐
- In the beginning, Coders create the repos and blogs
			---恢复内容开始--- 这是一个新的博客 print(‘hello new world’) 这个博客叫做 brain in a jar, 不知道以后会不会改名. 只是因为偶然想到了普特南的缸中之脑理 ... 
- hadoop2-HBase的安装和测试
			在安装和测试HBase之前,我们有必要先了解一下HBase是什么 我们可以通过下面的资料对其有一定的了解: HBase 官方文档中文版 HBase 深入浅出 我想把我知道的分享给大家,方便大家交流. ... 
- Go语言基础之切片
			Go语言基础之切片 本文主要介绍Go语言中切片(slice)及它的基本使用. 引子 因为数组的长度是固定的并且数组长度属于类型的一部分,所以数组有很多的局限性. 例如: func arraySum(x ... 
- [LeetCode] Binary Trees With Factors 带因子的二叉树
			Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ... 
- SSIS - 2.使用脚本任务弹出对话框
			步骤如下: 1.打开Visual Studio 2012或者SSDT工具->单击“文件”->选择"新建"打开创建新工程的对话框如下: 2.在"Business ... 
- css学习_css3伸缩布局  flex布局
			1.flex布局 案例一: 案例二: 保证不至于缩放得太小或太大 案例三:flex的值不一定要写成几份,可以写成固定值 案例四: 竖着3等分(父容器按照高度3等分) !!案例 -----用fle ... 
- go/wiki/MutexOrChannel  Golang并发:选channel还是选锁?
			https://mp.weixin.qq.com/s/JcED2qgJEj8LaBckVZBhDA https://github.com/golang/go/wiki/MutexOrChannel M ... 
- Go并发示例-Pool
			https://mp.weixin.qq.com/s/MBY6l5VxrFPJ4AA8nGeQUQ <Go语言实战>笔记(十六) | Go并发示例-Pool 飞雪无情 异步图书 2017- ... 
- urllib库的应用及简单爬虫的编写
			1.urllib库基础 1.1爬虫的异常处理 常见状态码及含义 301 Moved Permanently:重定向到新的URL,永久性 302 Found:重定向到临时的URL,非永久性 304 No ... 
- oracle初级语法
			--select关键字第一个句型: select emp.ename,emp.sal from emp; --from 关键字后面跟 需要查询表名: select ename from emp; -- ... 
