上一節說了Mybatis的框架搭建和簡單查詢,這次我們來說一說用Mybatis進行基本的增刪改操作;
一、 插入一條數據
1、首先編寫USER.XML(表的xml)使用insert元素,元素寫在mapper中:
1
2
3
|
<insert id= "insertitem" parameterType= "cn.qkp.po.user" > INSERT INTO user(username,birthday,sex,address) VALUES(#{username},#{ birthday},#{sex},#{address}) </insert> |
注意:這里的parameterType使用了實體對象的類型。因為#{}和${}可以接收pojo數據,可以使用OGNL解析出pojo的屬性值;如:#{username}為獲取pojo中username的屬性值,切記使用實體對象類型,在大括號中要保持數據一致;
2、在運行java文件中的代碼(MybatisFrist.java中):
1
2
3
4
5
6
7
8
9
10
11
|
@Test public void start3(){ SqlSession session = sqlsessionfactory.openSession(); //打開SqlSession User user = new User(); //創建實體對象 user.setUsername( "mylydg" ); user.setSex( "1" ); user.setBirthday( new Date()); session.insert( "test.insertUser" , user); //執行插入 session.commit(); //提交事務 session.close(); //關閉連接 } |
以上便是基本的數據插入操作,使用對象來操作Mybatis進行插入;
PS:拓展(如何獲得插入數據的主鍵返回)?
看如下代碼操作:
1.1、在insert元素中使用selectKey元素
1
2
3
4
5
6
7
8
9
|
<insert id= "insertitem" parameterType= "cn.qkp.pojo.user" > <!-- 1 、語句select LAST_INSERT_ID()為查找最后插入元素的id 2 、order屬性表示相對insert語句在什么時候執行,有兩個參數 "AFTER" 之后和 "BEFORE" 之前 3 、resultType屬性表示返回值的類型 4 、keyProperty表示返回到的屬性,切記要與insert中的parameterType類型的值相同,它會把值返回給parameterType對象 --> <selectKey order= "AFTER" resultType= "int" keyProperty= "id" > select LAST_INSERT_ID() </selectKey> |
1.2、在運行java文件中的代碼(MybatisFrist.java中)直接通過之前傳入的對象的user.Id獲得即可
1
2
3
4
5
6
7
8
9
10
11
12
|
@Test public void start3(){ SqlSession session = sqlsessionfactory.openSession(); //打開SqlSession User user = new User(); //創建實體對象 user.setUsername( "mylydg" ); user.setSex( "1" ); user.setBirthday( new Date()); session.insert( "test.insertUser" , user); //執行插入 session.commit(); //提交事務 session.close(); //關閉連接 System.out.println( "the id =" +user.getId()); //獲得主鍵并打印 } |
二、更新數據操作
1、在表xml(user.xml,寫在mapper中):
1
2
3
|
<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、在運行java文件中的代碼(MybatisFrist.java中):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Test public void update(){ //通過工廠類打開數據接口 SqlSession sqlsession = sqlsessionfactory.openSession(); //設置接收對象 User user = new User();; user.setUsername( "mylydg" ); user.setSex( "1" ); user.setBirthday( new Date()); user.setAddress( "the address" ); user.setId( 27 ); try { //查詢數據selectOne為查詢一條的方法第一個參數是user.xml中的namespace.id;第二個參數是user配置文件中的#{id} sqlsession.update( "test.update" , user); sqlsession.commit(); } catch (Exception e) { // TODO: handle exception } finally { sqlsession.close(); //讀完要關閉sqlsession } System.out.println( "the id =" +user.getId()); //打印輸出 } |
運行方法后,則可以更新指定id的一條數據
三、刪除一條數據的操作
1、在表xml文件(user.xml,寫在mapper中):
1
2
3
|
<delete id= "delete" parameterType= "int" > delete from user where id = #{id} </delete> |
2、在運行java文件中的代碼(MybatisFrist.java中):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Test public void delete(){ //通過工廠類打開數據接口 SqlSession sqlsession = sqlsessionfactory.openSession(); try { //查詢數據selectOne為查詢一條的方法第一個參數是user.xml中的namespace.id;第二個參數是user配置文件中的#{id} sqlsession.delete( "test.delete" , 27 ); sqlsession.commit(); } catch (Exception e) { // TODO: handle exception } finally { sqlsession.close(); //讀完要關閉sqlsession } |
運行方法后便可以刪除指定id的一條數據。
以上所述是小編給大家介紹的Mybatis入門教程之新增、更新、刪除功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/mylydg/archive/2017/02/21/6423863.html