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

原文地址:https://www.cnblogs.com/daxiong225/p/9902114.html

时间: 2024-08-30 10:34:05

Mybatis映射器(一)的相关文章

MyBatis映射器总结

Mybatis映射器xml配置包含如下标签: select 查询语句,自定义参数返回结果集 insert 插入语句 update 更新语句 delete 删除语句 parameterMap 定义参数映射关系,不建议使用 sql 定义一段SQL,可以再其他部分引用 resultMap 结果集,提供映射规则 cache 给定命名空间的缓存配置 cache-ref 其他命名空间缓存配置的引用 1.select 1.1 select标签包含如下属性: 属性 说明 备注 id 它和Mybatis的命名空间

MyBatis映射器(一)--多参数传递方式

参考自:https://www.cnblogs.com/hellowhy/p/9678245.html 在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个.当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名称了,mybatis中有以下四种 第一种:使用map传递 1??定义接口 1 // 使用map传递多个参数进行查询 2 public List<Product> getByMap(Map<

Mybatis映射器select

Mybatis映射器select 简单的select元素的应用 id 配合Mapper的全限定名,联合成为一个唯一的标示 parameterType 表示这条SQL接受的参数类型 resultType表示这条SQL返回的结果类型 #{firstName} 是被传递进去的参数 <select id="countUserByFirstName" parameterType="string" resultType="int"> select

Mybatis 映射器接口代理对象的方式 运行过程debug分析

查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

mybatis 映射器

1 映射器 Mapper 是由java接口和 XML 文件共同组成.它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4)定义查询结果和POJO的映射关系 2 SqlSessionFactoryBuilder 主要作用是用来生成 SqlSessionFactory,生成以后就不用了,所以它的生命周期只存在于方法局部. 3 SqlSessionFactory 的作用是创建SqlSession,而 SqlSession 就是一个会话,相当于是JDBC的 Connection 对象

SQL映射器Mapper接口(MyBatis)

SQL映射器Mapper接口 MyBatis基于代理机制,可以让我们无需再写Dao的实现.直接把以前的dao接口定义成符合规则的Mapper. 注意事项: 1.接口必须以Mapper结尾,名字是DomainMapper 2.mapper.xml文件要和Mapper接口建立关系,通过namespace:要能连接到Mapper接口   3.mapper.xml中写查询语句的标签的传入参数类型(parameterType).返回结果类型(resultType)必须和mapper接口中对应方法的传入参数

Java Persistence with MyBatis 3(中文版) 第四章 使用注解配置SQL映射器

在上一章,我们看到了我们是怎样在映射器Mapper XML配置文件中配置映射语句的.MyBatis也支持使用注解来配置映射语句.当我们使用基于注解的映射器接口时,我们不再需要在XML配置文件中配置了.如果你愿意,你也可以同时使用基于XML和基于注解的映射语句. 本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l 动态SQL @SelectProvi

Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务器所提供的SQL语句的巨大威力.与此同时,MyBaits消除了书写大量冗余代码的痛苦,它使使用SQL更容易. 在代码里直接嵌套SQL语句是很差的编码实践,并且维护起来困难.MyBaits使用了映射器配置文件或注解来配置SQL语句.在本章中,我们会看到具体怎样使用映射器配置文件来配置映射SQL语句.

深入浅出MyBatis:「映射器」全了解

本篇文章是「深入浅出MyBatis:技术原理与实践」书籍的总结笔记. 上一篇总结了MyBatis的配置,详细说明了各个配置项,其中提到了映射器,它是MyBatis最强大的工具,也是使用最多的工具. 通过映射器,可以很容易的进行数据的增删改查操作,我们抽象下进行这些操作的关键点:传递查询参数.组装各种场景下的查询条件.关联查询.将查询结果映射为Java Bean对象或集合等.另外,可以通过延迟加载.缓存提高数据查询的性能. 本篇就按照这个思路进行总结,首先列举下映射器的主要元素,每个元素提供的配置