Mybatis开发之mapper代理实现自定义接口(常用)
2024-10-21 16:07:08
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();
}
}
最新文章
- Smart3D系列教程3之 《论照片三维重建中Smart3D几个工作模块的功能意义》
- table布局的简单网页
- 深入浅出 - Android系统移植与平台开发(六)- 为Android启动加速
- charles 结合mocky 模拟数据
- ADF_Controller系列5_通过绑定TasksFlow创建Train
- 曲演杂坛--EXISTS语句
- php和AJAX用户注册演示程序
- BZOJ 1864: [Zjoi2006]三色二叉树( 树形dp )
- shell编程其实真的很简单(三)
- javaWeb学习总结(10)- EL函数库(2)
- 【转载】C/C++中的char,wchar,TCHAR
- jango路由层
- 浅谈DP
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
- restfull环境搭建-helloword(三)
- sim卡联系人name为空的问题。
- Python2.7-robotparser
- STM32 双ADC同步规则采样
- jQuery API的特点
- mongodb3.6集群搭建:分片集群认证
热门文章
- pat乙级1022 D进制的A+B
- (unsigned)short溢出后隐式转换为int
- SAP VL02N 字段不允许编辑
- 四种语言刷算法之删除链表的倒数第 N 个结点
- 关于使用C++调用WCF的方法
- 2.4	在DispatcherServlet的service方法中,通过ServletPath获取对应的Controller对象
- JavaScrip核心基础(讲师李立超)
- 解决Hbuliderx的代码不能自动补全的问题
- 微信内h5调用支付
- VS2022 17.1.6在windows10下打开winform设计器报timed out while connecting to named pipe错误