Mybatis應用

工具類MyBatisUtils

在上一篇博客中 經過sqlsession 中 寫了一個就簡單的查詢方法 並輸出測試  咱們發現 幾乎全部的增刪該查 都會調用 sqlsession 這樣獲得代碼十分的冗餘 全部在這裏咱們將這些公共的代碼 都提到了一個公共的靜態類既方便理解也方便後期的維護java

 1 package cn.wh.util;  2 
 3 import org.apache.ibatis.session.SqlSession;  4 import org.apache.ibatis.session.SqlSessionFactory;  5 import org.apache.ibatis.session.SqlSessionFactoryBuilder;  6 
 7 import java.io.IOException;  8 import java.io.InputStream;  9 public class MybatisUtil { 10         static String path="mybatis-config.xml"; 11         static InputStream is; 12         static SqlSessionFactory factory; 13     static { 14         try { 15             is = org.apache.ibatis.io.Resources.getResourceAsStream(path); 16             factory=new SqlSessionFactoryBuilder().build(is); 17         } catch (IOException e) { 18  e.printStackTrace(); 19  } 20  } 21     public static SqlSession getSqlSession() { 22         return factory.openSession(); 23  } 24 }
View Code

下面我寫一個簡單的案例 來書寫一下這個MyBatisUtils的使用(接口和小配置的XML文件這裏就不展現了 只是展現一下MyBatisUtils的使用sql

1  @Test //使用工具類 MybatisUtil
2     public void MybatisUtil(){ 3         SqlSession sqlSession = MybatisUtil.getSqlSession(); 4         User user= sqlSession.selectOne("SelectUser"); 5         System.out.println(user.getUname()+"="); 6     }
View Code

這裏咱們僅僅用了三行代碼就實現了這個查詢 仍是比較便於開發的apache

getMapper()

getMapper的做用,獲取到接口,直接經過點的方式調用方法,以避免直接手打的方式寫錯方法名,(強類型的方式)數組

 

public  void FirstList(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); IUserDao mapper = sqlSession.getMapper(IUserDao.class); Map<String ,Object> map=new HashMap<String ,Object>() ; //Key千萬必定要和實體類對應
        map.put("uname","1"); map.put("upwd","1"); List<User> users = mapper.conditionFirst(map); System.out.println(users.size()); }

 

在mybatis中 有一些智能標籤 

他分爲 where ,if ,choose ,foreacharray方式 ,foreachlist方式 ,foreachlist自定義類型方式session

下面我書寫兩個例子mybatis

 

 1   <!--智能標籤forearch 數組-->
 2     <select id="conditionByforearch" resultMap="MessageResult">
 3      SELECT * from user  4      <where>
 5  uid in  6          <foreach collection="array" item="myid" open="(" close=")" separator=",">
 7  #{myid}  8          </foreach>
 9      </where>
10     </select>
11     <!--智能標籤forearch 集合-->
12     <select id="conditionByforearchList" resultMap="MessageResult">
13         SELECT * from user 14         <where>
15  uid in 16             <foreach collection="list" item="myid" open="(" close=")" separator=",">
17  #{myid} 18             </foreach>
19         </where>
20     </select>
21     <!--智能標籤forearch 自定義集合-->
22     <select id="conditionByforearchListSecond" resultMap="MessageResult">
23         SELECT * from user 24         <where>
25  uid in 26             <foreach collection="list" item="user" open="(" close=")" separator=",">
27  #{user.uid} 28             </foreach>
29         </where>
30     </select>
View Code

 

測試類app

 1   @Test //智能標籤 forearch 數組
 2     public  void conditionByforearch(){  3         SqlSession sqlSession = MybatisUtil.getSqlSession();  4         IUserDao mapper = sqlSession.getMapper(IUserDao.class);  5         int [] bookid={1,2};  6         List<User> condition = mapper.conditionByforearch(bookid);  7         for (User item:condition  8  ) {  9  System.out.println(item.getUname()); 10  } 11  } 12     @Test //智能標籤 forearch 集合
