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表示的执行完数据库操作之后,返回的结果数据类型。
这个结果可以用三种类型数据来处理:
- 简单类型。例如:string、long、integer、double等
- pojo类型。例如:Person,User等
- 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.完成高级查询。例如:一对一、一对多、多对多。
数据库表中的列名称与实体类中的成员变量的名称不相同时?
- 设置resultMap
- 设置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>