Previous chapter: mybatis.003 using the properties file!
Next chapter:
Where can I configure data insertion?
Corresponding to the pojo objectMapperConfigure in the configuration file
Use<Insert/>Construct tag names
For example, the following code
Location: CC/fozone/demo/mybatis/config/mapper/user. Mapper. xml
<?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="cc.fozone.demo.mybatis.controller.IUserController"> ...<insert id="insertUser" parameterType="cc.fozone.demo.mybatis.pojo.User" statementType="PREPARED" flushCache="true" keyProperty="id"> <![CDATA[ insert into users (name,age,phone) values (#{name},#{age},#{phone}) ]]></insert>...</mapper>
What are the meanings of these parameters?
Attribute |
Required |
Description |
ID |
Yes |
Globally Unique Identifier, representing this operation |
Parametertype
|
No |
Parameter type. It can be a basic data class, such as Int or string, or a class. |
Statementtype |
No |
Statement, prepared (default) or callable Mybatis selects statement, preparedstatement, or callablestatement. |
Flushcache |
No |
Default Value: false. Indicates whether to clear the Cache during execution. |
Keyproperty
|
No |
It is mainly used for setting fields that automatically generate auto-increment primary keys. It is particularly useful for databases that do not support auto-increment.
Default: NULL Note: Only applicable to insert and update |
Keycolumn |
No |
Same as above, but mainly set keyproperty |
Usegeneratedkeys |
No |
Return the primary key generated by the database using the getgeneratedkeys method of the JDBC driver. Default Value: false Note: Only applicable to insert and update |
How to parse and execute parametertype?
<insert id="insertUser" parameterType="cc.fozone.demo.mybatis.pojo.User" ... > <![CDATA[ insert into users (name,age,phone) values (#{name},#{age},#{phone}) ]]></insert>
When executing the insert statement, you need to input a classCc. fozone. Demo. mybatis. pojo. UserObject;
During parsing# {String}Through the passed object, call the correspondingGetterMethod value replacement,
For example:# {Name}YesUser. getname ()Value Replacement,# {Age}Will beUser. getage ()Value replacement;
How to add a user to the iusercontroller Interface
Location: CC/fozone/demo/mybatis/controller/iusercontroller. Java
Package CC. fozone. demo. mybatis. controller; import Java. util. list; import CC. fozone. demo. mybatis. pojo. user; public interface iusercontroller {... // Add User Information Public void insertuser (User user );}
How to insert a user?
Location: CC/fozone/demo/mybatis/APP. Java
Package CC. fozone. demo. mybatis ;... public class app {... // SQL session factory private sqlsessionfactory factory ;... public void insertuser (){...}}
1. execution using interfaces (recommended)
Public void insertuser () {user = new user (); User. setname ("user"); User. setage (100); User. setphone ("13812341234"); // open the session sqlsession session = factory. opensession (); // get the instantiation interface through getmapper // Why can this be instantiated? The focus is on the namespace configuration iusercontroller = session configured by mapper. getmapper (iusercontroller. class); // insert data controller. insertuser (User); // submit and execute the SQL session. commit (); // close the session. close ();}
2. Session-based execution
Public void insertuser () {user = new user (); User. setname ("user"); User. setage (100); User. setphone ("13812341234"); // open the session sqlsession session = factory. opensession (); // insert data session through session. insert ("insertuser", user); // submit and execute the SQL session. commit (); // close the session. close ();
Test Results
1. Compile the execution function
public class App { ... public static void main(String[] args) { App app = new App(); app.insertUser(); } ...}
2. After execution, check whether the database users table data is inserted.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/49/F7/wKiom1QgPJayzcNdAABU43N3rjo116.jpg "Title =" 008.png" alt = "wkiom1qgpjayzcndaabu43n3rjo116.jpg"/>
Previous chapter: mybatis.003 using the properties file!
Next chapter:
This article from "Bo yuan to Jing" blog, please be sure to keep this source http://sunspot.blog.51cto.com/372554/1557107
[Crazy guy] mybatis.004 is the first one to be inserted!