13     public  void conditionByforearchList(){ 14         SqlSession sqlSession = MybatisUtil.getSqlSession(); 15         IUserDao mapper = sqlSession.getMapper(IUserDao.class); 16      List<Integer> list=new ArrayList<>(); 17             list.add(1); 18         List<User> condition = mapper.conditionByforearchList(list); 19         for (User item:condition 20  ) { 21  System.out.println(item.getUname()); 22  } 23  } 24     @Test //智能標籤 forearch 集合自定義
25     public  void SecondconditionByforearchList(){ 26         SqlSession sqlSession = MybatisUtil.getSqlSession(); 27         IUserDao mapper = sqlSession.getMapper(IUserDao.class); 28         List<User> list=new ArrayList<User>(); 29         User user=new User(); 30         user.setUid(1); 31  list.add(user); 32         User user1=new User(); 33         user1.setUid(2); 34  list.add(user1); 35         List<User> condition = mapper.conditionByforearchListSecond(list); 36         for (User item:condition 37  ) { 38  System.out.println(item.getUname()); 39  } 40 
41     }
View Code

這些智能標籤大大增長了咱們的開發速度,從而也減小咱們的代碼數量ide

多表關聯查詢(有一點難理解 整整弄了半天  )工具

先看一下實體類吧測試

 

 1 package cn.wh.entity;  2 
 3 import java.util.ArrayList;  4 import java.util.List;  5 
 6 public class UserType {  7     private Integer typeid;  8     private String typeName;  9     private Integer uid; 10 
11     private List<User> users=new ArrayList<User>(); 12 
13     public List<User> getUsers() { 14         return users; 15  } 16 
17     public void setUsers(List<User> users) { 18         this.users = users; 19  } 20 
21     public Integer getTypeid() { 22         return typeid; 23  } 24 
25     public void setTypeid(Integer typeid) { 26         this.typeid = typeid; 27  } 28 
29     public String getTypeName() { 30         return typeName; 31  } 32 
33     public void setTypeName(String typeName) { 34         this.typeName = typeName; 35  } 36 
37     public Integer getUid() { 38         return uid; 39  } 40 
41     public void setUid(Integer uid) { 42         this.uid = uid; 43  } 44 }
View Code

 

一共兩個實體類這是第二個

 1 package cn.wh.entity;  2 
 3 public class User {  4 
 5     // 用戶id
 6     private int uid;  7     // 用戶名
 8     private String uname;  9     //密碼
10     private String upwd; 11     //手機
12     private String phone; 13 
14     private UserType userType; 15 
16     public UserType getUserType() { 17         return userType; 18  } 19 
20     public void setUserType(UserType userType) { 21         this.userType = userType; 22  } 23 
24     public String getUpwd() { 25         return upwd; 26  } 27 
28     public void setUpwd(String upwd) { 29         this.upwd = upwd; 30  } 31 
32     public String getPhone() { 33         return phone; 34  } 35 
36     public void setPhone(String phone) { 37         this.phone = phone; 38  } 39 
40 
41     public int getUid() { 42         return uid; 43  } 44     public void setUid(int uid) { 45         this.uid = uid; 46  } 47     public String getUname() { 48         return uname; 49  } 50     public void setUname(String uname) { 51         this.uname = uname; 52  } 53 }
View Code

 

 

第一套方式

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper  3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="cn.wh.dao.IUserType">
 6     <resultMap type="cn.wh.entity.UserType" id="UserType">
 7         <id column="typeid" jdbcType="INTEGER" property="typeid"/>
 8         <result column="typeName" jdbcType="VARCHAR" property="typeName" />
 9 
