接口代理
接口代理指的是Mapper接口代理,也叫注册接口,或接口编程。
即将实体类持久层的接口与Mapper映射文件相关联,实现调用接口中的方法即调用Mapper.xml中相应的操作。
通俗:如何在测试类中调用<mapper>中的CRUD?
方法一:通过sqlSession中的方法;
方法二:即通过接口代理调用。
1.为实体类写一个Interface接口;
2.在Interface接口中编写CRUD方法;
package com.liuYongQi.MuBatis2.dao;
import com.liuYongQi.MuBatis2.pojo.Users;
import java.util.List;
public interface IUsersDao {
public void addUsers(Users users);
//根据用户id删除用户信息
public void deleteUsers(int uuid);
//根据用户id修改一个用户的信息
public void updateUsers(Users users);
//根据uuid获得对象
public Users getUsersByUuid(int uuid);
//得到所有的用户
public List<Users> getAllUsers();
}
3.修改实体类的Mapper映射文件中的namespace名字空间,
即:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--接口注册-->
<mapper namespace="com.liuYongQi.MuBatis2.dao.IUsersDao">
<!--根据uuid获得对象-->
<select id="getUsersByUuid" parameterType="int" resultType="com.liuYongQi.MuBatis2.pojo.Users">
select * from users where uuid=#{uuid}
</select>
<!--添加一个用户-->
<insert id="addUsers" parameterType="com.liuYongQi.MuBatis2.pojo.Users">
insert into users(username,password) values(#{username},#{password})
</insert>
<!--根据用户id修改一个用户的信息-->
<update id="updateUsers" parameterType="com.liuYongQi.MuBatis2.pojo.Users">
update users set username=#{username},password=#{password} where uuid=#{uuid}
</update>
<!--根据用户id删除用户信息-->
<delete id="deleteUsers" parameterType="int">
delete from users where uuid=#{uuid}
</delete>
<!--得到所有的用户-->
<select id="getAllUsers" resultType="com.liuYongQi.MuBatis2.pojo.Users">
select * from users
</select>
</mapper>
4.将Mapper映射文件中的CRUD标签id与Interface接口中的CRUD方法名一一对应。
5.测试类:
package com.liuYongQi.test;
import com.liuYongQi.MuBatis2.dao.IUsersDao;
import com.liuYongQi.MuBatis2.pojo.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestCRUD {
//获得SqlSession对象,通过SqlSession操作CRUD
private SqlSession sqlSession;
private IUsersDao iusersDao;
@Before
public void before(){
//读取配置文件,获取SQLSessionFactory
try {
//通过Resources类加载核心配置文件,得到文件的输入流
InputStream inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
//创建会话工厂,编译配置文件流,sqlSessionFactory
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlSessionFactory得到sqlSession象
sqlSession = sqlSessionFactory.openSession();
//通过sqlSession获得映射实例类的父接口
iusersDao= sqlSession.getMapper(IUsersDao.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void after(){
sqlSession.commit();
}
@Test
public void testGetOne(){
//通过父接口调用接口中获得单个用户信息的方法
Users users = iusersDao.getUsersByUuid(1);
System.out.println(users);
}
@Test
public void getAll(){
//通过父接口调用接口中的方法,即调用Mapper.xml中相同名称的标签
List<Users> allUsers = iusersDao.getAllUsers();
for(Users users:allUsers){
System.out.println(users);
}
}
@Test
public void testUpdate(){
//通过父接口调用接口中修改用户信息的方法
iusersDao.updateUsers(new Users(45,"zs","123456"));
}
@Test
public void testAddUsers(){
//通过父接口调用接口中添加用户的方法
iusersDao.addUsers(new Users("zhangsan","123456"));
}
@Test
public void testdelete(){
//通过父接口调用接口中删除用用户的方法
iusersDao.deleteUsers(45);
}
}
1.增加用户
@Test
public void testAddUsers(){
//通过父接口调用接口中添加用户的方法
iusersDao.addUsers(new Users("zhangsan","123456"));
}
测试成功
2.删除用户
@Test
public void testdelete(){
//通过父接口调用接口中删除用用户的方法
iusersDao.deleteUsers(45);
}
测试成功
删除前
删除后
3.修改用户
@Test
public void testUpdate(){
//通过父接口调用接口中修改用户信息的方法
iusersDao.updateUsers(new Users(45,"zs","123456"));
}
测试成功
修改前
修改后
4.查询单个用户的信息
@Test
public void testGetOne(){
//通过父接口调用接口中获得单个用户信息的方法
Users users = iusersDao.getUsersByUuid(1);
System.out.println(users);
}
测试成功
5.查询所有的用户
@Test
public void getAll(){
//通过父接口调用接口中的方法,即调用Mapper.xml中相同名称的标签
List<Users> allUsers = iusersDao.getAllUsers();
for(Users users:allUsers){
System.out.println(users);
}
}
测试成功
今天就测试到这里了.....
如果大家想浏览我的下一篇文章,请留言
版权声明:此文章属于原创,不准随意转载:https://blog.csdn.net/LYQ2332826438