mybatis高级查询
sqlSession的获取
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(mysqlDataSource());
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
bean.setTypeAliasesPackage("com.getword.entity");
sqlSessionFactory = bean.getObject();
one to one
方式一
假设user和role是一对一关系,中间表为Sys_user_role
public class SysUser{
private SysRole role;
...
}
<select id="findUserAndRoleById" resultType="SysUser">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id "role.id",
r.roleName "role.roleName"
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
方式二
使用resultmap,和第一种方式类似
<resultMap id="userRoleMap" type="SysUser">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="userPassword" column="userPassword" />
<result property="role.id" column="roleId" />
<result property="role.roleName" column="roleName" />
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id roleId,
r.roleName roleName
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
方式三,association一一对应
此种方式,对于多表查询,只会封装指定的result标签的字段
<resultMap id="userRoleMap" type="SysUser">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="userPassword" column="userPassword" />
<association property="role" columnPrefix="role" javaType="SysRole">
<result property="id" column="Id" />
<result property="roleName" column="Name" />
</association>
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id roleId,
r.roleName roleName
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
resultMap可以继承
<resultMap id="userRoleMap" type="SysUser" extends="userMap">
<result property="userPassword" column="userPassword" />
<association property="role" columnPrefix="role" javaType="SysRole">
<result property="id" column="Id" />
<result property="roleName" column="Name" />
</association>
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id roleId,
r.roleName roleName
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
方式四,association嵌套查询,最简单的方式
<resultMap id="userMap" type="SysUser">
<id property="id" column="id" />
<result property="userName" column="userName" />
</resultMap>
<resultMap id="userRoleMap" type="SysUser" extends="userMap">
<result property="userPassword" column="userPassword" />
<association property="role" column="roleId" select="com.getword.dao.SysRoleDao.findById" />
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id roleId
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
简写
<resultMap id="userRoleMap" type="SysUser">
<result property="userPassword" column="userPassword" />
<association property="role" column="roleId" select="com.getword.dao.SysRoleDao.findById" />
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
*
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
where u.id = #{id}
</select>
one to many
方式一
<resultMap id="userMap" type="SysUser">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="userPassword" column="userPassword" />
</resultMap>
<resultMap id="userRoleMap" type="SysUser" extends="userMap">
<collection property="roles" javaType="SysRole" columnPrefix="role">
<id property="id" column="Id" />
<result property="roleName" column="Name" />
</collection>
</resultMap>
<select id="findUserAndRoleById" resultMap="userRoleMap">
select
u.id,
u.userName userName,
u.userPassword userPassword,
r.id roleId,
r.roleName roleName
from sys_user u
inner join sys_user_role ur on u.id=ur.userId
inner join sys_role r on r.id = ur.roleId
where u.id = #{id}
</select>
对于id相同的user被归为一个user,相当于分组了