JAVA-Mybatis basics 2. javamybatis Basics

Source: Internet
Author: User

JAVA-Mybatis basics 2. javamybatis Basics

The previous section describes the Mybatis framework setup and simple query. This time, we will talk about using Mybatis for basic addition, deletion, and modification operations;

1. insert a piece of data. 1. First, write USER. XML (Table xml) and use the insert element. The element is written in mapper:
<insert id="insertitem" parameterType="cn.qkp.po.user">INSERT INTO user(username,birthday,sex,address) VALUES(#{username},#{ birthday},#{sex},#{address})</insert>

Note: The parameterType here uses the object type. Because # {} and $ {} can receive pojo data, you can use OGNL to parse the attribute value of pojo. For example, # {username} is used to obtain the attribute value of username in pojo, remember to use the object type to ensure data consistency in braces;

2. Code in the running java file (in MybatisFrist. java ):
@ Test public void start3 () {SqlSession session = sqlsessionfactory. openSession (); // open SqlSession User user = new User (); // create Object user. setUsername ("mylydg"); user. setSex ("1"); user. setBirthday (new Date (); session. insert ("test. insertUser ", user); // execute the insert session. commit (); // submit the transaction session. close (); // close the connection}

The above is the basic data insertion operation, using objects to operate on Mybatis for insertion;

PS: expansion (how to obtain the primary key returned for data insertion )?

See the following code:

1.1. Use the selectKey element in the insert element.
<Insert id = "insertitem" parameterType = "cn. qkp. pojo. user"> <! -- 1. The select LAST_INSERT_ID () statement is the id of the last inserted element. 2. The order attribute indicates when the insert statement is executed, there are two parameters: "AFTER" and "BEFORE". 3. The resultType attribute indicates the type of the returned value. 4. The keyProperty indicates the returned property. Remember to use the same value as the parameterType value in the insert statement, it returns the value to the parameterType object --> <selectKey order = "AFTER" resultType = "int" keyProperty = "id"> select LAST_INSERT_ID () </selectKey>
1.2. The code in the running java file (in MybatisFrist. java) is obtained directly through the user. Id of the previously passed object.
@ Test public void start3 () {SqlSession session = sqlsessionfactory. openSession (); // open SqlSession
User user = new User (); // create Object user. setUsername ("mylydg"); user. setSex ("1"); user. setBirthday (new Date ());
Session. insert ("test. insertUser ", user); // execute the insert session. commit (); // submit the transaction session. close (); // close the System connection. out. println ("the id =" + user. getId (); // obtain the primary key and print it}
Ii. Data Update operation 1. In table xml (user. xml, written in mapper ):
    <update id="update" parameterType="cn.qkp.mybatis.po.User">    update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} where id=#{id}    </update>
2. Code in the running java file (in MybatisFrist. java ):
@ Test public void update () {// use the factory class to open the data interface SqlSession sqlsession = sqlsessionfactory. openSession (); // set the Recipient User user = new User (); user. setUsername ("mylydg"); user. setSex ("1"); user. setBirthday (new Date (); user. setAddress ("the address"); user. setId (27); try {// query data selectOne is to query a method. The first parameter is user. namespace in xml. id; the second parameter is # {id} sqlsession in the user configuration file. update ("test. update ", user); sqlsession. commit ();} catch (Exception e) {// TODO: handle exception} finally {sqlsession. close (); // close sqlsession} System after reading. out. println ("the id =" + user. getId (); // print the output}

After running the method, you can update a data entry with the specified id.

3. delete a piece of data. 1. In the table xml file (user. xml, written in mapper ):
    <delete id="delete" parameterType="int">    delete from user where id = #{id}    </delete>
2. Code in the running java file (in MybatisFrist. java ):
@ Test public void delete () {// use the factory class to open the data interface SqlSession sqlsession = sqlsessionfactory. openSession (); try {// query data selectOne is to query a method. The first parameter is user. namespace in xml. id; the second parameter is # {id} sqlsession in the user configuration file. delete ("test. delete ", 27); sqlsession. commit ();} catch (Exception e) {// TODO: handle exception} finally {sqlsession. close (); // close sqlsession after reading}

After running the method, you can delete a piece of data with the specified id.

The above is the simple addition, deletion, and modification operation code of Mybatis. Thank you!

 

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.