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

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

  • 1.自定义接口,定义相关业务方法。

    • (1)自定义接口

      package com.southwind.repository;
      
      import com.southwind.entity.Account;
      
      import java.util.List;
      
      /**
       * @author yyz
       * @date 2021/9/16 15:37
       */
      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的paramenterType和接口中对应方法的参数类型一致。
    • Mapper.xml 中statement的resultrType和接口中对应方法的返回值类型一致。
    <?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 into t_account(username,password,age)values(#{username},#{password},#{age})
    </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_accpunt where id=#{id}
        </delete>
        <select id="fianAll" resultType="com.southwind.entity.Account">
            select *from t_account
        </select>
        <select id="fingById" parameterType="long" resultType="com.southwind.entity.Account">
            select *from t_account where id=#{id}
        </select>
    </mapper>
    

3.在config.xml(全局配置文件)中注册AccountRepository.xml

<!--注册AccountMapper.xml-->
<mappers>
    <mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>

    <mapper resource="com/southwind/repository/AccountReposity.xml"></mapper>
</mappers>

4。调用接口和代理对象完成相关的业务操作

package com.southwind.test;

import com.southwind.entity.Account;
import com.southwind.mapper.test.Test;
import com.southwind.repository.AccountRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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

/**
 * @author yyz
 * @date 2021/9/16 16:22
 */
public class Test2 {
    public static void main(String[] args) {
        InputStream inputStream= Test.class.getClassLoader().getResourceAsStream(name:"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);
        }
        //添加对象
        Account account=new Account(id:2L,username:"李四",password:"12123",23);
         accountRepository.save(account);
         sqlSession.commit();
//通过id查询
        Acount account1=accountRepository.findById(2L);
        System.out.println(account);
sqlSession.close();
//修改对象
        Account account1=accountRepository.findById(2L);
        account.setUsername("小米");
        account.getPassword("000");
        int result=accountRepository.update(account);
        sqlSession.commit();
        System.out.println(result);
        sqlSession.close();
//删除
        int result =accountRepository.deleteById(2L);
        System.out.println(result);
        sqlSession.commit();
        sqlSession.close();
//删除
    }
}
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

已黑化的小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值