java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis通过Mapper代理自定义接口

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

作者:DrLai

这篇文章主要介绍了java应用开发之Mybatis通过Mapper代理自定义接口的实现方式,有需要的朋友可以借鉴参考下,希望能够有所帮助

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

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 框架会根据规则⾃动创建接⼝实现类的代理对象

规则:

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

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

您可能感兴趣的文章:
阅读全文