目录
一、Mybatis学习。
(1)MyBatis的多表操作。
(1.1)一对一查询。
(1.2)一对多查询。
(1.3)多对多查询。
(1.4)三种查询知识小结。
一、Mybatis学习。
(1)MyBatis的多表操作。
(1.1)一对一查询。
第1种映射OrderMapper.xml文件:
<?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="mapper.OrderMapper">
<resultMap id="orderMap" type="domain.Order">
<!--手动指定字段与实体属性的映射关系
column:数据表的字段名称
property:实体的属性名称
-->
<id column="oid" property="id"></id><!--因为数据库多表查询的两个表都有id-->
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<result column="uid" property="user.id"></result>
<result column="username" property="user.username"></result>
<result column="password" property="user.password"></result>
<result column="birthday" property="user.birthday"/>
</resultMap>
<select id="findAll" resultMap="orderMap">
select *,o.id oid from orders o,user u where o.uid = u.id
</select>
</mapper>
第2种映射OrderMapper.xml文件:
<?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="mapper.OrderMapper">
<resultMap id="orderMap" type="domain.Order">
<!--手动指定字段与实体属性的映射关系
column:数据表的字段名称
property:实体的属性名称
-->
<id column="oid" property="id"></id><!--因为数据库多表查询的两个表都有id-->
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<!--association标签属性:
property:当前实体(order)中的属性名称(private User user)user
javaType:当前实体(order)中的属性的类型(User)
-->
<association property="user" javaType="domain.User">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="orderMap">
select *,o.id oid from orders o,user u where o.uid = u.id
</select>
</mapper>
(1.2)一对多查询。
映射UserMapper.xml文件:
<?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="mapper.UserMapper">
<!--一对多查询的配置-->
<resultMap id="userMap" type="domain.User">
<!--user的信息-->
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="birthday" property="birthday"/>
<!--配置集合信息:
property:集合名称
ofType:当前集合中的数据类型
-->
<collection property="orderList" ofType="domain.Order">
<!--封装order的数据-->
<id column="oid" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
select *,o.id oid from user u,orders o where u.id = o.id
</select>
</mapper>
(1.3)多对多查询。
映射UserMapper.xml文件:
<?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="mapper.UserMapper">
<resultMap id="userRoleMap" type="domain.User">
<!--user的信息-->
<id column="userid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="birthday" property="birthday"/>
<collection property="roleList" ofType="domain.Role">
<!--user内部的roleList信息-->
<id column="roleid" property="id"/>
<result column="roleName" property="roleName"/>
<result column="roleDesc" property="roleDesc"/>
</collection>
</resultMap>
<!--多对多的查询配置-->
<select id="findUserAndRole" resultMap="userRoleMap">
select * from user u,sys_user_role ur,sys_role r where u.id = ur.userid and ur.roleid = r.id
</select>
</mapper>