Home > Java > javaTutorial > body text

Detailed explanation of Mybatis mapping files

不言
Release: 2018-10-12 14:50:55
forward
4252 people have browsed it

This article brings you a detailed explanation of the Mybatis mapping file. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The real core of MyBatis is the mapping file. Because it is extremely powerful, if you compare it with JDBC code with the same function, you will find that it saves nearly 95% of the code.

MyBatis entity class mapping file

MyBatis has several top-level elements

select: mapping query statement

<select id="findAll" resultType="com.simple.mybatis.entitys.Employee">
        select * from tal_employee
</select>
Copy after login

insert: mapping insertion statement

Use #{parameter attribute name} to assign value

<insert id="saveEmp" parameterType="com.simple.mybatis.entitys.Employee">
        insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})
</insert>
Copy after login

: Mapping modification statement

<update id="updateEmp" parameterType="com.simple.mybatis.entitys.Employee">
        update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
 </update>
Copy after login

: Mapping deletion statement

<delete id="deleteEmp" parameterType="Integer">
        delete from tal_employee where id=#{id}
</delete>
Copy after login
Copy after login

: SQL statement that can be reused by other SQL statements .
: It is the most complex and powerful element, used to map database tables and entity classes.

<resultMap type="com.simple.mybatis.entitys.Employee" id="Employee">
    <id property="id" column="id"/>
    <result property="lastName" column="last_name"/>
    <result property="email" column="email"/>
    <result property="gender" column="gender"/></resultMap><!-- 返回外部resultMap格式的类型。-->
 <select id="findAll" resultMap="Employee">
    select id,last_name AS lastName,email,gender from tal_employee
</select>
Copy after login

: Cache configuration for the given namespace.
: References to other namespace cache configurations.

We combine Chapter 1 and use the previous method of interacting with the database:

sqlSession.selectList("命名空间.select标签ID");
sqlSession.selectOne("命名空间.select标签ID");
sqlSession.update("命名空间.update标签ID", "Object类型参数");
sqlSession.delete("命名空间.delete标签ID", "Object类型参数");
sqlSession.insert("命名空间.insert标签ID", "Object类型参数");
Copy after login

Additions, deletions and modifications must submit transactions: sqlSession.commit();

Use When executing a method using the XML mapping file method, since the method is called through a string, the type constraints are not mandatory and the readability is poor. This was the calling method of early ibatis. In addition, Mybatis also provides two other implementation methods

MyBatis annotation method

1. @Select: used to modify the method of using query statements

2.@Insert : Used to modify the method of adding statements,

3, @Update: Used to modify the method of modifying.

4. @Delete: used to modify the method of using delete statement.

public interface EmployeeAnnotation {
@Select("select id,last_name AS lastName,email,gender from tal_employee")
List

 @Insert("insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})")
 int saveEmp(Employee employee);

 @Update("update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}")
 boolean updateEmp(Employee employee);

 @Delete("elete from tal_employee where id=#{id}")
 long deleteEmp(Integer id);
 }
Copy after login

Then we need to configure the mybatis main configuration file Mapping this annotation interface

<!-- 映射注解类 -->
<mapper class="com.simple.mybatis.dao.EmployeeAnnotation"/>

@Test
    public void testAnnotation(){
        SqlSession session = sqlSessionFactory.openSession();
        EmployeeAnnotation employeeAnnotation = session.getMapper(EmployeeAnnotation.class);
        Employee employee = new Employee("测试注解增加", "email", "男");
        employeeAnnotation.saveEmp(employee);
        //提交事务
        session.commit();
        session.close();
}
Copy after login

Although it is more convenient to use annotations, it is more complicated to configure complex sql statements. Therefore, a combination of the two methods is often used.

Mybatis interface programming

We first add an interface

public interface EmployeeMapper {
    List<Employee> findAll();
    
    int saveEmp(Employee employee);
    
    boolean updateEmp(Employee employee);
    
    long deleteEmp(Integer id);
}
Copy after login

The namespace (namespace) of the entity class mapping file must correspond to the full name of the interface, and the methods inside need to correspond to the additions in it Delete and modify the tag ID to complete the binding

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 该配置命名空间 -->
<mapper namespace="com.simple.mybatis.dao.EmployeeMapper">
    <resultMap type="com.simple.mybatis.entitys.Employee" id="Employee">
        <id property="id" column="id"/>
        <result property="lastName" column="last_name"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
    </resultMap>

    <!-- 返回外部resultMap格式的类型。-->
    <select id="findAll" resultMap="Employee">
        select id,last_name AS lastName,email,gender from tal_employee
    </select>
    
    <insert id="saveEmp" parameterType="com.simple.mybatis.entitys.Employee">
        insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})
    </insert>
    
    <update id="updateEmp" parameterType="com.simple.mybatis.entitys.Employee">
        update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
    </update>
    
    <delete id="deleteEmp" parameterType="Integer">
        delete from tal_employee where id=#{id}
    </delete>
