Mybatis开发之mapper代理实现自定义接口(常用)
Mybatis开发之mapper代理实现自定义接口(常用)
通过mapper代理实现自定义接口
- 自定义接口,接口里面定义定义相关的业务方法
- 编写方法相对应的Mapper.xml、
- 定义完接口后,Mapper会自动帮我们生成实现类和对象。
1.自定义接口
package com.southwind.repository;
import com.southwind.entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int deleteById(long id);
public List<Account> findAll();
public Account findById(long id);
}
2.创建接口对应的Mapper.xml,定义接口方法中的SQL语句
Mapper.xml中的statement标签课根据Sql执行的业务选择insert,delete,update,select。
Mybatis框架会根据规则自动创建接口实现类的代理对象,不用自己手动再创建实现类。
为了方便,直接把mapper文件写在与接口相同的文件夹下。
规则:
- Mapper.xml中namespace为接口的全类名
- Mapper.xml中statement的id为接口中对应的方法名
- Mapper.xml中statemnet的parameterType和接口中对应的方法的参数类型一致
- Mapper.xml中statement的resultType和接口中的对应方法返回值基本一致。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.repository.AccountRepository">
<insert id="save" parameterType="com.southwind.entity.Account"></insert>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password=#{password},age=#{age} where id =#{id}
</update>
<delete id="delete" parameterType="long">
delete from t_account where id = #{id}
</delete>
<!--AccountRepository接口中,findAll方法没有参数不用写parameterType-->
<!--有返回值,但是列表类型的返回值只写泛型里面的类Account的类型即可-->
<select id="findAll" resultType="com.southwind.entity.Account">
select * from t_account
</select>
<select id="findById" parameterType="long" resultType="com.southwind.entity.Account">
select * from t_account where id =#{id}
</select>
</mapper>
3.在全局配置文件config.xml中注册AccountRepository.xml
<mappers>
<!--原生接口注册-->
<mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>
<!--Mapper代理接口注册 -->
<mapper resource="com/southwind/repository/AccountMapper.xml"></mapper>
</mappers>
test
package com.southwind.test;
import com.southwind.entity.Account;
import com.southwind.repository.AccountRepository;
import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.sql.SQLOutput;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
InputStream inputStream = Test2.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取接口的代理对象
AccountRepository accountRepository = sqlSession.getMapper(AccountRepository.class);
//添加对象
Account account1 = new Account(2l,"李四","123",25);
Account account2= new Account(3l,"王五","123",26);
int save = accountRepository.save(account1);
int save2 = accountRepository.save(account2);
System.out.println(save);
System.out.println(save2);
//增删改操作都需要提交事务数据库才会更新,查询不需要提交事务
sqlSession.commit();
//查询所有对象
List<Account> list = accountRepository.findAll();
for (Account account:list){
System.out.println(account);
}
//通过ID查询
Account account3 = accountRepository.findById(2);
System.out.println("查询ID为2的用户结果:"+account3);
//修改对象
Account account4 = accountRepository.findById(2);
account4.setUsername("小明");
account4.setPassword("123124");
account4.setAge(37);
accountRepository.update(account4);
List<Account> list2 = accountRepository.findAll();
for (Account account:list2){
System.out.println(account);
}
//通过ID删除对象
accountRepository.deleteById(4);
sqlSession.commit();
sqlSession.close();
}
}
Mybatis开发之mapper代理实现自定义接口(常用)的更多相关文章
- Mybatis-Dao层开发之Mapper接口
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法. Mapper接口开发 ...
- JavaEE开发之SpringMVC中的自定义拦截器及异常处理
上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...
- mybatis框架(2)---mapper代理方法
mapper代理方法 在我们在写MVC设计的时候,都会写dao层和daoimp实现层,但假如我们使用mapper代理的方法,我们就可以不用先daoimp实现类 当然这得需要遵守一些相应的规则: (1) ...
- dt二次开发之-url伪静态的自定义
dt内核的方便性在于代码内核完全开源,都可以根据自身需要进行优化整改,个人在这段时间的深入研究,发现这套内核的方便性,今天继续给大家分享下DT的url伪静态如何自定义函数. url自定义文件是在api ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- mybatis——使用mapper代理开发方式
---------------------------------------------------------------generatorConfig.xml------------------ ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- 用mybatis实现dao的编写或者实现mapper代理
一.mybatis和hibernate的区别和应用场景hibernate:是一个标准的ORM框架(对象关系映射).入门门槛较高的,不需要写sql,sql语句自动生成了.对sql语句进行优化.修改比较困 ...
- 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...
- Mybatis学习总结(二)——Mapper代理开发
一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...
随机推荐
- LRU 居然翻译成最近最少使用?真相原来是这样!(附力扣题解)
前言 相信有很多同学和我一样,第一次碰到 LRU(Least Recently Used) 的这个解释「最近最少使用」都不知道是什么意思,用汤家凤老师的话来说: 我真的感到匪夷所思啊! 最近是表示时间 ...
- 初始化安装后 Nacos 动态路由配置不生效
一.问题描述 1.每次初始化安装整套项目,包括安装 Nacos 和其他服务还有mysql,redis等其他中间件,安装后 Nacos 获取不到 nacos 路由信息(包括后续新写入动态路由配置)!只有 ...
- LeetCode-2043 两数相加题解
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/simple-bank-system 题目描述 你的任务是为一个很受欢迎的银行设计一款程序,以自动 ...
- 97、UserAgentUtils
user-agent-utils 是一个用来解析 User-Agent 字符串的 Java 类库. 其能够识别的内容包括: 超过150种不同的浏览器: 7种不同的浏览器类型: 超过60种不同的操作系统 ...
- No.2.7
响应式 什么是响应式网页?就是一套代码适配不同的屏幕宽度,不同的适配 媒体查询:能够根据设备宽度的变化,设置差异化样式 开发常用写法: 媒体特性常用写法 max-width min-width @me ...
- 【MySQL 服务器参数优化】
http://www.hainiubl.com/topics/75823 https://www.cnblogs.com/msjhw/p/15816582.html https://blog.csdn ...
- Python: 取消numpy科学计数法
Numpy中默认是使用科学计数法来显示数据的,但是这种做法往往不利于我们观测数据,比如坐标数据等.那么如何取消numpy科学计数法呢,请往下看. np.set_printoptions() impor ...
- 安装完IDEA后无法打开
安装完IDEA后无法打开 一.现象 安装完IDEA2021.3版本后,无论用什么办法都无法打开 二.原因 原先有安装过idea,里面有加载过一些插件,或者是破解过,会生成一些文件,导致IDEA无法运行 ...
- ERROR StatusLogger No Log4j 2 configuration file found
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only er ...
- 真的,Web安全入门看这个就够了!
一.HTTP协议 1.HTTP 什么是HTTP? 超文本传输协议,HTTP是基于B/S架构进行通信的,而HTTP的服务器端实现程序有httpd.nginx等,其客户端的实现程序主要是Web浏览器,例如 ...