欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java應(yīng)用開(kāi)發(fā)之Mybatis通過(guò)Mapper代理自定義接口的實(shí)現(xiàn)

 更新時(shí)間:2021年09月15日 14:09:22   作者:DrLai  
這篇文章主要介紹了java應(yīng)用開(kāi)發(fā)之Mybatis通過(guò)Mapper代理自定義接口的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助

如何實(shí)現(xiàn)?主要分為以下兩步驟

  • 1.通過(guò) Mapper 代理實(shí)現(xiàn)⾃定義接口
  • 2.編寫與方法相對(duì)應(yīng)的 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.創(chuàng)建接⼝對(duì)應(yīng)的 Mapper.xml,定義接口方法對(duì)應(yīng)的 SQL 語(yǔ)句。

statement 標(biāo)簽可根據(jù) SQL 執(zhí)⾏的業(yè)務(wù)選擇 insert、delete、update、select。 MyBatis 框架會(huì)根據(jù)規(guī)則⾃動(dòng)創(chuàng)建接⼝實(shí)現(xiàn)類的代理對(duì)象

規(guī)則:

  • Mapper.xml 中 namespace 為接⼝的全類名。
  • Mapper.xml 中 statement 的 id 為接⼝中對(duì)應(yīng)的⽅法名。
  • Mapper.xml 中 statement 的 parameterType 和接⼝中對(duì)應(yīng)⽅法的參數(shù)類型⼀致。
  • Mapper.xml 中 statement 的 resultType 和接⼝中對(duì)應(yīng)⽅法的返回值類型⼀致。
<?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 中注冊(cè) AccountRepository.xml

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

4.調(diào)用接⼝的代理對(duì)象完成相關(guān)的業(yè)務(wù)操作

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();
        //通過(guò)id查找對(duì)象
//        Account account=accountRepository.findById(3L);
//        System.out.println(account);
//        sqlSession.close();
        //通過(guò)id改對(duì)象
//        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應(yīng)用開(kāi)發(fā)之Mybatis通過(guò)Mapper代理自定義接口的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Mybatis通過(guò)Mapper代理自定義接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入理解Java中的字符串類型

    深入理解Java中的字符串類型

    這篇文章主要介紹了Java中的字符串類型,需要的朋友可以參考下
    2014-02-02
  • Java8日期類LocalDate、LocalTime和LocalDateTime使用方法詳解

    Java8日期類LocalDate、LocalTime和LocalDateTime使用方法詳解

    這篇文章主要給大家介紹了關(guān)于Java8日期類LocalDate、LocalTime和LocalDateTime使用方法的相關(guān)資料,LocalDateTime是JDK1.8出現(xiàn)的新特性,解決線程不安全的問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • 詳解netty中的frame解碼器

    詳解netty中的frame解碼器

    netty為我們提供了一些合適的frame解碼器,通過(guò)使用這些frame解碼器可以有效的簡(jiǎn)化我們的工作,這篇文章主要介紹了netty中的frame解碼器,需要的朋友可以參考下
    2022-04-04
  • arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析

    arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析

    這篇文章主要為大家介紹了arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • SpringBoot自定義啟動(dòng)器Starter流程詳解

    SpringBoot自定義啟動(dòng)器Starter流程詳解

    SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動(dòng)掃描到要加載的信息并啟動(dòng)相應(yīng)的默認(rèn)配置。starter讓我們擺脫了各種依賴庫(kù)的處理,需要配置各種信息的困擾
    2022-11-11
  • Spring使用@Conditional進(jìn)行條件裝配的實(shí)現(xiàn)

    Spring使用@Conditional進(jìn)行條件裝配的實(shí)現(xiàn)

    在spring中有些bean需要滿足某些環(huán)境條件才創(chuàng)建某個(gè)bean,這個(gè)時(shí)候可以在bean定義上使用@Conditional注解來(lái)修飾,所以本文給大家介紹了Spring使用@Conditional進(jìn)行條件裝配的實(shí)現(xiàn),文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Maven中dependency和plugins的繼承與約束

    Maven中dependency和plugins的繼承與約束

    這篇文章主要介紹了Maven中dependency和plugins的繼承與約束,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java源碼解析之GenericDeclaration詳解

    Java源碼解析之GenericDeclaration詳解

    這篇文章主要介紹了Java源碼解析之GenericDeclaration詳解。有句古話說(shuō)得好,源碼能使人快樂(lè)!這里分享給大家,供需要的朋友參考。
    2017-10-10
  • springBoot中的CORS跨域注解@CrossOrigin詳解

    springBoot中的CORS跨域注解@CrossOrigin詳解

    這篇文章主要介紹了springBoot中的CORS跨域注解@CrossOrigin詳解,通常,服務(wù)于?JS?的主機(jī)(例如?example.com)與服務(wù)于數(shù)據(jù)的主機(jī)(例如?api.example.com)是不同的,在這種情況下,CORS?可以實(shí)現(xiàn)跨域通信,需要的朋友可以參考下
    2023-12-12
  • 詳解 Java繼承關(guān)系下的構(gòu)造方法調(diào)用

    詳解 Java繼承關(guān)系下的構(gòu)造方法調(diào)用

    這篇文章主要介紹了詳解 Java繼承關(guān)系下的構(gòu)造方法調(diào)用的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10

最新評(píng)論