国王陛下万万岁

导航

 

本文演示了给mybatis的mapper文件当中的SQL语句传递参数或者参数值的各种方式.

xml格式mapper文件:

<?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.galaxy.bank.dao.AccountDao">
    <select id="selectOneAccountByNo" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where actno = #{actno}
    </select>

    <select id="selectAccountById" parameterType="java.lang.Integer" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where id = #{id}
    </select>
    <select id="selectAccountByMultiParam" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where balance > #{myBalance}
          and country = #{myCountry}
    </select>
    <!--    把java对象作为参数传给mapper,对象的属性的值就是SQL语句中的#{}-->
    <select id="selectMultipleAccountByObject" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where balance > #{balance}
           or country = #{country}
    </select>

    <!--按照位置传递参数值-->
    <select id="selectAccountByBalanceAndCountry" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where balance > #{arg0}
          and country = #{arg1}
    </select>

    <!--使用${}传递参数,替换列名-->
    <select id="selectAccount$Order" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where balance > 1000.00
          order by ${myBalance}
    </select>

    <!--用Map传递参数值-->
    <select id="selectAccountByMap" resultType="com.galaxy.bank.pojo.Account">
        select id, actno, balance, holder_name, country
        from bankdb.t_account
        where balance > #{mapBalance}
          and country = #{mapCountry}
    </select>

    <update id="updateOneAccountByAccountNo">
        update bankdb.t_account
        set balance=#{balance}
        where actno = #{actno}
    </update>
    <insert id="insertOneAccount">
        insert into bankdb.t_account
        values (#{id}, #{actno}, #{balance})
    </insert>
</mapper>

 

Java Dao接口:

package com.galaxy.bank.dao;

import com.galaxy.bank.pojo.Account;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface AccountDao {





    /**
     * 1.Mybatis传递参数的方法之一:简单类型参数示例
     */
    Account selectAccountById(int id);

    Account selectAccountByNo(String accountNo);

    /**
     * 2.Mybatis传递参数的方法之二:在Java Dao接口中使用@Param注解给参数取别名
     * @Param注解的属性值就是参数的别名,参数别名将用在xml格式的mapper文件的SQL语句里
     */
    List<Account> selectAccountByMultiParam(@Param("myBalance") Double balance, @Param("myCountry") String country);

    /**
     * 3.1Mybatis传递参数的方法之三:使用Java对象作为参数
     */
    //返回值1表示更新成功,其它值表示失败
    int updateOneAccountByAccountNo(Account account);

    /**
     * 3.2使用java对象作为Mybatis的参数
     * 注意,这个例子里面new了一个Account对象,然后用setter方法给其中两个属性赋值,其余属性没有赋值;
     * 已经赋值的这两个属性的值恰好是xml格式mapper文件当中SQL语句所需要的。
     * 实际上,我们创建任意对象,再把对象属性值赋值给mapper就行。
     */
    List<Account> selectMultipleAccountByObject(Account account);

    /**
     * 4.按照位置传递参数值
     * 在mapper中用#{arg0},#{arg1}表示参数位置
     */
    List<Account> selectAccountByBalanceAndCountry(Double balance,String country);

    /**
     * 5.用Map传递参数值
     */
    List<Account> selectAccountByMap(Map<String,Object> map);

    /**
     * 6.使用${}替换列名
     * 注意:是”$“字符串替换拼接,不是占位符"#"赋值。
     */
    List<Account> selectAccount$Order(@Param("myBalance") String colBalance);


}

 

Java Pojo 类:

package com.galaxy.bank.pojo;

public class Account {
    private Long id;
    private String actno;
    private Double balance;
    private String holderName;
    private String country;

    public Account() {
    }

    public Account(Long id, String actno, Double balance, String holderName, String country) {
        this.id = id;
        this.actno = actno;
        this.balance = balance;
        this.holderName = holderName;
        this.country = country;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getActno() {
        return actno;
    }

    public void setActno(String actno) {
        this.actno = actno;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public String getHolderName() {
        return holderName;
    }

    public void setHolderName(String holderName) {
        this.holderName = holderName;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", actno='" + actno + '\'' +
                ", balance=" + balance +
                ", holderName='" + holderName + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}

 

Java 测试类:

package com.galaxy;


import com.galaxy.bank.dao.AccountDao;
import com.galaxy.bank.pojo.Account;
import com.galaxy.bank.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * date:2022-10-08
 */
public class AppTest{

    /**
     * 1.Mybatis传递参数的方法之一:简单类型参数示例
     */
    @Test
    public void test01(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);
        //简单类型参数
        Account account=dao.selectAccountById(2);
        System.out.println("Account:"+account);
        sqlSession.close();

    }

    /**
     * 2.Mybatis传递参数的方法之二:在Java Dao接口中使用@Param注解给参数取别名
     * @Param注解的属性值就是参数的别名,参数别名将用在xml格式的mapper文件的SQL语句里
     */
    @Test
    public void test02(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);
        //@Para注解命名参数
        List<Account> accounts=dao.selectAccountByMultiParam(1000.01,"UK");
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.close();

    }

    /**
     * 3.1使用java对象作为Mybatis的参数
     *
     */
    @Test
    public void test03(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);

        Account myAccount=new Account(8L,"UK100708",2212.00,"Henry Cavill","UK");
        dao.updateOneAccountByAccountNo(myAccount);

        //@Para注解命名参数
        List<Account> accounts=dao.selectAccountByMultiParam(1000.01,"UK");
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.commit();
        sqlSession.close();

    }

    /**
     * 3.2使用java对象作为Mybatis的参数
     * 注意,这个例子里面new了一个Account对象,然后用setter方法给其中两个属性赋值,其余属性没有赋值;
     * 已经赋值的这两个属性的值恰好是xml格式mapper文件当中SQL语句所需要的。
     * 实际上,我们创建任意对象,再把对象属性值赋值给mapper就行。
     */

    @Test
    public void test04(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);

        //只给其中两个属性赋值
        Account myAccount=new Account();
        myAccount.setBalance(1089.01);
        myAccount.setCountry("KR");

        List<Account> accounts=dao.selectMultipleAccountByObject(myAccount);
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.close();

    }


    /**
     * 4.按照位置传递参数值
     * 在mapper中用#{arg0},#{arg1}表示参数位置
     */
    @Test
    public void test05(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);
        List<Account> accounts=dao.selectAccountByBalanceAndCountry(2000.01,"UK");
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.close();
    }

    /**
     * 5.用Map传递参数值
     */
    @Test
    public void test06(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);

        Map<String,Object> accountMap=new HashMap<>();
        accountMap.put("mapBalance",2001.00);
        accountMap.put("mapCountry","UK");

        List<Account> accounts=dao.selectAccountByMap(accountMap);
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.close();
    }

    /**
     * 6.使用${}替换列名
     * 这里dao.selectAccount$Order("balance")中的"balance"将替换mapper当中的SQL语句的${}的内容
     * 注意:是”$“字符串替换拼接,不是占位符"#"赋值。
     */
    @Test
    public void test07(){
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        AccountDao dao = sqlSession.getMapper(AccountDao.class);
        List<Account> accounts=dao.selectAccount$Order("balance");
        accounts.forEach(account -> {System.out.println("Account:"+account);});

        sqlSession.close();
    }

}

 

------------------------------------------------------------------------------------------------
2022年10月8日,农历九月十三,星期六,阴天有小雨,气温较冷,上海。
国庆节7天假期后的第一天,今日不调休,不上班.
-------------------------------------------------------------------------------------------------

posted on 2022-10-08 22:36  国王陛下万万岁  阅读(373)  评论(0编辑  收藏  举报