</mapper>
Copy after login

And this mapping file must be loaded in the MyBatis main configuration file

<mappers>
        <mapper resource="com/simple/mybatis/entitys/EmployeeMapper.xml" />
</mappers>
Copy after login

Usage:

@Test
    public void test4(){
        SqlSession session = sqlSessionFactory.openSession();
        //获取接口。这个接口mybatis会帮我创建代理实现类完成接口与XML映射的绑定
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        employeeMapper.deleteEmp(1);
        session.commit();
        session.close();
    }
Copy after login

Get the automatically growing primary key after insertion

The useGeneratedKeys parameter only takes effect for the insert statement, and the default is false. When set to true, it means that if the inserted table has an auto-increment column as the primary key, JDBC is allowed to support automatic generation of the primary key, and the automatically generated primary key can be returned. keyProperty is to be encapsulated to the primary key ID attribute name in parameterType="com.simple.mybatis.entitys.Employee

<insert id="saveEmpGetKey" parameterType="com.simple.mybatis.entitys.Employee"
    useGeneratedKeys="true" keyProperty="id"
    >
        insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})
</insert>

@Test
    public void saveEmpGetKey(){
        SqlSession session = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee = new Employee("自动封装主键", "email", "男");//增加之前主键没赋值
        employeeMapper.saveEmpGetKey(employee);
        session.commit();//提交事务
        System.out.println("增加后主键有值: " + employee.getId());
        session.close();//关闭资源
    }
Copy after login

Mybatis parameter rules

When a parameter is used:

directly Just use #{xxx} to access the parameters. The parameter name is arbitrary. For example, #{a}, #{b} can access the parameter value. If it is a collection (Collection, List, Set) type, use the collection name in lowercase as the key value .

Required interface method:

long deleteEmp(Integer emp);
Copy after login

Mapping file:

<delete id="deleteEmp" parameterType="Integer">
        delete from tal_employee where id=#{id}
</delete>
Copy after login
Copy after login

You can see here that we have not assigned a value to this, #{id}My attribute name is also It is not called id but emp. So a parameter can have any name if it is the incoming collection type

The value when passing in the Map parameter:

//接口有方法:long deleteEmp(Map<String, Object> map);
<delete id="deleteEmp" parameterType="Map">
        delete from tal_employee where id=#{empID}
</delete>
Copy after login

The value here is empID, which Just think about it and you know that you can create a map.put("empID", 1) as a parameter and pass it into the method call.

When there are multiple parameters:

  1. Default: Just You cannot use #{xxx} to access parameters. When there are multiple parameters, mybatis will encapsulate the parameters into a map collection, and the value can only be obtained through #{subscript} or #{paramN}.

  2. Named parameters: Manually specify the key of the map when explicitly specifying the encapsulation parameters. Specify the parameter name by adding @Param("key") to the interface method.

  3. Entity class: If multiple parameters are encapsulated business classes, then just pass in the business object directly. You can get the attributes through #{property name}. Map: If multiple parameters are not encapsulated business classes , then just pass in the map collection. Get the corresponding value through #{key}.

{} and ${}

In MyBatis, in addition to using #{} to obtain the value of the parameter, you can also use ${} to obtain the value of the parameter.

Difference: #{}: The parameters will be generated into the sql statement in a pre-compiled manner. $ {}: will directly generate the value into the sql statement.

In most cases, we use #{} to obtain parameters, but in some places where placeholders are not supported, you can use ${ } to get parameters, such as table names.

resultType return type

  1. For methods of addition, deletion and modification, Mybatis will automatically encapsulate the results and return int, Boolean, long All are OK.

  2. For returning entity objects and List collections, resultType can be set to the entity type.

  3. If a single object is returned, it can also be encapsulated For Map, the resultType is set to map.

  4. If a Map collection object is returned, such as Map, the resultType is set to the Student type, and the key value placed in the Map is modified through the @MapKey("id") annotation .

ResultMap标签介绍

ResultMap的常用子标签

  1. id:映射主键。result:映射普通列。

  2. association:复杂结果映射。

  3. collection:复杂类型的集合映射。

  4. constructor:构造函数注入。

前面我们的案例我们数据库的字段与实体类的属性基本上一致,或者不一致使用取别名方案解决,可是每条语句都去取别名是非常麻烦的,ResultMap这个标签就很好的解决数据库与实体类字段不对应的问题

我们新创建一个部门表:

CREATE TABLE tal_dept(
    d_id INT PRIMARY KEY AUTO_INCREMENT,
    d_name VARCHAR(50)
);
Copy after login

对应实体类:

public class Dept {
    private Integer id;
    private Integer name;
}
Copy after login

