关于Mybatis的Dao动态代理多种传参方式

老规矩,上配置文件

  • 不知道配置文件写的啥,无所谓,往下看
<?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">

<!-- 要动态代理,这里的namespace要指定你要代理的是哪个dao,全路径名 -->
<mapper namespace="com.iamzhuwh.dao.DinnerTableDao">

    <!-- 
        这里的方法id与参数都要与dao的某个方法一一对应
        id == 方法名
        resultType == 以什么形式返回结果,
                            我这里是查询到的每条数据都以对象DinnerTable的形式返回
        当调用dao的方法的时候,mybatis会参照这里的配置,
                            找到对应的id,然后执行下面的sql语句
    -->
    <select id="selectAllTables" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table
    </select>

    <!-- 同上,注意一点,查询语句是用select标签,insert语句请用insert标签 -->
    <select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table where id = #{id}
    </select>
</mapper>

实体类

  • 别急,继续往下看
//dao接口
public interface DinnerTableDao {
    List<DinnerTable> selectAllTables();
    DinnerTable selectTableById(int id);
}
//实体类
public class DinnerTable implements Serializable{
    private int id; //INT AUTO_INCREMENT PRIMARY KEY,
    private int table_id; //INT UNIQUE, -- 编号
    private String table_name; //VARCHAR(20) UNIQUE, -- 桌名
    private int reservation_status; //INT DEFAULT 0, -- 状态
    private Date reservation_time; //DATETIME -- 预定时间
    ...省略get、set方法
}
//对应数据库
CREATE TABLE dinner_table(
    id INT AUTO_INCREMENT PRIMARY KEY,
    table_id INT, -- 编号
    table_name VARCHAR(20), -- 桌名
    reservation_status INT DEFAULT 0, -- 状态
    reservation_time DATETIME -- 预定时间
);

重点

  • 测试代码, 详见注释
package com.iamzhuwh.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.iamzhuwh.dao.DinnerTableDao;
import com.iamzhuwh.entity.DinnerTable;

public class testDao {
    public static void main(String[] args) {
        try {

            /**
                映射文件:mapper-daoTest.xml
                接口:DinnerTableDao.java

                使用session的getMapper来代理Dao

                以前我们都是写好接口:xxxdao,然后写实现类:xxxDaoImpl
                现在可以将实现类省略,通过配置文件的方式,让session去代理

                <!-- mapper的namespace要写Dao接口的全路径名 -->
                <mapper namespace="com.iamzhuwh.dao.DinnerTableDao">

                    <!-- select的id要与Dao接口里面定义方法名字一毛一样!!! -->
                    <select id="selectAllTables" resultType="com.iamzhuwh.entity.DinnerTable">
                        select * from dinner_table
                    </select>

                    <!-- resultType根据sql语句返回的类型自己选择,一般使用对应的实体类比较方便 -->
                    <select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
                        select * from dinner_table where id = #{id}
                    </select>

                </mapper>
             */

            /** 第一步,通过流来加载配置文件 */
            InputStream is = Resources.getResourceAsStream("mybatis.cfg.xml");
            /** 第二步,创建session工厂 */
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory build = builder.build(is);
            /** 第三步,创建当前session */
            SqlSession session = build.openSession();

            /** 实现代理,获取Dao对象 */
            DinnerTableDao dao = session.getMapper(DinnerTableDao.class);

            /** 调用方法 */
            List<DinnerTable> list = dao.selectAllTables();
            System.out.println("list:"+list);

            System.out.println("-----------=========");

            /** 调用方法 ,sql语句有#{id},这里直接将id传进去就可以了 ,不需要再手动写那么多*/
            DinnerTable table = dao.selectTableById(5);
            System.out.println("table:"+table);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上面只测试了一个参数的,如果多参数呢!!!

  • 这个是错误示范
配置
<select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table where id = #{id} and table_id = #{table_id}
</select>

//调用
dao.selectTableById(5,1266547476);

//返回结果
Parameter 'id' not found. Available parameters are [0, 1, param1, param2]
  • 正确示范,多参数传递请使用parameterType指定对象
//配置
<insert id="insert" parameterType="com.iamzhuwh.entity.DinnerTable">
    insert into dinner_table(table_id,table_name) values(#{table_id},#{table_name})
</insert>

//对应dao的方法
int insert(DinnerTable dinnerTable);

//调用
dinnerTableDao.insert(dinnerTable);

//DinnerTable
public class DinnerTable implements Serializable{
    private int id; //INT AUTO_INCREMENT PRIMARY KEY,
    private int table_id; //INT UNIQUE, -- 编号
    private String table_name; //VARCHAR(20) UNIQUE, -- 桌名
    private int reservation_status; //INT DEFAULT 0, -- 状态
    private Date reservation_time; //DATETIME -- 预定时间
}

觉得上面的麻烦?没关系,还有更简单的

  • 把上面的错误示范改一下就好了
//错误配置
<select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table where id = #{id} and table_id = #{table_id}
</select>

//正确配置,将占位符的名称改为数字,#{id},#{table_id}改为#{0},#{1}
<select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table where id = #{0} and table_id = #{1}
</select>

//调用
dao.selectTableById(5,1266547476);

//返回结果
[id=5, table_id=1266547476, table_name=东京, reservation_status=0, reservation_time=null]

任性?就是要指定名称不要数字?好吧,还可以这样改

  • 改下dao接口的内容
//原来的
public interface DinnerTableDao {
    List<DinnerTable> selectAllTables();
    DinnerTable selectTableById(int id,int table_id);
}

//修改的
public interface DinnerTableDao {
    List<DinnerTable> selectAllTables();
    DinnerTable selectTableById(@Param("id")int id,@Param("table_id")int table_id);
}

//配置的名称,与dao的方法上的参数做绑定,指定哪个参数传给谁
<select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable">
        select * from dinner_table where id = #{id} and table_id = #{table_id}
</select>

//调用
dao.selectTableById(5,1266547476);

//返回结果
[id=5, table_id=1266547476, table_name=东京, reservation_status=0, reservation_time=null]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值