Home > Java > Java Tutorial > body text

An in-depth explanation of the Mybatis series (7)---insert, update, delete of mapper mapping file configuration

黄舟
Release: 2017-03-02 10:53:52
Original
1488 people have browsed it

The previous article "In-depth introduction to Mybatis series (6)---Introduction and configuration of objectFactory, plugins, mappers" simply concluded the configuration of mybatis. So starting from this article, we will introduce the configuration of the mapper mapping file. This is one of the cores of mybatis, and you must learn it well. In the mapper file, with mapper as the root node, the element nodes that can be configured below it are: select, insert, update, delete, cache, cache-ref, resultMap, sql.

This article will briefly introduce the configuration and use of insert, update, delete, and will provide an in-depth explanation of the source code of mybatis in the future.

I believe that when we see insert, update, and delete, we will know its function. As the name suggests, myabtis, as a persistence layer framework, must handle CRUD.

Okay, let’s first take a look at how to configure insert, update, delete and what elements can be configured:

    

    
      
      id="insertUser"      
      
      
      parameterType="com.demo.User"      
      
      
      flushCache="true"      
      
      
      statementType="PREPARED"      
      
      
      keyProperty=""      
      
      
      keyColumn=""      
      
      
      useGeneratedKeys="false"      
      
      timeout="20">    

    
Copy after login


The above is a template configuration , which ones are necessary configurations and which ones are based on your actual needs, you can know at a glance.

Next, let’s use the demo in the first article "In-depth introduction to Mybatis series (1)---Getting started with Mybatis" as an example:

Database (user table):

My project structure:

User.java:

package com.dy.entity;public class User {    
private int id;    
private String name;    
private String password;    
private int age;    
private int deleteFlag;    
    public int getId() {        
    return id;
    }   
     public void setId(int id) {        
    this.id = id;
    }    
    public String getName() {        
    return name;
    }    
    public void setName(String name) {        
    this.name = name;
    }    
    public String getPassword() {        
    return password;
    }    
    public void setPassword(String password) {        
    this.password = password;
    }   
     public int getAge() {        
    return age;
    }    
    public void setAge(int age) {        
    this.age = age;
    }    
    public int getDeleteFlag() {        
    return deleteFlag;
    }    
    public void setDeleteFlag(int deleteFlag) {        
    this.deleteFlag = deleteFlag;
    }
    
}
Copy after login

UserDao.java:

package com.dy.dao;import com.dy.entity.User;public interface UserDao {    
public void insertUser (User user);    
    public void updateUser (User user);    
    public void deleteUser (User user);
    
}
Copy after login

userDao.xml:

    
   
   
   
           insert into user(id, name, password, age, deleteFlag) 
               values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})   
   
   
   
           update user set name = #{name}, 
           password = #{password}, age = #{age}, deleteFlag = #{deleteFlag}
               where id = #{id};   
    
    
   
           delete from user where id = #{id};   
Copy after login


In this way, a simple mapping relationship is established. Carefully observe the above parameterType, "com.dy.entity.User". If the package name is longer, it will be written like this every time, and it will be painful to write. Don't forget the typeAliases (aliases) mentioned before. So, using aliases in this place, wouldn't it mean that the skill says goodbye to the painfully long package name? Okay, let’s match the alias, where should we match it? Of course, it is in the global configuration file of mybatis (my name here is mybatis-conf.xml). Don't think that it is configured in the mapper configuration file.

mybatis-conf.xml:


      
      
  
Copy after login


In this way, an alias is created. We can add all the above com.dy.entity.User Directly changed to user. How convenient this is!

My database here uses mysql. I set the primary key id of the user table to automatically grow. The above code runs normally, so here comes the problem (of course, I am not asking which excavator company Strong), what if I switch to Oracle database? Oracle does not support self-increment of IDs? what to do? Please see below:



   
              
           insert into user(id, name, password, age, deleteFlag) 
               values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})   
Copy after login


同理,如果我们在使用mysql的时候,想在数据插入后返回插入的id, 我们也可以使用 selectKey 这个元素



   
            
        
        
        
               SELECT LAST_INSERT_ID() as id           
          
           insert into user(id, name, password, age, deleteFlag) 
               values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})   
Copy after login

这儿,我们就简单提一下 这个元素节点吧:


        keyProperty="id"        
        
        resultType="int"        
        
        order="BEFORE"        
        
        statementType="PREPARED">
Copy after login

好啦,本篇文章主要介绍了insert, update, delete的配置及用法。 下篇文章将介绍复杂的 select相关的配置及用法, 待这些都讲完后,会先根据源码分析一下mybatis的整个运行流程,然后再深入mybatis的用法。

 以上就是深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!