typeAliases
四种方式,感觉记住一种就好,通过包找到类,生成别名(类名)(如果类上面加了注解以注解为主)
1.配置文件中加入下面属性
<typeAliases>
<package name="cn.com.mybatis.po"/>
</typeAliases>
2.在配置文件中的用法
作用:在配置文件中就不需要写全路径了可以直接写别名,如下面可以直接写
resultType=“BatchCustomer”,而不需要写resultType=“cn.com.mybatis.po.BatchCustomer”
<select id="findBatchCustomer" resultType="BatchCustomer">
SELECT
batch.*, customer.username,
customer.acno
FROM
batch,
customer
WHERE
batch.cus_id = customer.cus_id;
</select>
typeHandlers
- 首先在配置文件中加入参数属性
<typeHandlers>
<typeHandler handler="cn.com.mybatis.handlers.DateTypeHandler"/>
</typeHandlers>
指定重写的handler的类
- 类的具体写法
//泛型String是指映射成String
public class DateTypeHandler implements TypeHandler<String>{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//通过名称获取值(这个已经测试过,下面的方法尚未测试)
@Override
public String getResult(ResultSet resultset, String s) throws SQLException {
//通过名称获取返回的值
Date date = resultset.getDate(s);
//处理这个值
String format = sdf.format(date);
//返回string类型(将Date转换成String)(String与上面的泛型对应的)
return format;
}
//通过下标获取值
@Override
public String getResult(ResultSet resultset, int i) throws SQLException {
Date date = resultset.getDate(i);
String format = sdf.format(date);
return format;
}
//供存储过程调用
@Override
public String getResult(CallableStatement callablestatement, int i)
throws SQLException {
Date date = callablestatement.getDate(i);
String format = sdf.format(date);
return format;
}
//从java对象传入的参数转换
@Override
public void setParameter(PreparedStatement arg0, int arg1, String arg2,
JdbcType arg3) throws SQLException {
java.util.Date parse = null;
try {
parse = sdf.parse(arg2);
} catch (ParseException e) {
e.printStackTrace();
}
Date date = new java.sql.Date(parse.getTime());
arg0.setDate(arg1, date);
}
}
- 映射文件写法
<select id="findBatchCustomerTwo" resultMap="test1">
SELECT
batch.*, customer.username,
customer.acno
FROM
batch,
customer
WHERE
batch.cus_id = customer.cus_id;
</select>
<resultMap type="BatchCustomer" id="test1">
<result column="createtime" javaType="string" jdbcType="DATE" typeHandler="cn.com.mybatis.handlers.DateTypeHandler" property="createtimeStr" />
</resultMap>
重点在:<result column="createtime" javaType="string" jdbcType="DATE" typeHandler="cn.com.mybatis.handlers.DateTypeHandler" property="createtimeStr" />
column:数据库字段名称
property:java对象字段名称
javaType:映射的java字段类型
jdbcType:数据库的字段类型
typeHandler:类型处理器的类
验证查询传入参数的类型转换:
typeHandler写法
public void setParameter(PreparedStatement arg0, int arg1, String arg2,
JdbcType arg3) throws SQLException {
//arg2是传入的参数,和泛型对应的
int t = 0;
GenderEnum[] values = GenderEnum.values();
for (GenderEnum genderEnum : values) {
if(genderEnum.name().equals(arg2)){
t = genderEnum.getCode();
break;
}
}
//t是你想转换的数据库类型
arg0.setInt(arg1, t);
}
<select id="selectByHCode" parameterType="String" resultType="TryHandlerDetail">
select
t.*
from
tryhandler t
where
hcode = #{hstr,javaType=string,jdbcType=INTEGER,typeHandler=cn.com.mybatis.handlers.gender}
</select>
javaType:java类型,不区分大小写
jdbcType:jdbc类型,区分大小写
测试可用!
关于mybatis的自动映射
- 一对一
第一种方法是继承
<select id="findBatchCustomer" resultType="BatchCustomer">
SELECT
batch.*, customer.username,
customer.acno
FROM
batch,
customer
WHERE
batch.cus_id = customer.cus_id;
</select>
public class BatchCustomer extends Batch{}
第二种方式,成员变量(嵌入式查询,association )
<select id="findBatchCustomerToMap" resultMap="BatchInfoMap">
SELECT
batch.*,
customer.*
FROM
batch,
customer
WHERE
batch.cus_id = customer.cus_id;
</select>
<resultMap type="BatchItem" id="BatchInfoMap">
<id column="batch_id" property="batch_id"/>
<result column="cus_id" property="cus_id"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<association property="customer" javaType="Customer">
<id column="cus_id" property="cus_id"/>
<result column="username" property="username"/>
<result column="acno" property="acno"/>
<result column="gender" property="gender"/>
<result column="phone" property="phone"/>
</association>
</resultMap>
public class BatchItem {
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
还可以用全部映射的方式,resultMap中就不需要列举全部字段(但是不推荐这样:因为如果两个表中有相同的字段会出现错误映射的情况)
<select id="findBatchCustomerToMapTwo" resultMap="BatchInfoMapTwo">
SELECT
batch.*,
customer.*
FROM
batch,
customer
WHERE
batch.cus_id = customer.cus_id;
</select>
<resultMap type="BatchItem" id="BatchInfoMapTwo" autoMapping="true">
<id column="batch_id" property="batch_id"/>
<association property="customer" javaType="Customer"/>
</resultMap>
public class BatchItem {
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
映射类型有三种
“NONE”:不启动自动映射
“PAPTIAL”:只对非前天的resultMap进行自动映射
“FULL”:对所有的resultMap斗进行映射
ps:autoMapping="true"这个属性会覆盖mybatis映射文件中的setting属性(自动映射)
<settings>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
总结:嵌入查询就是对应成员变量的用法:1.开启FULL的自动映射 2.写resultMap每个字段都列举出来
而继承的方式随便都可以映射
2. 一对多