zoukankan      html  css  js  c++  java
  • Mybatis映射器(一)

    XML查询参数:

    parameterType:可以给出类别名,全名等.

    resultType:查询结果,可以为 int,float,map等不可以与resultMap同时使用。

    resultMap: 映射集的引用可以配置映射规则,级联,typeHandler等,是mybatis最复杂的元素。

     本文返回是resultType。

    查询方法可以在parameterType设置为单个参数XML设置为int,string等,多个参数可以设置为map

    <select id="getRoleUseResultMap" parameterType="long" resultMap="roleMap">
    select id, role_name, note from t_role where id = #{id}
    </select>

    <select id="findRolesByMap" parameterType="map" resultType="role">
    select id, role_name as roleName, note from t_role
    where role_name like
    concat('%', #{roleName}, '%')
    and note like concat('%', #{note}, '%')
    </select>

    或者多个参数避免map可读性差时可以用在Mapper java中注解(此时XML中没有parameterType)

    public List<Role> findRolesByAnnotation(@Param("roleName") String rolename, @Param("note") String note);

    <select id="findRolesByAnnotation" resultType="role">
    select id,
    role_name as roleName, note from t_role
    where role_name like
    concat('%', #{roleName}, '%')
    and note like concat('%', #{note}, '%')
    </select>

    亦或是通过java bean传递多个参数:

    public class RoleParams {
    private String roleName;
    private String note;

    public String getRoleName() {
    return roleName;
    }
    public void setRoleName(String roleName) {
    this.roleName = roleName;
    }
    public String getNote() {
    return note;
    }
    public void setNote(String note) {
    this.note = note;
    }
    }

    mapper:

    public List<Role> findRolesByBean(RoleParams roleParam);

    XML:parameterType="com.ssm.chapter5.param.RoleParams".

    <select id="findRolesByBean" parameterType="com.ssm.chapter5.param.RoleParams"
    resultType="role">
    select id, role_name as roleName, note from t_role
    where
    role_name like
    concat('%', #{roleName}, '%')
    and note like concat('%',#{note}, '%')
    </select>

    -----------------------------------------------------------------------------------------------

    测试:

    case 1:简单查询

    <select id="getRole" parameterType="long" resultType="com.ssm.chapter5.pojo.Role">
    select id,
    role_name as roleName, note from t_role where id = #{id}
    </select>
    mapper: public Role getRole(Long id);

    public static void testGetRole() { SqlSession sqlSession = null; try { sqlSession = SqlSessionFactoryUtils.openSqlSession(); RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); Role role = roleMapper.getRole(1L); System.out.println(role.getRoleName()); } catch(Exception ex) { ex.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } }

    case 2: map 查询:

        <select id="findRolesByMap" parameterType="map" resultType="role">
            select id, role_name as roleName, note from t_role
            where role_name like
            concat('%', #{roleName}, '%')
            and note like concat('%', #{note}, '%')
        </select>
    mapper: 
    public List<Role> findRolesByMap(Map<String, Object> parameterMap);
    
    test:
    public static void testFindRolesByMap() {
            SqlSession sqlSession = null;
            try {
                sqlSession = SqlSessionFactoryUtils.openSqlSession();
                RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
                Map<String, Object> parameterMap = new HashMap<String, Object>();
                parameterMap.put("roleName", "1");
                parameterMap.put("note", "1");
                List<Role> roles = roleMapper.findRolesByMap(parameterMap);
                System.out.println(roles.size());
            } catch(Exception ex) {
                ex.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }

    case 3: 注解:

    xml:
    <select id="findRolesByAnnotation" resultType="role">
            select id,
            role_name as roleName, note from t_role
            where role_name like
            concat('%', #{roleName}, '%')
            and note like concat('%', #{note}, '%')
    </select>
    mapper:    
    public List<Role> findRolesByAnnotation(@Param("roleName") String rolename, @Param("note") String note);
    
    test:
    public static void testFindRolesByAnnotation() {
            SqlSession sqlSession = null;
            try {
                sqlSession = SqlSessionFactoryUtils.openSqlSession();
                RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
                List<Role> roles = roleMapper.findRolesByAnnotation("1", "1");
                System.out.println(roles.size());
            } catch(Exception ex) {
                ex.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }

    case 4: java bean:

    java bean:
    public class RoleParams {
        private String roleName;
        private String note;
    
        public String getRoleName() {
            return roleName;
        }
        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
        public String getNote() {
            return note;
        }
        public void setNote(String note) {
            this.note = note;
        }
    }
    
    xml:
    <select id="findRolesByBean" parameterType="com.ssm.chapter5.param.RoleParams"
            resultType="role">
            select id, role_name as roleName, note from t_role
            where
            role_name like
            concat('%', #{roleName}, '%')
            and note like concat('%',
            #{note}, '%')
    </select>
    
    mapper:
    public List<Role> findRolesByBean(RoleParams roleParam);
    
    test
    public static void testFindRolesByBean() {
            SqlSession sqlSession = null;
            try {
                sqlSession = SqlSessionFactoryUtils.openSqlSession();
                RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
                RoleParams roleParam = new RoleParams();
                roleParam.setNote("1");
                roleParam.setRoleName("1");
                List<Role> roles = roleMapper.findRolesByBean(roleParam);
                System.out.println(roles.size());
            } catch(Exception ex) {
                ex.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }

    case 5: 混合使用 bean 和注解

    bean 2:
    
    public class PageParams {
        private int start;
        private int limit;
    
        public int getStart() {
            return start;
        }
    
        public void setStart(int start) {
            this.start = start;
        }
    
        public int getLimit() {
            return limit;
        }
    
        public void setLimit(int limit) {
            this.limit = limit;
        }
    }
    
    xml:
    <select id="findByMix" resultType="role">
            select id, role_name as
            roleName, note from t_role
            where role_name like
            concat('%',
            #{params.roleName}, '%')
            and note like concat('%', #{params.note}, '%')
            limit #{page.start}, #{page.limit}
        </select>
    
    mapper:
    public List<Role> findByMix(@Param("params") RoleParams roleParams, @Param("page") PageParams PageParam);
    
    test:
    public static void testFindByMix() {
            SqlSession sqlSession = null;
            try {
                sqlSession = SqlSessionFactoryUtils.openSqlSession();
                RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
                RoleParams roleParam = new RoleParams();
                roleParam.setNote("1");
                roleParam.setRoleName("1");
                PageParams pageParams = new PageParams();
                pageParams.setStart(0);
                pageParams.setLimit(100);
                List<Role> roles = roleMapper.findByMix(roleParam, pageParams);
                System.out.println(roles.size());
            } catch(Exception ex) {
                ex.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
  • 相关阅读:
    supervisord + docker run = web页面管理运行的docker
    docker之Dockerfile实践用dockerfile构建nginx环境
    Dockerfile文件详解
    【docker】CMD ENTRYPOINT 区别 终极解读!
    golang html/template
    网络连接带宽的理论最大值
    Queue length 和 Queue depth 的区别
    .tar.gz 文件和 .tar.xz 文件的区别
    如何同时在Isilon的所有网卡上抓取网络包?
    Reimage Isilon cluster,结果忘记了修改管理口的netmask,怎么办?
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9902114.html
Copyright © 2011-2022 走看看