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, ...
随机推荐
- ENCOURAGE研究: 病情中度活动RA获得深度缓解后有望实现停药
标签: 类风湿关节炎; TNF拮抗剂; 维持期减停 病情中度活动的RA患者在获得深度缓解后有望实现停用生物制剂 电邮发布日期: 2016年2月3日 病情中度活动的类风湿关节炎(RA)患者通过传统DMA ...
- gin 01
1.gin介绍: 2.gin的安装 go get -u github.com/gin-gonic/gin 3.gin的第一个helloWord package main import ( " ...
- 基于AD9361的双收双发射频FMC子卡
FMC177-基于AD9361的双收双发射频FMC子卡 一.板卡介绍 FMC177射频模块分别包含两个接收通道与发射通道,其频率可覆盖达到70MHz~6GHz,AD9361芯片提供具有成本效益的实验平 ...
- vue3 h函数
1. 自定义指令(withDirectives 仅可在setup或render函数中使用)可以使用 withDirectives 将自定义指令应用于 VNode: const { h, resolve ...
- 解决使用mapstruct过程中的一次编译报错问题_Internal error in the mapping processor
说明 mapstruct版本:1.2.0.Final 开发工具:IntelliJ IDEA 2021.3.1 (Ultimate Edition) 报错现象 java: Internal error ...
- 在SublimeText3中想使用快捷键调出插件ColorPicker不起作用办法解决
在SublimeText3中快速打开插件ColorPicker的快捷健是Ctrl+Shift+C,但是我试了却不管用,后来发现是跟插件ConvertToUTF8插件中的快捷键冲突导致,知道原因就好办了 ...
- 【Go】类型转换
字符串转int32 package main import ( "fmt" "strconv" ) func main() { str := "123 ...
- dcat-admin在弹框中使用grid的编辑框不提示也不报错
显示效果 #版本:2.1.5-beta #点击编辑时没有反应,其实它已经把编辑框显示出来了,只是在当前这个弹框的后面,我们看不见,这样你可以在自己的项目中把弹框挪开或在F12中html搜索应该显示的代 ...
- C3861: “CoInitialize”“CoUninitialize”: 找不到标识符
error C3861: "CoInitialize": 找不到标识符error C3861: "CoUninitialize": 找不到标识符 包含头文件和链 ...
- 分布式计算 Distributed computing
分布式计算(Distributed computing),又译为分散式运算,是一门计算机科学,它研究如何把一个需要非常巨大的计算能力才能解决的问题分成许多小的部分,然后把这些部分分配给许多计算机进行处 ...