Home  >  Article  >  Java  >  Detailed explanation of Mybatis mapping files

Detailed explanation of Mybatis mapping files

不言
不言forward
2018-10-12 14:50:554030browse

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

insert: mapping insertion statement

Use #{parameter attribute name} to assign value


        insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})

: Mapping modification statement


        update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
 

: Mapping deletion statement


        delete from tal_employee where id=#{id}

: 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.


    
    
    
    
 

: 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类型参数");

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

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




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

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 findAll();
    
    int saveEmp(Employee employee);
    
    boolean updateEmp(Employee employee);
    
    long deleteEmp(Integer id);
}

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





    
        
        
        
        
    

    
    
    
    
        insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})
    
    
    
        update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
    
    
    
        delete from tal_employee where id=#{id}
    

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


        

Usage:

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

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 into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})


@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();//关闭资源
    }

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

Mapping file:


        delete from tal_employee where id=#{id}

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 map);

        delete from tal_employee where id=#{empID}

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 Map1384ae87b3aa4d7618d5d6d4b582a21b, the resultType is set to map.

  4. If a Map collection object is returned, such as Mapbfb6431144afb6dca75c307d25533a56, 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)
);

对应实体类:

public class Dept {
    private Integer id;
    private Integer name;
}

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



        
        


关系映射

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

常见的映射关系:

  1. 关联属性映射association

  2. 映射collection映射

result方式映射属性

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


        
        
        
        
        
        
        

association映射(分步查询)

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


        
        
        
        
        
        


Collection查询

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



        
        
        
        
            
            
            
            
        

Collection嵌套查询

一对多分布查询


        
        
        
        
        
    

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

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

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

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

延迟加载

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

在mybatis主配置文件中配置:







设置级联延迟加载


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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete