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();
            }
        }
    }

猜你喜欢

转载自www.cnblogs.com/daxiong225/p/9902114.html