10 
11         <collection property="users" ofType="cn.wh.entity.User">
12             <id column="uid" jdbcType="INTEGER" property="uid"/>
13             <result column="uname" jdbcType="VARCHAR" property="uname" />
14         </collection>
15     </resultMap>
16 
17     <resultMap type="cn.wh.entity.UserType" id="UserType1">
18         <id column="typeid" jdbcType="INTEGER" property="typeid"/>
19         <result column="typeName" jdbcType="VARCHAR" property="typeName" />
20         <collection property="users" ofType="cn.wh.entity.User" select="ss" column="typeid">
21         </collection>
22     </resultMap>
23 <select id="listType" resultMap="UserType">
24 select user.uid from usertype,user 25 where user.typeid=usertype.typeid 26 and user.uid='1'
27 
28 
29 </select>
30     <select id="UserTypecondition" resultMap="UserType1">
31  SELECT typeid from usertype 32         where typeid='1'
33     </select>
34     <select id="ss" resultType="cn.wh.entity.User">
35         SELECT * from user 36         where typeid='1'
37   </select>
38 </mapper>
View Code

 

測試類

 1  @Test  2  public  void userTypeList(){  3         SqlSession sqlSession = MybatisUtil.getSqlSession();  4         IUserType mapper = sqlSession.getMapper(IUserType.class);  5         UserType userType = mapper.listType();  6  System.out.println(userType.getUsers().size());  7  }  8     //查詢單一條 有條件 (一對多)
 9  @Test 10     public  void userTypeListcondition(){ 11         SqlSession sqlSession = MybatisUtil.getSqlSession(); 12         IUserType mapper = sqlSession.getMapper(IUserType.class); 13         UserType userType = mapper.UserTypecondition(1); 14         List<User> users = userType.getUsers(); 15  System.out.println(users); 16         for (User item:users 17  ) { 18  System.out.println(item.getUpwd()); 19  } 20 }
View Code

第二種方式

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper  3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="cn.wh.dao.IUser">
 6 
 7     <resultMap id="Users" type="cn.wh.entity.User">
 8         <id column="uid" jdbcType="INTEGER" property="uid"/>
 9         <result column="uname" jdbcType="VARCHAR" property="uname" />
10         <association property="userType" javaType="cn.wh.entity.UserType">
11             <id column="typeid" jdbcType="INTEGER" property="typeid"/>
12             <result column="typeName" jdbcType="VARCHAR" property="typeName" />
13         </association>
14     </resultMap>
15     <resultMap id="manyUsers" type="cn.wh.entity.User">
16         <id column="uid" jdbcType="INTEGER" property="uid"/>
17         <result column="uname" jdbcType="VARCHAR" property="uname" />
18         <association property="userType" javaType="cn.wh.entity.UserType" column="uid" select="ss">
19         </association>
20     </resultMap>
21 <!--多對一-->
22     <select id="findeUser" resultMap="Users">
23     SELECT * from user,usertype where user.typeid=usertype.typeid and user.uid=#{uid} 24     </select>
25     <!--多對多-->
26     <select id="findempmany" resultMap="manyUsers">
27  SELECT uid from user 28         where typeid='1'
29     </select>
30     <select id="ss" resultType="cn.wh.entity.UserType">
31          SELECT * from usertype 32         where typeid='1'
33     </select>
34 
35 </mapper>
View Code

 

測試類

 1     //多對一
 2  @Test  3     public  void manyTOne(){  4         SqlSession sqlSession = MybatisUtil.getSqlSession();  5         IUser mapper = sqlSession.getMapper(IUser.class);  6         User user = mapper.findeUser(2);  7 
 8         System.out.println(user.getUname()+"=");  9  } 10     //多對多
11  @Test 12     public  void manyTomany(){ 13         SqlSession sqlSession = MybatisUtil.getSqlSession(); 14         IUser mapper = sqlSession.getMapper(IUser.class); 15         User user = mapper.findempmany(1); 16  System.out.println(user.getUserType().getUid()); 17     }
View Code
相關文章
相關標籤/搜索