MyBatis學習筆記之二--關聯關係(一對一和一對多)
首先給大家推薦幾個網頁:
http://blog.csdn.net/isea533/article/category/2092001 沒事看看 - MyBatis工具:www.mybatis.tk
http://www.mybatis.org/mybatis-3/zh/getting-started.html 入門
http://edu.51cto.com/course/course_id-1354.html mybatis視訊教程
今天主要學習的關聯關係是一對一關係與一對多關係。
一、一對一關係
還是通過例子來解釋說明。(一個妻子對應一個丈夫)。
1)資料庫資訊
1 create table t_wife( 2 id int primary key auto_increment, 3 wife_name varchar(20), 4 fk_husband_id int 5 ); 6 7 create table t_husband( 8 id int primary key auto_increment, 9 husband_name varchar(20) 10 ); 11 12 insert into t_husband values (null,'hello'); 13 insert into t_wife values(null,'kitty',1)
2)對應的JavaBean程式碼
雖然在資料庫裡只有一方配置的外來鍵,但是這個一對一是雙向的關係。
HusbandBean.java
1 package com.cy.mybatis.beans; 2 3 import java.io.Serializable; 4 /** 5 * one to one 6 * @author acer 7 * 8 */ 9 public class HusbandBean implements Serializable{ 10 11 12 private static final long serialVersionUID = 1L; 13 14 private Integer id; 15 private String name; 16 private WifeBean wife; 17 public HusbandBean() { 18 super(); 19 } 20 public HusbandBean(Integer id, String name, WifeBean wife) { 21 super(); 22 this.id = id; 23 this.name = name; 24 this.wife = wife; 25 } 26 public Integer getId() { 27 return id; 28 } 29 public void setId(Integer id) { 30 this.id = id; 31 } 32 public String getName() { 33 return name; 34 } 35 public void setName(String name) { 36 this.name = name; 37 } 38 public WifeBean getWife() { 39 return wife; 40 } 41 public void setWife(WifeBean wife) { 42 this.wife = wife; 43 } 44 @Override 45 public String toString() { 46 return "Husband [id=" + id + ", name=" + name + ", wife=" + wife + "]"; 47 } 48 49 50 51 }
WifeBean.java
1 package com.cy.mybatis.beans; 2 3 import java.io.Serializable; 4 /** 5 * one to one 6 * @author acer 7 * 8 */ 9 public class WifeBean implements Serializable{ 10 11 private static final long serialVersionUID = 1L; 12 private Integer id; 13 private String name; 14 private HusbandBean husband; 15 public WifeBean() { 16 super(); 17 } 18 public WifeBean(Integer id, String name, HusbandBean husband) { 19 super(); 20 this.id = id; 21 this.name = name; 22 this.husband = husband; 23 } 24 public Integer getId() { 25 return id; 26 } 27 public void setId(Integer id) { 28 this.id = id; 29 } 30 public String getName() { 31 return name; 32 } 33 public void setName(String name) { 34 this.name = name; 35 } 36 public HusbandBean getHusband() { 37 return husband; 38 } 39 public void setHusband(HusbandBean husband) { 40 this.husband = husband; 41 } 42 @Override 43 public String toString() { 44 return "Wife [id=" + id + ", name=" + name + ", husband=" + husband 45 + "]"; 46 } 47 48 49 }
3)接下來建立兩個介面,HusbandMapper,WifeMapper.
HusbandMapper
1 package com.cy.mybatis.mapper; 2 3 import com.cy.mybatis.beans.HusbandBean; 4 5 public interface HusbandMapper { 6 /** 7 * 根據id查詢丈夫資訊 8 * @param id 9 * @return 10 * @throws Exception 11 */ 12 public HusbandBean selectHusbandById (int id) throws Exception; 13 14 /** 15 * 根據id查詢丈夫與妻子資訊 16 * @param id 17 * @return 18 * @throws Exception 19 */ 20 public HusbandBean selectHusbandAndWife(int id) throws Exception; 21 22 }
4)定義HusbandMapper.xml檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.cy.mybatis.mapper.HusbandMapper"> 4 5 <resultMap type="HusbandBean" id="husbandAndWife"> 6 <id property="id" column="id" javaType="java.lang.Integer"/> 7 <result property="name" column="name" javaType="java.lang.String"/> 8 9 <!-- association – 一個複雜的型別關聯;許多結果將包成這種型別 10 嵌入結果對映 – 結果對映自身的關聯,或者參考一個 11 column="id" 這裡的id指的是在t_wife表來的主鍵id 12 這個查詢妻子,所以在妻子mapper裡有個方法 --> 13 <association property="wife" column="id" javaType="WifeBean" select="com.cy.mybatis.mapper.WifeMapper.selectWifeByHusbandId" ></association> 14 </resultMap> 15 16 <!-- resultType 返回型別 從這條語句中返回的期望型別的類的完全限定名或別名 17 。--> 18 <select id="selectHusbandById" resultType="HusbandBean"> 19 select * from t_husband where id=#{id} 20 </select> 21 22 <!-- resultMap 命名引用外部的 resultMap。返回的是一個集合。--> 23 <select id="selectHusbandAndWife" resultMap="husbandAndWife"> 24 select * from t_husband where id=#{id} 25 </select> 26 27 28 29 30 </mapper>
在WifeMapper.xml裡有個方法
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <mapper namespace="com.cy.mybatis.mapper.WifeMapper"> 5 <select id="selectWifeByHusbandId" resultType="WifeBean"> 6 select * from t_wife where fk_husband_id = #{id} 7 8 </select> 9 </mapper>
5)寫個實現
1 package com.cy.mybatis.service; 2 3 import org.apache.ibatis.session.SqlSession; 4 5 import com.cy.mybatis.beans.HusbandBean; 6 import com.cy.mybatis.mapper.HusbandMapper; 7 import com.cy.mybatis.tools.DBTools; 8 9 10 11 12 public class OneToOneService { 13 14 public static void main(String[] args) { 15 selectHusbandAndWife(); 16 17 } 18 19 20 private static void selectHusbandAndWife() { 21 SqlSession session = DBTools.getSession(); 22 HusbandMapper hm = session.getMapper(HusbandMapper.class); 23 try { 24 HusbandBean husband = hm.selectHusbandAndWife(1); 25 System.out.println(husband); 26 session.commit(); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 } 31 32 }
注意:那個工具類還是前一章那樣寫的,就相當與在昨天的基礎上建立的。
注意:
mybatis實際是對XML進行操作,我們所有的方法都直接定義在XML中,寫個介面只是為了更好的符合我們3層的思想,如果不寫介面,直接通過session也可以直接操作xml中的方法 ,
XML中只要有方法,就可以使用,而呼叫的方式就是:namespace+方法名;
例外使用resultType時,一定要保證,你屬性名與欄位名相同;
如果不相同,就使用resultMap 。
二、一對多關係
還是通過例子來解釋說明。(一把鎖對應多把鑰匙)。
2.1)資料庫資訊 這裡沒有新增資料了,我們用批量新增資料
1 create table t_key( 2 id int primary key auto_increment, 3 key_name varchar(20), 4 fk_lock_id int 5 ); 6 7 8 create table t_lock( 9 id int primary key auto_increment, 10 lock_name varchar(20) 11 );
2.2) 實體類
KeyBean.java
1 package com.cy.mybatis.beans; 2 3 import java.io.Serializable; 4 /** 5 * manyTOone 6 * 7 * 8 */ 9 public class KeyBean implements Serializable { 10 11 12 private static final long serialVersionUID = 3712545874604618746L; 13 14 private Integer id; 15 private String key; 16 17 private LockBean lock; 18 19 public KeyBean() { 20 super(); 21 } 22 23 public KeyBean(Integer id, String key, LockBean lock) { 24 super(); 25 this.id = id; 26 this.key = key; 27 this.lock = lock; 28 } 29 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getKey() { 39 return key; 40 } 41 42 public void setKey(String key) { 43 this.key = key; 44 } 45 46 public LockBean getLock() { 47 return lock; 48 } 49 50 public void setLock(LockBean lock) { 51 this.lock = lock; 52 } 53 54 @Override 55 public String toString() { 56 return "KeyBean [id=" + id + ", key=" + key + ", lock=" + lock + "]"; 57 } 58 59 }
LockBean.java
1 package com.cy.mybatis.beans; 2 3 import java.io.Serializable; 4 import java.util.List; 5 /** 6 * oneTOmany 7 * 8 * 9 */ 10 public class LockBean implements Serializable{ 11 12 private static final long serialVersionUID = 7092410462131162665L; 13 14 private Integer id; 15 private String lock; 16 17 private List<KeyBean> keys; 18 19 public LockBean() { 20 super(); 21 } 22 23 public LockBean(Integer id, String lock, List<KeyBean> keys) { 24 super(); 25 this.id = id; 26 this.lock = lock; 27 this.keys = keys; 28 } 29 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getLock() { 39 return lock; 40 } 41 42 public void setLock(String lock) { 43 this.lock = lock; 44 } 45 46 public List<KeyBean> getKeys() { 47 return keys; 48 } 49 50 public void setKeys(List<KeyBean> keys) { 51 this.keys = keys; 52 } 53 54 @Override 55 public String toString() { 56 return "LockBean [id=" + id + ", keys=" + keys + ", lock=" + lock + "]"; 57 } 58 59 }
2.3) 建立介面
KeyMapper.java
1 package com.cy.mybatis.mapper; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Param; 6 7 import com.cy.mybatis.beans.KeyBean; 8 9 public interface KeyMapper { 10 /** 11 * 批量新增鑰匙 12 * @return 13 * 提倡 這樣使用 @Param("keys") 14 */ 15 public int batchSaveKeys(@Param("keys")List<KeyBean> keys); 16 }
LockMapper.java
1 package com.cy.mybatis.mapper; 2 3 import org.apache.ibatis.annotations.Param; 4 5 import com.cy.mybatis.beans.LockBean; 6 7 public interface LockMapper { 8 /** 9 * 新增鎖 10 * @param lock 11 * @return 12 */ 13 public int saveLock(@Param("lock")LockBean lock); 14 15 /** 16 * 根據ID查詢鎖的資料 17 * @param id 18 * @return 19 */ 20 public LockBean findLockById(int id); 21 22 /** 23 * 根據ID查詢鎖與鑰匙的資料 24 * one2many 25 * @param id 26 * @return 27 */ 28 public LockBean findLockAndKeys(int id); 29 30 }
2.4) 建立xml檔案
KeyMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.cy.mybatis.mapper.KeyMapper"> 4 5 <resultMap id="keyMap" type="KeyBean"> 6 <id property="id" column="id" javaType="java.lang.Integer"/> 7 <result property="key" column="key_name" javaType="java.lang.String"/> 8 </resultMap> 9 10 11 <!--collection 為用於遍歷的元素(必選),支援陣列、List、Set --> 12 <!-- item 表示集合中每一個元素進行迭代時的別名. --> 13 <!--separator表示在每次進行迭代之間以什麼符號作為分隔 符. --> 14 <insert id="batchSaveKeys"> 15 insert into t_key values 16 <foreach collection="keys" item="key" separator=","> 17 (null,#{key.key},#{key.lock.id}) 18 </foreach> 19 </insert> 20 21 <select id="findKeysByLockId" resultMap="keyMap"> 22 select * from t_key where fk_lock_id = #{id} 23 </select> 24 25 </mapper>
LockMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.cy.mybatis.mapper.LockMapper"> 4 <!--自定義返回型別 --> 5 <resultMap id="lockMap" type="LockBean"> 6 <id property="id" column="id" javaType="java.lang.Integer"/> 7 <result property="lock" column="lock_name" javaType="java.lang.String"/> 8 </resultMap> 9 10 <!--自定義返回型別 --> 11 <resultMap id="lockAndKeysMap" type="LockBean"> 12 <id property="id" column="id" javaType="java.lang.Integer"/> 13 <result property="lock" column="lock_name" javaType="java.lang.String"/> 14 15 <collection property="keys" column="id" select="com.cy.mybatis.mapper.KeyMapper.findKeysByLockId"></collection> 16 </resultMap> 17 18 <insert id="saveLock"> 19 insert into t_lock values (null,#{lock.lock}) 20 </insert> 21 22 <select id="findLockById" resultMap="lockMap"> 23 select * from t_lock where id= #{id} 24 </select> 25 26 <select id="findLockAndKeys" resultMap="lockAndKeysMap"> 27 select * from t_lock where id= #{id} 28 </select> 29 30 </mapper>
2.5 ) 實現
1 package com.cy.mybatis.service; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.ibatis.session.SqlSession; 7 8 import com.cy.mybatis.beans.KeyBean; 9 import com.cy.mybatis.beans.LockBean; 10 import com.cy.mybatis.mapper.KeyMapper; 11 import com.cy.mybatis.mapper.LockMapper; 12 import com.cy.mybatis.tools.DBTools; 13 14 public class OneToManyService { 15 public static void main(String[] args) { 16 // saveLock(); 17 // batchSaveKeys(); 18 findLockAndKeys(); 19 } 20 21 private static void findLockAndKeys() { 22 23 SqlSession session = DBTools.getSession(); 24 LockMapper lm = session.getMapper(LockMapper.class); 25 LockBean lock = lm.findLockAndKeys(1); 26 System.out.println(lock); 27 } 28 29 private static void batchSaveKeys() { 30 31 SqlSession session = DBTools.getSession(); 32 LockMapper lm = session.getMapper(LockMapper.class); 33 KeyMapper km = session.getMapper(KeyMapper.class); 34 35 LockBean lock = lm.findLockById(1); 36 List<KeyBean> keys = new ArrayList<KeyBean>(); 37 for(int i = 0; i < 5; i++){ 38 KeyBean key = new KeyBean(null, "鑰匙"+i, lock); 39 keys.add(key); 40 } 41 km.batchSaveKeys(keys); 42 session.commit(); 43 } 44 45 private static void saveLock() { 46 SqlSession session = DBTools.getSession(); 47 LockMapper lm = session.getMapper(LockMapper.class); 48 LockBean lock = new LockBean(null, "鎖1", null); 49 lm.saveLock(lock); 50 session.commit(); 51 } 52 }
結果顯示:
三 、批量操作與分頁
這裡就使用前一章的User.就寫出主要的程式碼。
首先定義分頁物件。
1 package com.cy.mybatis.beans; 2 3 import java.util.List; 4 5 6 7 /** 8 * 定義一個分頁物件 9 * 10 * @author 11 * 12 */ 13 public class Pager { 14 15 private int pageNo;// 當前頁碼 16 private int pageTotal;// 總頁碼 17 private int rowsTotal;// 總條數 18 private int pageSize;// 每頁顯示條數 19 private List<Object> list;// 返回的資料集合 20 21 public int getPageNo() { 22 return pageNo; 23 } 24 25 public void setPageNo(int pageNo) { 26 this.pageNo = pageNo; 27 } 28 29 public int getPageTotal() { 30 return pageTotal; 31 } 32 33 public void setPageTotal(int pageTotal) { 34 this.pageTotal = pageTotal; 35 } 36 37 public int getRowsTotal() { 38 return rowsTotal; 39 } 40 41 public void setRowsTotal(int rowsTotal) { 42 this.rowsTotal = rowsTotal; 43 pageTotal = rowsTotal % pageSize == 0 ? rowsTotal / pageSize : rowsTotal / pageSize + 1; 44 } 45 46 public int getPageSize() { 47 return pageSize; 48 } 49 50 public void setPageSize(int pageSize) { 51 this.pageSize = pageSize; 52 } 53 54 public List<?> getList() { 55 return list; 56 } 57 58 public void setList(List<Object> list) { 59 this.list = list; 60 } 61 62 63 @Override 64 public String toString() { 65 return "Pager [pageNo=" + pageNo + ", pageTotal=" + pageTotal 66 + ", rowsTotal=" + rowsTotal + ", pageSize=" + pageSize 67 + ", list=" + list + "]"; 68 } 69 70 }
UserMapper.java介面。
1 package com.cy.mybatis.mapper; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.apache.ibatis.annotations.Param; 7 8 import com.cy.mybatis.beans.UserBean; 9 10 public interface UserMapper { 11 /** 12 * 新增使用者 13 * @param user 14 * @return 15 * @throws Exception 16 */ 17 public int insertUser(@Param("user")UserBean user) throws Exception; 18 /** 19 * 修改使用者 20 * @param user 21 * @param id 22 * @return 23 * @throws Exception 24 */ 25 public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception; 26 /** 27 * 刪除使用者 28 * @param id 29 * @return 30 * @throws Exception 31 */ 32 public int deleteUser(int id) throws Exception; 33 /** 34 * 根據id查詢使用者資訊 35 * @param id 36 * @return 37 * @throws Exception 38 */ 39 public UserBean selectUserById(int id) throws Exception; 40 /** 41 * 查詢所有的使用者資訊 42 * @return 43 * @throws Exception 44 */ 45 public List<UserBean> selectAllUser() throws Exception; 46 47 48 /** 49 * 批量增加 50 * @param user 51 * @return 52 * @throws Exception 53 */ 54 public int batchInsertUser(@Param("users")List<UserBean> user) throws Exception; 55 56 /** 57 * 批量刪除 58 * @param list 59 * @return 60 * @throws Exception 61 */ 62 public int batchDeleteUser(@Param("list")List<Integer> list) throws Exception; 63 64 65 /** 66 * 分頁查詢資料 67 * @param parma 68 * @return 69 * @throws Exception 70 */ 71 public List<UserBean> pagerUser(Map<String, Object> parmas) throws Exception; 72 73 /** 74 * 75 * 分頁統計資料 76 * @param parma 77 * @return 78 * @throws Exception 79 */ 80 public int countUser(Map<String, Object> parmas) throws Exception; 81 82 83 84 }
xml檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.cy.mybatis.mapper.UserMapper"> 4 <!-- 自定義返回結果集 --> 5 <resultMap id="userMap" type="UserBean"> 6 <id property="id" column="id" javaType="java.lang.Integer"></id> 7 <result property="username" column="username" javaType="java.lang.String"></result> 8 <result property="password" column="password" javaType="java.lang.String"></result> 9 <result property="account" column="account" javaType="java.lang.Double"></result> 10 </resultMap> 11 <!-- 在各種標籤中的id屬性必須和介面中的方法名相同 , id屬性值必須是唯一的,不能夠重複使用。parameterType屬性指明查詢時使用的引數型別,resultType屬性指明查詢返回的結果集型別--> 12 <!-- useGeneratedKeys:( 僅 對 insert 有 用 ) 這 會 告 訴 MyBatis 使 用 JDBC 的getGeneratedKeys 13 方法來取出由資料(比如:像 MySQL 和 SQLServer 這樣的資料庫管理系統的自動遞增欄位)內部生成的主鍵。預設值: false。 --> 14 <!--keyProperty: (僅對 insert有用)標記一個屬性, MyBatis 會通過 getGeneratedKeys或者通過 insert 語句的 selectKey 子元素設定它的值。預設:不設定。 --> 15 <!--#{}中的內容,為佔位符,當引數為某個JavaBean時,表示放置該Bean物件的屬性值 --> 16 17 18 <insert id="insertUser" useGeneratedKeys="true" keyProperty="user.id"> 19 insert into t_user (username,password,account) values (#{user.username},#{user.password},#{user.account}) 20 </insert> 21 22 <update id="updateUser"> 23 update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id} 24 </update> 25 26 <delete id="deleteUser" parameterType="int"> 27 delete from t_user where id=#{id} 28 </delete> 29 30 <select id="selectUserById" parameterType="int" resultMap="userMap"> 31 select * from t_user where id=#{id} 32 </select> 33 34 <select id="selectAllUser" resultMap="userMap"> 35 select * from t_user 36 </select> 37 38 39 40 41 <!-- 批量操作和foreach標籤 --> 42 43 <insert id="batchInsertUser" parameterType="java.util.List"> 44 insert into t_user values 45 <foreach collection="users" item="users" separator=","> 46 (null,#{users.username},#{users.password},#{users.account}) 47 </foreach> 48 </insert> 49 50 51 <delete id="batchDeleteUser"> 52 delete from t_user where id in ( 53 <foreach collection="list" item="list" separator=","> 54 #{id} 55 </foreach> 56 ) 57 </delete> 58 59 <!--collection 為用於遍歷的元素(必選),支援陣列、List、Set --> 60 <!-- item 表示集合中每一個元素進行迭代時的別名. --> 61 <!--separator表示在每次進行迭代之間以什麼符號作為分隔 符. --> 62 63 64 <select id="pagerUser" parameterType="java.util.Map" resultMap="userMap"> 65 select * from t_user where 1=1 66 67 <if test="username!=null"> 68 and username like '%${username}%' 69 </if> 70 limit ${index},${pageSize} 71 </select> 72 73 <select id="countUser" parameterType="java.util.Map" resultType="int"> 74 select count(*) from t_user where 1=1 75 <if test="username != null"> 76 and username like '%${username}%' 77 </if> 78 </select> 79 80 81 </mapper>
#在生成SQL時,對於字元型別引數,會拼裝引號
$在生成SQL時,不會拼裝引號,可用於order by之類的引數拼裝
測試類
1 package com.cy.mybatis.service; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.apache.ibatis.session.SqlSession; 9 10 import com.cy.mybatis.beans.UserBean; 11 import com.cy.mybatis.tools.DBTools; 12 import com.cy.mybatis.mapper.UserMapper; 13 14 public class UserService { 15 16 /** 17 * @param args 18 */ 19 public static void main(String[] args) { 20 // insertUser(); 21 // deleteUser(); 22 // updateUser(); 23 // selectUserById(); 24 // selectAllUser(); 25 26 // batchInsertUser(); 27 // batchDeleteUser(); 28 // countUser(); 29 pagerUser(); 30 } 31 32 33 private static void countUser() { 34 SqlSession session = DBTools.getSession(); 35 UserMapper mapper = session.getMapper(UserMapper.class); 36 Map<String,Object> params = new HashMap<String,Object>(); 37 params.put("username", "kitty"); 38 int index = 0; 39 params.put("index", index);//從第幾頁開始。mysql是從0開始的 40 params.put("pageSize", 5);//每頁顯示的資料條數 41 int count; 42 try { 43 count = mapper.countUser(params); 44 System.out.println(count); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 49 } 50 51 52 private static void pagerUser() { 53 SqlSession session = DBTools.getSession(); 54 UserMapper mapper = session.getMapper(UserMapper.class); 55 Map<String,Object> params = new HashMap<String,Object>(); 56 params.put("username", "kitty"); 57 params.put("index", 0);//從第幾頁開始。mysql是從0開始的 58 params.put("pageSize", 5);//每頁顯示的資料條數 59 try { 60 List<UserBean> u = mapper.pagerUser(params); 61 for (UserBean userBean : u) { 62 System.out.println("--------"+userBean); 63 } 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 68 } 69 70 71 private static void batchDeleteUser() { 72 SqlSession session = DBTools.getSession(); 73 UserMapper mapper = session.getMapper(UserMapper.class); 74 List<Integer> ids = new ArrayList<Integer>(); 75 for(int i = 4; i < 10; i ++){ 76 ids.add(i); 77 } 78 try { 79 mapper.batchDeleteUser(ids); 80 session.commit(); 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } 84 85 } 86 87 88 private static void batchInsertUser() { 89 SqlSession session = DBTools.getSession(); 90 UserMapper mapper = session.getMapper(UserMapper.class); 91 92 List<UserBean> users = new ArrayList<UserBean>(); 93 for(int i = 0; i < 10; i ++){ 94 UserBean user = new UserBean("kitty"+i, "123456", 6000.0); 95 users.add(user); 96 } 97 try { 98 mapper.batchInsertUser(users); 99 session.commit(); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 105 106 /** 107 * 新增使用者 108 */ 109 private static void insertUser() { 110 SqlSession session = DBTools.getSession(); 111 UserMapper mapper = session.getMapper(UserMapper.class); 112 UserBean user = new UserBean("懿", "1314520", 7000.0); 113 try { 114 mapper.insertUser(user); 115 System.out.println(user.toString()); 116 session.commit(); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 session.rollback(); 120 } 121 } 122 123 124 /** 125 * 刪除使用者 126 */ 127 private static void deleteUser(){ 128 SqlSession session=DBTools.getSession(); 129 UserMapper mapper=session.getMapper(UserMapper.class); 130 try { 131 mapper.deleteUser(1); 132 session.commit(); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 session.rollback(); 136 } 137 } 138 139 /** 140 * 修改使用者資料 141 */ 142 private static void updateUser(){ 143 SqlSession session=DBTools.getSession(); 144 UserMapper mapper=session.getMapper(UserMapper.class); 145 UserBean user =new UserBean("小明", "111",6000.0); 146 try { 147 mapper.updateUser(user, 3); 148 session.commit(); 149 } catch (Exception e) { 150 e.printStackTrace(); 151 session.rollback(); 152 } 153 } 154 155 /** 156 * 根據id查詢使用者 157 */ 158 private static void selectUserById(){ 159 SqlSession session=DBTools.getSession(); 160 UserMapper mapper=session.getMapper(UserMapper.class); 161 try { 162 UserBean user= mapper.selectUserById(2); 163 System.out.println(user.toString()); 164 165 session.commit(); 166 } catch (Exception e) { 167 e.printStackTrace(); 168 session.rollback(); 169 } 170 } 171 172 /** 173 * 查詢所有的使用者 174 */ 175 private static void selectAllUser(){ 176 SqlSession session=DBTools.getSession(); 177 UserMapper mapper=session.getMapper(UserMapper.class); 178 try { 179 List<UserBean> user=mapper.selectAllUser(); 180 System.out.println(user.toString()); 181 session.commit(); 182 } catch (Exception e) { 183 e.printStackTrace(); 184 session.rollback(); 185 } 186 } 187 188 }
看一下專案的整體: