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();
    }
}

posted on 2023-02-14 15:21  张铁蛋666  阅读(223)  评论(0)    收藏  举报

导航