1. 程式人生 > >Mybatis對映器(一)

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