老规矩,上配置文件
<?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.iamzhuwh.dao.DinnerTableDao" >
<select id ="selectAllTables" resultType ="com.iamzhuwh.entity.DinnerTable" >
select * from dinner_table
</select >
<select id ="selectTableById" resultType ="com.iamzhuwh.entity.DinnerTable" >
select * from dinner_table where id = #{id}
</select >
</mapper >
实体类
public interface DinnerTableDao {
List<DinnerTable> selectAllTables();
DinnerTable selectTableById(int id);
}
public class DinnerTable implements Serializable {
private int id;
private int table_id;
private String table_name;
private int reservation_status;
private Date reservation_time;
...省略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>
int insert(DinnerTable dinnerTable);
dinnerTableDao.insert(dinnerTable);
public class DinnerTable implements Serializable {
private int id;
private int table_id;
private String table_name;
private int reservation_status;
private Date reservation_time;
}
觉得上面的麻烦?没关系,还有更简单的
<select id="selectTableById" resultType="com.iamzhuwh.entity.DinnerTable" >
select * from dinner_table where id = #{id} and table_id = #{table_id}
</select >
<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 ]
任性?就是要指定名称不要数字?好吧,还可以这样改
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);
}
<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 ]