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">
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; } }
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); }
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};
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:
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})
同理,如果我们在使用mysql的时候,想在数据插入后返回插入的id, 我们也可以使用 selectKey 这个元素:
SELECT LAST_INSERT_ID() as id insert into user(id, name, password, age, deleteFlag) values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})
这儿,我们就简单提一下
keyProperty="id" resultType="int" order="BEFORE" statementType="PREPARED">
以上就是深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete的内容,更多相关内容请关注PHP中文网(www.php.cn)!