Mybatis 通过 Mapper 代理实现自定义接口

如何实现?主要分为以下两步骤

1.通过 Mapper 代理实现⾃定义接口

2.编写与方法相对应的 Mapper.xml

1.自定义接口AccountRepository

package repository;

import 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 语句。

        statement 标签可根据 SQL 执⾏的业务选择 insert、delete、update、select。 MyBatis 框架会根据规则⾃动创建接⼝实现类的代理对象

        规则:   Mapper.xml 中 namespace 为接⼝的全类名。

                      Mapper.xml 中 statement 的 id 为接⼝中对应的⽅法名。

                      Mapper.xml 中 statement 的 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="repository.AccountRepository">
    <insert id="save" parameterType="entity.Account">
        insert into t_account(username,password,age) values (#{username},#{password},#{age});
    </insert>

    <update id="update" parameterType="entity.Account">
        update t_account set username=#{username},password=#{password},age=#{age} where id=#{id};
    </update>

    <delete id="deleteById" parameterType="long">
        delete from t_account where id=#{id};
    </delete>

    <select id="findAll" resultType="entity.Account">
        select * from t_account;
    </select>

    <select id="findById" parameterType="long" resultType="entity.Account">
        select * from t_account where id =#{id};
    </select>
</mapper>

3、在 config.xml 中注册 AccountRepository.xml

<mappers>
        <mapper resource="mapper/AccountMapper.xml"></mapper>
        <mapper resource="repository/AccountRepository.xml"></mapper>
    </mappers>

4、调用接⼝的代理对象完成相关的业务操作

package Test;

import entity.Account;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import repository.AccountRepository;

import java.io.InputStream;
import java.util.List;

public class Test1 {
    public static void main(String[] args) {
        InputStream inputStream= ResolverUtil.Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        AccountRepository accountRepository=sqlSession.getMapper(AccountRepository.class);
//        List<Account> list=accountRepository.findAll();
//        for(Account account:list){
//            System.out.println(account);
//        }
//        sqlSession.close();

        //添加
//        Account account=new Account(3L,"wangwu","555555",122);
//        accountRepository.save(account);
//        sqlSession.commit();
        //通过id查找对象
//        Account account=accountRepository.findById(3L);
//        System.out.println(account);
//        sqlSession.close();

        //通过id改对象
//        Account account=accountRepository.findById(2L);
//        account.setUsername("alibaba");
//        account.setPassword("12345678");
//        account.setAge(11);
//        int result=accountRepository.update(account);
//        sqlSession.commit();
//        System.out.println(result);
//        sqlSession.close();

        //删除

        int result=accountRepository.deleteById(3L);
        sqlSession.commit();
        System.out.println(result);
        sqlSession.close();


    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

华农DrLai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值