Mybatis的接口函数能不能进行重载?
- mybatis版本:3.4x
- java版本:java 8
语法层面
1、接口的方法可以进行重载,因为 java 语法可以让接口函数进行重载。
Mybatis框架方面
1、结论:可以有条件的进行重载。
2、为什么会有这个问题?:mybatis里面将接口里面的方法名称和配置文件里面的id属性进行唯一配对,在同一个命名空间下只能有一个id,那么所有函数名称相同的重载函数都会被绑定到一个id上,所以,如果要实现函数的重载,必须让一个SQL语句去适应多个函数的参数,如果是单纯的重载是肯定不行的(重载函数的定义就是参数相关),但是得益于mybatis的多种传参方式和隐性的分页功能,可以在接口里面进行函数重载,但是还是需要将所有的重载函数适配到同一个id的SQL上面去,仍然有很大的局限性,并不是可以随意的进行重载。
测试
1、数据库环境
DROP TABLE IF EXISTS sys_user; CREATE TABLE sys_user( id BIGINT NOT NULL AUTO_INCREMENT COMMENT '用户的ID', user_name VARCHAR(50) COMMENT '用户名称', user_password VARCHAR(50) COMMENT '密码', user_email VARCHAR(50) COMMENT '邮箱', user_info TEXT COMMENT '简介', head_img BLOB COMMENT '头像', create_time DATETIME COMMENT '创建时间', PRIMARY KEY (id) )DEFAULT CHARSET utf8; ALTER TABLE sys_user COMMENT '用户表'; INSERT INTO sys_user VALUES ('1','admin','123456','[email protected]','管理员',null,'2016-04-01 17:00:58'); INSERT INTO sys_user VALUES ('1001','test','123456','[email protected]','测试用户',null,'2016-04-01 17:01:52');
2、实体类
public class SysUser { private Long id; private String userName; private String userPassword; private String userEmail; private String userInfo; private byte[] headImg; private Date createTime; public SysUser(){ } /**setter and getter*/ }
3、测试
3.1、测试模板代码
public class SysUserDaoTest { private static SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; @BeforeClass public static void beforeClass(){ String resource = "plus/mybatis-config.xml"; try{ InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e){ e.printStackTrace(); } } @Before public void before(){ //事物手动提交 sqlSession = sqlSessionFactory.openSession(false); } @After public void after(){ //回滚事物 sqlSession.rollback(true); //关闭会话 sqlSession.close(); } }
3.2、测试场景一
重载实现分页、参数类型转换实现重载。
3.2.1、接口
public interface SysUserDao { SysUser selectById(Long id); ListselectById(Long id,RowBounds rowBound); }
3.2.2、mapper配置文件
3.3、测试场景二
参数传递方式实现重载,map参数传递和固定参数名的参数传递方式
3.3.1、接口
public interface SysUserDao { SysUser selectByCondition(@Param("id")Long id,@Param("userName")String userName,@Param("userPassword")String userPassword); SysUser selectByCondition(Map params); }
3.3.2、mapper配置文件
3.4、完整的测试代码
package plus.dao; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.*; import org.junit.*; import plus.pojo.SysUser; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class SysUserDaoTest { private static SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; @BeforeClass public static void beforeClass(){ String resource = "plus/mybatis-config.xml"; try{ InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e){ e.printStackTrace(); } } @Before public void before(){ //事物手动提交 sqlSession = sqlSessionFactory.openSession(false); } @After public void after(){ //回滚事物 sqlSession.rollback(true); //关闭会话 sqlSession.close(); } @Test public void selectById(){ SysUserDao sysUserDao = sqlSession.getMapper(SysUserDao.class); SysUser sysUser = sysUserDao.selectById(1L); Assert.assertEquals("admin",sysUser.getUserName()); SysUser sysUser1 = sysUserDao.selectById("1"); Assert.assertEquals("admin",sysUser1.getUserName()); /* plus.dao.SysUserDao.selectById - ==> Preparing: SELECT * FROM sys_user WHERE id = ?; plus.dao.SysUserDao.selectById - ==> Parameters: 1(Long) plus.dao.SysUserDao.selectById - <== Total: 1 plus.dao.SysUserDao.selectById - ==> Preparing: SELECT * FROM sys_user WHERE id = ?; plus.dao.SysUserDao.selectById - ==> Parameters: 1(String) plus.dao.SysUserDao.selectById - <== Total: 1 */ RowBounds rowBound = new RowBounds(0,2); ListsysUsers = sysUserDao.selectById(1L,rowBound); Assert.assertEquals(1,sysUsers.size()); /* plus.dao.SysUserDao.selectById - ==> Preparing: SELECT * FROM sys_user WHERE id = ?; plus.dao.SysUserDao.selectById - ==> Parameters: 1(Long) plus.dao.SysUserDao.selectById - <== Total: 1 */ } @Test public void selectByCondition(){ SysUserDao sysUserDao = sqlSession.getMapper(SysUserDao.class); SysUser sysUser = sysUserDao.selectByCondition(1L,"admin","123456"); Assert.assertEquals("[email protected]",sysUser.getUserEmail()); /* plus.dao.SysUserDao.selectByCondition - ==> Preparing: SELECT * FROM sys_user WHERE id = ? AND user_name = ? AND user_password = ?; plus.dao.SysUserDao.selectByCondition - ==> Parameters: 1(Long), admin(String), 123456(String) plus.dao.SysUserDao.selectByCondition - <== Total: 1 */ Map params = new HashMap (3); params.put("id",1L); params.put("userName","admin"); params.put("userPassword","123456"); SysUser sysUser1 = sysUserDao.selectByCondition(params); /* plus.dao.SysUserDao.selectById - ==> Preparing: SELECT * FROM sys_user WHERE id = ?; plus.dao.SysUserDao.selectById - ==> Parameters: 1(Long) plus.dao.SysUserDao.selectById - <== Total: 1 */ Assert.assertEquals("[email protected]",sysUser1.getUserEmail()); } }
3.5、完整的mapper文件
3.6、完整的接口
public interface SysUserDao { SysUser selectById(Long id); ListselectById(Long id,RowBounds rowBound); SysUser selectById(String name); SysUser selectByCondition(@Param("id")Long id,@Param("userName")String userName,@Param("userPassword")String userPassword); SysUser selectByCondition(Map params); }
MyBatis实现方法重载的小技巧
QuestionMapper.java
利用接口默认方法 default 实现
QuestionMapper.xml
利用动态sql对 userId 字段进行判断
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。