MyBatis Mapper Mapping file configuration insert, UPDATE, delete

Source: Internet
Author: User

This article will briefly introduce the INSERT, UPDATE, delete configuration and use, in the future will be the source of mybatis in-depth explanation.

Believe, see INSERT, UPDATE, Delete, we know its role, as the name implies, Myabtis as a persistent layer framework, must be to crud AH.

OK, let's take a look at INSERT, UPDATE, delete How to configure, which elements can be configured:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//ibatis.apache.org//dtd mapper 3.0//en" "Http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd" & 

Gt <!--mapper is the root element node, and one namespace corresponds to one DAO--<mapper namespace= "Com.dy.dao.UserDao" > <insert <! --1. 
        The ID (must be configured) ID is a unique identifier in the namespace that can be used to represent the statement.
      
      A namespace (namespace) corresponds to a DAO interface, which should also correspond to a method within DAO (equivalent to the implementation of a method), so the ID should match the method name--id= "Insertuser" <!--2. ParameterType (optional configuration, default MyBatis automatic selection Processing) The fully qualified class name or alias of the parameter that will pass in the statement, and if it is not configured, MyBatis will select the appropriate Typehand by default based on the parameter type Parameterhandler Ler processing ParameterType primarily specifies parameter types, either int, short, long, string, or complex type (such as Object)--and parametertype= "com . Demo. User "<!--3. Flushcache (optional configuration, default configured to TRUE) sets it to true, which causes both the local cache and the level two cache to be emptied whenever the statement is invoked, with the default value: True (corresponding to insert, UPDATE, and DELETE statements)--F Lushcache= "true" <!--4. StatementType (optional configuration, default configuration is prepared) STATEMENT, one of PREPARED or callable. This allows MyBatis to use statement,preparedstatement or CallableStatement, respectively, with the default value: PREPARED. --Statementtype= "PREPARED" <!--5. Keyproperty (optional configuration, default unset), which is only useful for insert and update, uniquely marks an attribute, MyBatis either through Getgeneratedkeys return value or through the INSERT statement Sele The Ctkey child element sets its key value, by default: unset. If you want to get more than one generated column, you can also be a comma-delimited list of property names. --Keyproperty= "" <!--6. KeyColumn (optional configuration) (useful for INSERT and update only) sets the column name in the table by the generated key value, which is required only for certain databases (like PostgreSQL), which need to be set when the primary key column is not the first column in the table. If you want to get more than one generated column, you can also be a comma-delimited list of property names. --Keycolumn= "" <!--7. Usegeneratedkeys (optional configuration, default = False) (only useful for insert and update) This causes MyBatis to use the JDBC Getgeneratedkeys method to remove the primary key generated by the database (  For example, the automatic increment field for a relational database management system such as MySQL and SQL Server), the default value: False. --usegeneratedkeys= "false" <!--8. Timeout (optional configuration, default unset, dependent driver) This setting is the number of seconds that the driver waits for the database to return request results before throwing an exception. The default value is unset (dependent driver).
    -timeout= "> <update"  Id= "UpdateUser" parametertype= "Com.demo.User" flushcache= "true" statementtype= "PREPARED" timeout = "> <delete id=" deleteuser "parametertype=" Com.demo.User "flushcache=" true "statemen Ttype= "PREPARED" timeout= "> </mapper>

Above is a template configuration, which is the necessary configuration, which is based on their actual needs, a glance to know.

Below, or use the first article, "MyBatis series (a)---mybatis introduction" in the demo to sample it:

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.) {
		this.age = age;
	}
	public int Getdeleteflag () {
		return deleteflag;
	}
	public void Setdeleteflag (int deleteflag) {
		this.deleteflag = Deleteflag;
	}
	
}
View Code

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

Userdao.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <!   
DOCTYPE Mapper Public   
"-//ibatis.apache.org//dtd mapper 3.0//en"  
"http://ibatis.apache.org/dtd/ Ibatis-3-mapper.dtd "> 

<mapper namespace=" Com.dy.dao.UserDao ">
   
   <!--Userdao method in the corresponding Insertuser,  -
   <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.