文件的dtd声明
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
外层mapper
- namespace:唯一标记,包名加上映射文件的名字
<mapper namespace="mapper.uerMapper">
mapper中可包含的元素
select
- 可用的属性
insert updata delete
- 增加了两个属性 useGeneratedKeys和keyProperty
useGeneratedKeys:使用mysql的主键自动增长规则,但是要求所用的数据库可以主键自动增长,mysql可以Orcal不可以
keyProperty:将自动增长的键的值赋给一个属性,java对象的属性,而不是数据库中的字段名。
**mapper文件:**
<insert id="save" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into tb_user(name,age,sex) values(#{name},#{age},#{sex})
</insert>
**使用的代码:**
User user = new User();
user.setName("test");
user.setAge(22);
user.setSex("男");
int insert = sqlSession.insert("mapper.uerMapper.save", user);
//说明id已经被赋值
System.out.println(user.getId());
sql:定义被复用的sql语句
- 示例代码:
//定义sql语句
<sql id="userColumns">
${table}.id,${table}.name,${table}.age
</sql>
//使用被定义的sql语句
<select id="getUser" parameterType="int" resultType="map">
select
<include refid="userColumns"><property name="table" value="t1"></property></include>
from tb_user t1
where
id=#{id}
</select>
resultMap:当java的属性名与数据库的列名不一致时进行映射
- 只有当数据库的列名与java类的属性名一致时才可以对select的结果数据进行封装。如下将直接将查询的结果封装进user对象中。
<select id="getUser" parameterType="int" resultType="user">
select
id,name,sex
from tb_user t1
where
id=#{id}
</select>
- 当数据库的列名与java类的属性名不一致时,就需要用到resultMap指定映射进行封装。
//数据库中有表tb_student(s_id,s_name,s_age,s_cid),java中有类Student(id,name,age,cid)
<!--type:实际返回的类型 因为在主配置中配置了别名 此处直接使用-->
<resultMap id="student" type="student">
<!--property:类中的名字
column:数据库中的列的名字-->
<id property="id" column="s_id"></id>
<result property="name" column="s_name"></result>
<result property="age" column="s_age"></result>
<result property="cid" column="s_cid"></result>
</resultMap>
//使用resultMap进行数据的接收
<select id="getStudent" resultMap="student">
select * from tb_student
</select>
- 当所查的结果中包含另一个对象时需要使用resultMap进行映射,用到association
//数据库中有表tb_student(s_id,s_name,s_age,s_cid),tb_teacher(c_id,c_name,c_age),java中有类Student(id,name,age,teacher),Teacher(id,name,age)
<resultMap id="teacher" type="teacher">
<id property="id" column="c_id"></id>
<result property="name" column="c_name"></result>
<result property="age" column="c_age"></result>
</resultMap>
<!--type:实际返回的类型-->
<resultMap id="student" type="student">
<!--property:类中的名字
column:数据库中的列的名字-->
<id property="id" column="s_id"></id>
<result property="name" column="s_name"></result>
<result property="age" column="s_age"></result>
//association :将通过cid查询出来的结果再通过一个sql语句查询出来,并进行fengzhuang
property:在student类中对应的属性名
column:数据库中的列名,用作select属性的查询参数
javaType:该属性对应的类型的名称
select:用于查询的语句
<association property="teacher" column="s_cid" javaType="teacher" select="selectTeacherWithId"></association>
</resultMap>
//此处的resultMap是要将获得的数据进行封装为teacher,上面的javaType是属性对应的类型。
//可以暂时理解为将获得的resultMap指定的类型的数据赋值给javaType,此处javaType可以为Object,多态
<select id="selectTeacherWithId" resultMap="teacher">
select * from tb_teacher where c_id=#{id}
</select>
<select id="getStudent" resultMap="student">
select * from tb_student
</select>
- 当所查询的对象中包含一个list集合,list中存储的另一个对象
例如在teacher中包含一个llist,list中包含student对象
查询所有的teacher,每个teacher对象包含一个list,list中是他的学生
//teacher类,包含一个list集合
public class Teacher {
private Integer id;
private String name;
private Integer age;
private List list=new ArrayList<Student>();
}
//resultMap 映射的类型 以及collection的用法
<resultMap id="teacher" type="teacher">
<id property="id" column="c_id"></id>
<result property="name" column="c_name"></result>
<result property="age" column="c_age"></result>
<!--collection 对应于list
column:用作查询参数的数的类型
ofType:集合中的元素的类型
-->
<collection property="list" javaType="List" column="c_id" ofType="student" select="selectStudentWithCid"></collection>
</resultMap>
//查询的语句
<select id="selectStudentWithCid" resultMap="student">
select * from tb_student where s_cid=#{id}
</select>
<select id="getTeacher" resultMap="teacher" >
select * from tb_teacher
</select>