博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis 之 Mapper接口的使用
阅读量:6968 次
发布时间:2019-06-27

本文共 4932 字,大约阅读时间需要 16 分钟。

hot3.png

一、在前一篇中,存在一些问题:

    1.  没有使用接口编程,Java是面向接口编程语言。应该对数据库操作定义一些接口,调用dao接口完成数据库操作。

172814_wmKF_1757476.png

public interface UserDao {	//根据Id查询用户信息	public User findUserById(int userId) throws Exception;		//添加用户	public void insertUser(User user) throws Exception;		//修改用户	public void updateUser(User user) throws Exception;		//删除用户	public void deleteUser(int userId) throws Exception;}public class UserDaoImpl implements UserDao {	private SqlSessionFactory sqlSessionFactory;		//通过spring将 SqlSessionFactory 注入,这里没有spring,暂时使用构造方法代替		public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {		this.sqlSessionFactory = sqlSessionFactory;	}			public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {		this.setSqlSessionFactory(sqlSessionFactory);	}		@Override	public User findUserById(int userId) throws Exception {		SqlSession sqlSession = null;		User user = null;		try {			//通过SqlSessionFactory获取SqlSession			sqlSession = sqlSessionFactory.openSession();						//使用session操作数据库			//selectOne第一个参数:指定sql的id(statement id),注意带上namespace;第二个参数:向sql中传的参数值			user = sqlSession.selectOne("test.findUserById", userId);						System.out.println(user);		} catch (Exception e) {			// TODO: handle exception		} finally {			if (sqlSession != null) {				sqlSession.close();			}		}				return user;	}	@Override	public void insertUser(User user) throws Exception {		SqlSession sqlSession = null;		try {			//通过SqlSessionFactory获取SqlSession			sqlSession = sqlSessionFactory.openSession();						//使用session操作数据库						sqlSession.insert("test.insertUser", user);						//提交事务			sqlSession.commit();					} catch (Exception e) {			// TODO: handle exception		} finally {			if (sqlSession != null) {				sqlSession.close();			}		}	}}

    2.  虽然上面改写成Dao接口实现类的方式,但访问sql映射文件中定义的sql时需要调用 SqlSession 的 selectOne() 方法,          并将 sql 的位置(命名空间 + id)和参数传递到 selectOne() 方法中,且第一个参数是长字符串,第二个参数是object          对象,编写代码时出现错误无法 在编译阶段发现。

        优化:

        第一步:定义UserMapper.xml, 还用原来的不用变

        第二步:定义mapper接口

public interface UserMapper {	//根据Id查询用户信息	public User findUserById(int userId) throws Exception;			//添加用户	public void insertUser(User user) throws Exception;			//修改用户	public void updateUserById(User user) throws Exception;			//删除用户	public void deleteUserById(int userId) throws Exception;}

                接口定义有以下特点:

                        a. Mapper 接口方法名和 UserMapper.xml 中定义的每个 sql 的 id 同名。

                        b. Mapper 接口方法的输入参数类型和 UserMapper.xml 中定义的 sql parameterType 类型相同。

                        c. Mapper 接口的输出参数类型和 UserMapper.xml 中 定义的 sql 的 resultType 类型相同。

        第三步:修改 UserMapper.xml 的namespace

                修改后的 namespace 即是 Mapper 接口的类路径。

        第四步:通过 Mapper 接口调用statement 去操作数据库

public class UserMapperTest extends TestCase {	private SqlSessionFactory sqlSessionFactory;		String resource = "SqlMapConfig.xml";		//任何测试方法都要执行的方法	@Override	protected void setUp() throws Exception {		super.setUp();		//通过输入流读取配置文件		InputStream inputStream = Resources.getResourceAsStream(resource);				//获取SqlSessionFactory		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);	}		public User testFindUserById() throws Exception {		//获取SqlSession		SqlSession sqlSession = sqlSessionFactory.openSession();		//指定 mapper 接口的类型,MyBatis通过动态代理的方式实现mapper接口		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);				User user = userMapper.findUserById(100101);				sqlSession.commit();		sqlSession.close();				//查询主键为100101的用户并输出		System.out.println("user---" + user);				return user;	}		public void testInsertUser() throws Exception {		SqlSession sqlSession = sqlSessionFactory.openSession();		//指定 mapper 接口的类型,MyBatis通过动态代理的方式实现mapper接口		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);				User user = new User();		user.setUsername("Dina");		user.setSex("0");		user.setBirthday(new Date());		user.setAddress("Redwood City");		user.setDetail("good person");		user.setScore(99.2f);				userMapper.insertUser(user);		sqlSession.commit();		sqlSession.close();	}	public void testUpdateUserById() throws Exception {		SqlSession sqlSession = sqlSessionFactory.openSession();		//指定 mapper 接口的类型,MyBatis通过动态代理的方式实现mapper接口		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);				User user = new User();		user.setUserId(100102);		user.setUsername("Golden");		user.setSex("0");		user.setBirthday(new Date());		user.setAddress("Shen zhen");		user.setDetail("good person");		user.setScore(89.2f);				userMapper.updateUserById(user);		sqlSession.commit();		sqlSession.close();	}	public void testDeleteUserById() throws Exception {		SqlSession sqlSession = sqlSessionFactory.openSession();				UserMapper userMapper = sqlSession.getMapper(UserMapper.class);		userMapper.deleteUserById(100110);				sqlSession.commit();		sqlSession.close();	}}

总结:

使用 Mapper 接口的方式,不用写接口实现类接口完成数据库操作,简单方便,此方法是官方推荐。

是MyBatis一种很重要的用法。

使用 Mapper 接口调用必须具备以下条件:

  1. Mapper 接口方法名 和 UserMapper.xml 中定义的每个 sql 的 id 同名。

  2. Mapper 接口方法的输入参数类型和 UserMapper.xml 中定义的 sql parameterType 类型相同。

  3. Mapper 接口的输出参数类型和 UserMapper.xml 中 定义的 sql 的 resultType 类型相同。

  4. UserMapper.xml 文件中的 namespace 即是 Mapper 接口的类路径。

转载于:https://my.oschina.net/u/1757476/blog/500079

你可能感兴趣的文章
AMD APU A10-5800K, Debian Wheezy, 系统安装ATI闭源驱动
查看>>
bton框架业务核心流程架构
查看>>
利用Nginx实现平台saas化和页面动静态分离
查看>>
必须了解的网络运维知识
查看>>
感觉到花都反光镜反光镜打飞机 感觉
查看>>
memcached编译安装
查看>>
根文件系统基础
查看>>
学习Linux决心书
查看>>
2017年 JavaScript 框架回顾 -- React生态系统
查看>>
不停机发布策略
查看>>
OpenStack各组件介绍
查看>>
J.U.C工具类中的CountDownLatch和CyclicBarrier
查看>>
dovecot+mysql
查看>>
Mysql密码管理及授权
查看>>
JAVA线程安全之synchronized关键字的正确用法
查看>>
springmvc+mybatis+dubbo分布式平台-maven构建根项目
查看>>
一个小常识
查看>>
Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理
查看>>
解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitab
查看>>
Docker 快速删除所有容器
查看>>