MyBatis映射器元素

 映射器是MyBatis最强大的工具,也是我们使用MyBatis时用的最多的工具,映射器中主要有增删改查四大元素,来满足不同场景的需要;
下面是主要元素的介绍;
        select:查询语句,insert:插入语句,update:更新语句;delete:删除语句;sql:允许定义一部分的SQL,然后在各个地方引用他;
        resultMap:用来描述从数据库结果集中来加载对象,他是最复杂,最强大的元素;
        select元素;作为查询语句,我们需要的就有一个传入参数,一个结果集,而且要做好相应的映射;下面是select元素的配置;
        id,简单地说,就是用来标识select元素的配置;
        parameterType,这个就是传入参数的配置,可以是单元素,javabean,Map等复杂的参数类型传递;
        resultType,resultMap,他是映射集的引用,可以自定义映射规则,映射javaBean,定义int,double,float等参数,不能同时使用。
        下面简易数据类型的例子;
<select id = "countFirstName" parameterType = "string" resultType = "int">
    select count(*) as total from t_user where name like concat(#{firstName},'%')
</select>
        其中id标出了这条SQL;
        parameterType定义了参数类型;
        resultType定义返回值类型;
        下面是以javabeanl类型来映射结果集;
public class Role{
    private Long id;
    private String roleName;
    private String note;
    public Long getId(){
        return id;
    }
    public void setId(Long id){
        this.id = id;
    }
    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;
    }
}

  结果集映射的javabean已经写好了,接下来就是自动映射的语句;

<select parameterType = "id" id = "getRole" resultType = "com.learn.chapter4.pojo.Role">
    select id, role_name as roleName, note from t_note
    where id = #{id}
</select>

  再提供一个方法;

public Role getRole(Long id);

  接下来是关于传递参数的映射,首先,如何传递多个参数呢?这里有三个方法,是根据你的参数的个数来决定的,map方法,注解方法和javabean方法,其中,map方法因为可读性较差,在这里就不列举了,下面是注解的方式来传递;

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

  我们把映射器的xml修改为无需定义参数类型,

<select id = "findRoleByAnnotation" resultMap = "roleMap">
    select id ,role_name,note from t_role
    where role_name like concat ('%',#{roleName},'%')
    and note like concat('%',#{note},'%')
</select>
这里说无需定义参数类型也就是说不用写parameterType
使用javabean传递参数的方式跟上面结果集的映射是一样的; 需要指出,当参数的个数<=5个的时候,可以考虑用注解传参,而当参数的个数大于5个的时候,可以考虑用javabean方式;
还有一种是使用resultMap映射结果集,这样的话,我们在映射器中定义resultMap;
<resultMap id = "roleResultMap" type = "com.learn.chapter4.pojo.Role">
    <id property = "id" column = "id">
    <result property = "roleName" column = "role_name">
    <result property = "note" column = "note"/>
</resultMap>
<select parameterType = "long" id = "getRole" resultMap = "roleResultMap">
    select id,role_name,note from t_role where id = #{id}
</select>
解释一下,id是设置代表着使用哪个属性作为其主键,result元素定义普通列的映射关系;这样的话,select语句就不再需要自动映射的规则,直接用resultMap属性指定roleResultMap即可,下面还会讲到这个属性;
        insert增加语句,这个语句相比之下,少了一个结果集返回的影射,但是多了一个主键的设置,
下面是代码;
<insert parameterType = "role" id = "insertRole">
    insert into t_role(role_name,note) values (#{roleName},#{note})
</insert>

  有时候我们还需要声名主键 ,在声明的时候,我们传入的参数模型是不需要传入id这个属性的,因为我们会另外拿出来声明,使用keyProperty指定哪个是主键字段,用useGeneratedKeys属性告诉MyBatis这个主键是否使用数据库内置策略生成;

<insert id = "insertRole" parameterType = "role" 
        useGeneratedKeys = "true" keyProperty = "id">
    insert into t_role (role_name,note) values (#{roleName},#{note})
</insert>
所以,我们传入的rolejavabean对象是不需要设置id的值的,
update元素和delete元素,这两个都是只有传入参数类型,而没有结果集映射类型;
<update parameterType = "role" id = "updateRole">
    update t_role set 
    role_name = #{roleName},
    note = #{note}
    where id = #{id}
</update>
<delete id = "delete" parameterType = "long"
        delete from t_role where id = #{id}
</delete>
提一点就是我们可以替换掉特殊字符串,也就是灵活地传入参数;
    select ${columns} from t_tablenname;
sql元素;
    sql元素的功能在于我们可以将我们常用的参数给封装在sql语句中,下次要用的时候直接用<include>标签;
<sql id = "role_columns">
    id,role_name,note
</sql>
<select parameterType = "long" id = "getRole" resultMap = "roleMap">
    select <include refid = "role_columns"/> from t_note where id = #{id}
</select>
<select parameterType = "map" id = "findRoles">
    select id, role_name,note from t_role
    where role_name like concat('%',#{roleName},'%')
    and note like concat('%',#{note,'%'})
</select>

  

原文地址:https://www.cnblogs.com/zxx123/p/8571622.html