java应用开发之Mybatis通过Mapper代理自定义接口的实现

时间:2022-04-20 06:59:10

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

  • 1.通过 Mapper 代理实现⾃定义接口
  • 2.编写与方法相对应的 Mapper.xml

1.自定义接口AccountRepository

?
1
2
3
4
5
6
7
8
9
10
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 和接⼝中对应⽅法的返回值类型⼀致。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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

?
1
2
3
4
<mappers>
        <mapper resource="mapper/AccountMapper.xml"></mapper>
        <mapper resource="repository/AccountRepository.xml"></mapper>
    </mappers>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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();
     }
}

以上就是java应用开发之Mybatis通过Mapper代理自定义接口的实现的详细内容,更多关于Mybatis通过Mapper代理自定义接口的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/DrLai/article/details/119682671