后台(37)——MyBatis的Mapper开发方式
发布日期:2021-06-30 11:18:08
浏览次数:2
分类:技术文章
本文共 4262 字,大约阅读时间需要 14 分钟。
版权声明
- 本文原创作者:
- 作者博客地址:
使用Mybatis开发Dao,通常有两个方法:原始Dao开发方式和Mapper接口开发方式。
在本篇文章中,我们在前两篇博客的基础上来一起完成Mapper接口开发方式。开发规范
Mapper接口开发方式比原始的DAO的方式要简便许多,但是这种简便是建立在规范之上的,所以在采用该方式时务必严格遵守开发规范.
在Mapper接口开发方式中有两个核心的东西:mapper.xml和mapper.java
mapper接口开发需要遵循以下规范:
- 1、mapper.xml文件中的namespace与mapper.java接口的类的全路径相同。
- 2、mapper.java接口中的方法名和mapper.xml中定义的每个sql的id相同
- 3、mapper.java接口中的方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型保持一致
- 4、mapper.java接口中方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型保持一致
好了,我们现在就按照此规范来改造之前的例子
StudentMapper.java
/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */package cn.com;import java.util.List;public interface StudentMapper { public Student findStudentById(int id); public ListfindStudentByName(String name); public void insertStudent(Student student); public void deleteStudent(int id); public void updateStudent(Student student);}
StudentMapper.xml
SELECT LAST_INSERT_ID() INSERT INTO student (name,gender,birthday) value (#{name},#{gender},#{birthday})DELETE FROM student where id=#{id} UPDATE student set name=#{name},gender=#{gender},birthday=#{birthday} where id=#{id}
嗯哼,对照着这两个文件看就会发现:我们在书写的过程中严格遵守了开发规范。
TestCRUD.java
/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */package cn.com;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.List;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.Before;import org.junit.Test;public class TestCRUD { private SqlSessionFactory sqlSessionFactory; @Before public void intiSqlSessionFactory() throws Exception { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void findStudentById() throws IOException{ SqlSession sqlSession=sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.findStudentById(5); System.out.println(student); } @Test public void findStudentByName() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Listlist = studentMapper.findStudentByName("木"); for (Student student : list) { System.out.println(student); } } @Test public void insertStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setName("小小木希"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.insertStudent(student); sqlSession.commit(); sqlSession.close(); System.out.println(student.getId()); } @Test public void deleteStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); studentMapper.deleteStudent(5); sqlSession.commit(); sqlSession.close(); } @Test public void updateStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setId(5); student.setName("空空姐姐"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.updateStudent(student); sqlSession.commit(); sqlSession.close(); }}
这些测试用例中,最重要的就是:
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
得到Mapper,再调用它定义的增删改查方法
最后,按照惯例还是附上项目的结构图:
转载地址:https://it9527.blog.csdn.net/article/details/61658070 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月10日 21时54分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
智能体 Intelligent Agent
2019-04-30
Network Compression网络压缩(一)
2019-04-30
GAN系列(零)—— GAN的发展(两条路线)
2019-04-30
Conditional GAN (CGAN) 条件生成网络
2019-04-30
强化学习(三) —— Policy Gradient 策略梯度
2019-04-30
docker安装oracle(win10)
2019-04-30
Cloudera Quickstart & HUE
2019-04-30
HUE
2019-04-30
CDH
2019-04-30
行为树 BT
2019-04-30
Cassandra & CQL
2019-04-30
Oracle数据库
2019-04-30
Oracle数据库命令
2019-04-30
plsql
2019-04-30
有限状态机FSM
2019-04-30
Win10 Docker
2019-04-30
Python绘制动画并保存为gif/mp4 (matplotlib)
2019-04-30
PRM概率路线图
2019-04-30
ROS(六)——订阅者Subscriber的编程实现(C++ & Python)
2019-04-30
ROS(七)——话题消息的定义与使用
2019-04-30