MyBatis(四)

2.MyBatis的核心配置文件中的typeAliases元素有什么作用,如何配置,如何使用?

   typeAliases元素---出现在MyBatis的核心配置文件中,给SQL映射文件的数据类型设置别名用的。SQL映射文件的数据类型,insert 元素的参数类型,resultMap元素的types属性等这些地方都需要数据类型。如果我们不设置typeAliases元素,那么SQL映射文件的数据类型就得是包名+类名。

1.一个类一个别名【默认别名】

MyBatis的核心配置文件

<typeAliases>
    <!--默认的别名 [类名,不区分大小写]-->
    <typeAlias type="com.wangxing.mybatis.bean.Person"></typeAlias>
</typeAliases>

Sql映射文件

<insert id="insertPerson" parameterType="Person">
    insert into t_person values (null,#{pername},#{perage},#{peraddress});
</insert>
<update id="updatePerson" parameterType="person">
    update t_person set per_name=#{pername},per_age=#{perage},per_address=#{peraddress} where per_id=#{perid};
</update>

2.一个类一个别名【指定别名】

MyBatis的核心配置文件

<typeAliases>
    <typeAlias alias="personBean" type="com.wangxing.mybatis.bean.Person"></typeAlias>
</typeAliases>

Sql映射文件

<resultMap id="personMap" type="personBean">
    <id column="per_id" property="perid"></id>
    <result column="per_name" property="pername"></result>
    <result column="per_age" property="perage"></result>
    <result column="per_address" property="peraddress"></result>
</resultMap>
<select id="selectPersonById" parameterType="int" resultMap="personMap">
    select * from  t_person where per_id=#{perid};
</select>

3.指定包下的所有类自动生成别名【类名,不区分大小写】

MyBatis的核心配置文件

<typeAliases>
    <!--指定包下的所有类自动生成别名【类名,不区分大小写】-->
    <package name="com.wangxing.mybatis.bean"/>
</typeAliases>

SQL映射文件

<insert id="insertPerson" parameterType="Person">
    insert into t_person values (null,#{pername},#{perage},#{peraddress});
</insert>
<update id="updatePerson" parameterType="person">
    update t_person set per_name=#{pername},per_age=#{perage},per_address=#{peraddress} where per_id=#{perid};
</update>

3.Sql映射文件中的select元素resultType resultMap属性的区别?【输出数据就是返回值】

resultType/resultMap都是查询语句执行之后的返回结果的类型表示属性

会在sql映射文件中select元素中使用

 

1.resultType表示的执行完数据库操作之后,返回的结果数据类型。

这个结果可以用三种类型数据来处理:

  1. 简单类型。例如:string、long、integer、double等
  2. pojo类型。例如:Person,User等
  3. HashMap类型。

数据库表中的列名称与实体类中的成员变量的名称相同时,一般设置resultType指定返回的结果数据类型。

数据库表

create  table t_person(
per_id int primary key auto_increment,
per_name varchar(20),
per_age int,
per_address varchar(30)
);

java实体类

package com.wangxing.mybatis.bean;

public class Person {
    private int per_id;
    private String per_name;
    private int per_age;
    private String per_address;
........
getXXX()、setXXX()
}

数据访问接口

import com.wangxing.mybatis.bean.Person;
import java.util.List;
public interface PersonMapper {
    /**
     * 查询所有数据
     * @return
     */
    List<Person> selectPerson();
}

SQL映射文件

<?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.wangxing.mybatis.mapper.PersonMapper">
    <select id="selectPerson" resultType="com.wangxing.mybatis.bean.Person">
        select * from  t_person;
    </select>
</mapper>

2.resultMap表示的执行完数据库操作之后,返回的结果数据类型。

resultMap是mybatis中最重要最强大的元素,使用resultmap可以解决2个问题:

  1.数据库表中的列名称与实体类中的成员变量的名称不相同时设置resultMap指定返回的结果数据类型。

数据库表

create  table t_person(
per_id int primary key auto_increment,
per_name varchar(20),
per_age int,
per_address varchar(30)
);

java实体类

package com.wangxing.mybatis.bean;

public class Person {
    private int perid;
    private String pername;
    private int perage;
    private String peraddress;
........
getXXX()、setXXX()
}

数据访问接口

import com.wangxing.mybatis.bean.Person;
import java.util.List;
public interface PersonMapper {
    /**
     * 查询所有数据
     * @return
     */
    List<Person> selectPerson();
}

SQL映射文件

<?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.wangxing.mybatis.mapper.PersonMapper">
    <resultMap id="personMap" type="com.wangxing.mybatis.bean.Person">
        <id column="per_id" property="perid"></id>
        <result column="per_name" property="pername"></result>
        <result column="per_age" property="perage"></result>
        <result column="per_address" property="peraddress"></result>
    </resultMap>
    <select id="selectPerson" resultMap="personMap">
        select * from  t_person;
    </select>
</mapper>

2.完成高级查询。例如:一对一、一对多、多对多。

 

数据库表中的列名称与实体类中的成员变量的名称不相同时

  1. 设置resultMap
  2. 设置resultType,SQL语句设置别名建立映射【不推荐使用】

resultMap与resultType的区别?

resultType:

执行完数据库操作之后,返回的结果数据类型

数据库表中的列名称与实体类中的成员变量的名称相同时,一般设置resultType指定返回的结果数据类型

<select id="selectPerson" resultType="PersonBean">
		select * from t_person;
	</select>

给查询到的表起别名,别名和javabean的变量名一致

 <select id="selectPerson" resultType="PersonBean">
	 	select per_id as perid,per_name as pername,
	 		per_age as perage,per_address as peraddress
	 		from t_person;
	 </select>

resultMap:

执行完数据库操作之后,返回的结果数据类型

数据库表中的列名称实体类成员变量名称不相同时,设置resultMap指定返回的结果数据类型

<select id="selectPerson" resultMap="personMap">
		select * from t_person;
	</select>
	<resultMap type="PersonBean" id="personMap">
		<id column="per_id" property="perid"/>
		<result column="per_name" property="pername"/>
		<result column="per_age" property="perage"/>
		<result column="per_address" property="peraddress"/>
	</resultMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值