Mybatis应用
工具类MyBatisUtils
在上一篇博客中 通过sqlsession 中 写了一个就简单的查询方法 并输出测试 我们发现 几乎所有的增删该查 都会调用 sqlsession 这样得到代码十分的冗余 所有在这里我们将这些公共的代码 都提到了一个公共的静态类既方便理解也方便后期的维护
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 }
下面我写一个简单的案例 来书写一下这个MyBatisUtils的使用(接口和小配置的XML文件这里就不展示了 只是展示一下MyBatisUtils的使用)
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 }
这里我们仅仅用了三行代码就实现了这个查询 还是比较便于开发的
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 ,foreach的array方式 ,foreach的list方式 ,foreach的list自定义类型方式
下面我书写两个例子
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>
测试类
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 }
这些智能标签大大增加了我们的开发速度,从而也减少我们的代码数量
多表关联查询(有一点难理解 整整弄了半天 )
先看一下实体类吧
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 }
一共两个实体类这是第二个
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 }
第一套方式
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>
测试类
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 }
第二种方式
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>
测试类
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 }