• 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,相当于分组了

  • 相关阅读:
    C# -- 使用缓冲区进行文件下载操作
    C# -- 使用ODBC连接数据库
    C# -- Quartz.Net入门案例
    C# -- LinkedList的使用
    ASP.NET -- 获取浏览器信息
    Windows -- 从注册表删除IE浏览器加载项
    C# -- FTP上传下载
    C# -- 使用Ping检查网络是否正常
    WinForm -- 为TextBox文本框添加鼠标右键菜单
    C# -- 使用Parallel并行执行任务
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/11532123.html
Copyright © 2020-2023  润新知