如果我们查询语句直接使用resultType="com.simple.mybatis.entitys.Dept那么毫无疑问我们的实体类中名字不对应是赋值为null的,我们可以ResultMap映射实体类与数据库的对应关系

<!--配置返回结果,配置数据库字段与类属性的映射-->
<resultMap type="com.simple.mybatis.entitys.Dept" id="DeptResult">
        <id column="d_id" property="id"/>
        <result column="d_name" property="name"/>
</resultMap>
<!--使用resultMap使用指向上面返回结果的ID-->
<select id="getDeptOne" parameterType="Integer" resultMap="DeptResult">
        select * from tal_dept where d_id = #{id}
</select>
Copy after login

关系映射

在数据库中,许多数据是分布在多个表中的,有时候需要将多个表的数据关联起来进行查询。那么在ORM框架中,我们需要处理数据表的映射关系。

常见的映射关系:

  1. 关联属性映射association

  2. 映射collection映射

result方式映射属性

这种方式并不推荐,没有什么重用性

<resultMap type="com.simple.mybatis.entitys.Employee" id="EmployeeOrDept">
        <id property="id" column="id"/>
        <result property="lastName" column="last_name"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
        <!-- 指定Employee中Dept对象属性 -->
        <result property="dept.id" column="d_id"/>
        <result property="dept.name" column="d_name"/>
</resultMap>
<select id="getEmployeeOrDeptAll" resultMap="EmployeeOrDept">
        select * from tal_employee e inner join tal_dept d on e.d_id=d.d_id
</select>
Copy after login

association映射(分步查询)

多对一,查询员工获取员工中的部门:

<resultMap type="com.simple.mybatis.entitys.Employee" id="EmployeeOrDept">
        <id property="id" column="id"/>
        <result property="lastName" column="last_name"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
        <!-- property指定实体类中对象dept 指定发过去的参数column="d_id" 
             select指定com.simple.mybatis.dao.DeptMapper映射文件中getDeptOne查询方法
         -->
        <association property="dept" column="d_id" 
        select="com.simple.mybatis.dao.DeptMapper.getDeptOne"></association>
</resultMap>
<!--resultMap指定使用上面定义的返回结果-->
<select id="getEmployeeOrDeptAll" resultMap="EmployeeOrDept">
        select * from tal_employee e inner join tal_dept d on e.d_id=d.d_id
</select>
Copy after login

Collection查询

一对多,获取部门同时部门中员工也获取:

<!--部门映射-->
<resultMap type="com.simple.mybatis.entitys.Dept" id="DeptResult">
        <id column="d_id" property="id"/>
        <result column="d_name" property="name"/>
        <!--property指定Dept中集合属性名 ofType指定集合中属性类型 -->
        <collection property="Employees" ofType="com.simple.mybatis.entitys.Employee" >
            <id property="id" column="id"/>
            <result property="lastName" column="last_name"/>
            <result property="email" column="email"/>
            <result property="gender" column="gender"/>
        </collection>
</resultMap>
<select id="getDeptAll" resultMap="DeptResult">
        select * from tal_dept d left join tal_Employee e on d.d_id = e.d_id
</select>
Copy after login

Collection嵌套查询

一对多分布查询

<resultMap type="com.simple.mybatis.entitys.Dept" id="DeptOrEmployee">
        <id column="d_id" property="id"/>
        <result column="d_name" property="name"/>
        <!--select指定com.simple.mybatis.dao.EmployeeMapper映射文件中的getEmployeeByDeptId-->
        <collection property="Employees" column="d_id"  ofType="com.simple.mybatis.entitys.Employee" 
        select="com.simple.mybatis.dao.EmployeeMapper.getEmployeeByDeptId">
        </collection>
    </resultMap>
<!--com.simple.mybatis.dao.EmployeeMapperXML中方法-->
<select id="getEmployeeByDeptId" parameterType="Integer" resultType="com.simple.mybatis.entitys.Employee">
        select * from tal_employee where d_id = #{id}
    </select>
Copy after login

关联查询和分步查询的区别:

  1. 关联查询一次将多个表的数据查询出来,分步查询通过多次查询获取查询结果。

  2. 配置文件不同,关联查询需要定义额外的映射,分步查询需要定义外键列,和查询的select方法。

  3. 关联查询不支持延迟加载,分步查询支持延迟加载。fetchType="lazy"

延迟加载

刚才分布查询是一次将结果查询出来,为了提高效率,mybatis还支持延迟加载技术,等需要用到对象时才进行查询。

在mybatis主配置文件中配置:

<!– 通过全局配置文件设置延迟加载-->
<settings>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
Copy after login

设置级联延迟加载

<!– 设置延迟加载属性-->
<association fetchType="lazy"/>
Copy after login

The above is the detailed content of Detailed explanation of Mybatis mapping files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template