语法糖
<sql id="colums">
person_id, name, gender, person_addr, birthday
</sql>
<select id="selectPersonAll" resultMap="">
select <include refid="colums" /> from person
</select>
// parameterType 可以使用别名,int 可以代替java.lang.Integer ,
// map 可以代替 java.lang.Map , 其他自定义类,必须通过mybatis配置文件,配置别名使用
<update id="update" parameterType="com.rl.model1.Person">
update person t
<set>
<if test="name != null">
t.name = #{name},
</if>
<if test="gender != null">
t.gender = #{gender},
</if>
<if test="person_addr != null">
t.person_addr = #{person_addr},
</if>
</set>
where t.person_id = #{personId}
</update>
<update id="updateWhere" parameterType="com.rl.model1.Person">
update person t set
t.name = #{name},
t.gender = #{gender}
t.person_addr = #{personAddr}
where t.person_id = #{personId}
</update>
<!-- mysql 删除不可以用别名 -->
<delete id="enumerationDelete" parameterType="enumeration">
delete from enumeration_list where enumId = #{enumId}
</delete>
<!-- 批量删除,只能少量数据,大数据会溢出 -->
<delete id="enumerationDeleteMap" parameterType="Map">
delete from enumeration_list where enumId in
<foreach collection="ids" open="(" close=")" item="enumId" separator="," index="index">
#{enumId}
</foreach>
</delete>
<insert id="enumerationInsert" parameterType="enumeration">
<!--
selectKey 是做主键返回的
keyProperty 接受主键返回的属性
order: "AFTER" 是mysql中使用的 "BEFORE" 指的是顺序,生成主键的顺序 自增序列
resultType 返回主键的数据类型
生成主键的sql select LAST_INSERT_ID()
-->
<selectKey keyProperty="enumId" order="AFTER" resultType="java.lang.Integer">
<!-- 此处是mysql的写法,oracle中不一样 -->
select LAST_INSERT_ID()
</selectKey>
insert into enumeration_list (enumId, typeId, name, parentId)
values
<foreach collection="enumList" separator="," item="enum">
(#{enum.enumId}, #{enum.typeId}, #{enum.name}, #{enum.parentId})
</foreach>
</insert>
<insert id="enumerationInsert" parameterType="enumeration">
<!--
selectKey 是做主键返回的
keyProperty 接受主键返回的属性
order: "AFTER" 是mysql中使用的 "BEFORE" 指的是顺序,生成主键的顺序 自增序列
resultType 返回主键的数据类型
生成主键的sql select LAST_INSERT_ID()
-->
<selectKey keyProperty="enumId" order="AFTER" resultType="java.lang.Integer">
<!-- 此处是mysql的写法,oracle中不一样 -->
select LAST_INSERT_ID()
</selectKey>
insert into enumeration_list (enumId, typeId, name, parentId)
value(#{enumId}, #{typeId}, #{name}, #{parentId})
</insert>
### 转义语法 ###
<![CDATA[
select * from user u where u.datetime > #{date}
]]>
### parameterType的用法,可以是包装类型与任意Class,Map , 通过getUserId方法获取值的 ###
<select id="userSelectOne" parameterType="com.lijunyang.model.user" resultMap="user">
<![CDATA[
select * from user u where u.datetime > #{datatime}
]]>
</select>
<select id="userSelectOne" parameterType="java.util.Map" resultMap="user">
select * from PersonTest2 p where p.name like '%${name}%'
</select>
<select id="enumerationSelectMap" parameterType="java.util.Map" resultMap="enumeration">
select * from enumeration_list e
<where>
<if test="enumId != null">
e.enumId = #{enumId}
</if>
<if test="typeId != null">
and e.typeId = #{typeId}
</if>
<if test="name != null">
and e.name = #{name}
</if>
<if test="parentId != null">
and e.parentId = #{parentId}
</if>
</where>
</select>
<select id="enumerationMap" parameterType="Map">
select * from enumeration_list where enumId in
<foreach collection="ids" open="(" close=")" item="enumId" separator="," index="index">
#{enumId}
</foreach>
</select>
一对一
一个班主任只属于一个班级,一个班级也只能有一个班主任
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
classObj = SELECT FROM class WHERE c_id = 1;
SELECT FROM teacher WHERE t_id = 1; //1 是上一个查询得到的teacher_id的值
表设计,使用MYSQL
CREATE TABLE IF NOT EXISTS `class_list` (
`c_id` int(18) UNSIGNED NOT NULL AUTO_INCREMENT,
`c_name` varchar(255) NOT NULL,
`t_id` int(18) UNSIGNED NOT NULL,
PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `teacher_list` (
`t_id` int(18) UNSIGNED NOT NULL AUTO_INCREMENT,
`t_name` varchar(255) NOT NULL,
PRIMARY KEY (`t_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=0;
alter table class_list add constraint `FK_TEACHERID` foreign key(`t_id`) references teacher_list(`t_id`);
SET FOREIGN_KEY_CHECKS=1;
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样保证了namespace的值是唯一的-->
<mapper namespace="com.yc.mybatis.test.classMapper">
<!--
方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
封装联表查询的数据(去除重复的数据)
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
-->
<select id="getClass" parameterType="int" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.teacher_id=#{id}
</select>
<!-- resultMap:映射实体类和字段之间的一一对应的关系 -->
<resultMap type="Classes" id="getClassMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--
方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值
property:别名(属性名) column:列名 -->
<!-- 把teacher的字段设置进去 -->
<select id="getClass1" parameterType="int" resultMap="getClassMap1">
select * from class where c_id=#{id}
</select>
<resultMap type="Classes" id="getClassMap1">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" select="getTeacher"/>
</resultMap>
<select id="getTeacher" parameterType="int" resultType="Teacher">
select t_id id,t_name name from teacher where t_id =#{id}
</select>
</mapper>
property | 对象属性的名称 |
---|---|
javaType | 对象属性的类型 |
column | 所对应的外键字段名称 |
select | 使用另一个查询封装的结果 |
一对多 and 多对一
一个顾客对应多个订单,而一个订单只能对应一个客户。
一个订单对应多个商品
1表示当前的orderId
CREATE TABLE `user` (
`userId` int(18) unsigned NOT NULL,
`userName` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`Telephone` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT '',
`createTime` datetime NOT NULL,
`updatePasswordTime` datetime NOT NULL,
PRIMARY KEY (`userId`),
UNIQUE KEY `userName` (`userName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `goods_list` (
`goodsId` int(18) unsigned NOT NULL AUTO_INCREMENT,
`goodsName` varchar(255) NOT NULL,
`goodsPrice` double(11,2) NOT NULL,
`goodsUnit` varchar(255) NOT NULL,
`goodsIntroduce` varchar(255) DEFAULT '',
`pathId` int(18) NOT NULL,
`classType` int(18) DEFAULT NULL,
PRIMARY KEY (`goodsId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE IF NOT EXISTS `order_list` (
`orderId` int(18) UNSIGNED NOT NULL AUTO_INCREMENT,
`orderStartTime` datetime NOT NULL,
`orderEndTime` datetime,
`orderTotalPrice` double(11,2) NOT NUll,
`userId` int(18) UNSIGNED NOT NULL,
`orderGoodsListNum` int(18) UNSIGNED NOT NULL,
PRIMARY KEY (`orderId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table order_list add constraint `FK_ORDERID`
foreign key(`userId`) references user(`userId`);
CREATE TABLE `order_goods_list` (
`orderId` int(18) UNSIGNED NOT NULL,
`goodsId` int(18) UNSIGNED NOT NULL,
`goodsCount` int(18) UNSIGNED NOT NULL,
`goodsTotalPrice` double(11,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table order_goods_list add constraint `FK_ORDERID_GOODS`
foreign key(`orderId`) references order_list(`orderId`);
alter table order_goods_list add constraint `FK_GOODSID`
foreign key(`goodsId`) references goods_list(`goodsId`);
select * from user;
select * from goods_list;
INSERT INTO `order_list` (orderStartTime, orderTotalPrice, userId, orderGoodsListNum)
VALUES ('2019-03-18 16:12:34', 10.0, 10010, 10);
select * from order_goods_list ol;
select * from order_goods_list ol where ol.orderId = 1;
update order_list set orderTotalPrice = 310.00 where orderId = 1;
update order_list set orderTotalPrice = 310.00, orderGoodsListNum = 30 where orderId = 1;
INSERT INTO `order_goods_list` (orderId, goodsId, goodsCount, goodsTotalPrice)
VALUES (1, 29, 10, 1.0);
INSERT INTO `order_goods_list` (orderId, goodsId, goodsCount, goodsTotalPrice)
VALUES (1, 30, 10, 10.0);
INSERT INTO `order_goods_list` (orderId, goodsId, goodsCount, goodsTotalPrice)
VALUES (1, 31, 10, 10.0);
INSERT INTO `order_goods_list` (orderId, goodsId, goodsCount, goodsTotalPrice)
VALUES (1, 32, 10, 10.0);
select * from order_goods_list;
select * from order_goods_list ogl where ogl.orderId = 1;
select * from order_goods_list ol where ol.orderId = 1 limit 0,3;
select * from order_list o, order_goods_list ol where o.orderId = 1 and o.orderId = ol.orderId;
## 包含 in
select * from goods_list where goodsId in (29, 30, 31, 32);
## between and 在什么什么之间
select * from goods_list where goodsId between 29 and 32;
## is
select * from goods where classType is (not) NULL;
select * from goods_list where classType is not NUll;
select * from goods_list where classType is NUll;
#select distinct 字段名 from 表名;字段名表示要过滤重复记录的字段
## like %
## % 表示匹配任意字符
## _ 表示匹配单个字符
#### 如果需要查询带有 % 或 _ 的数据,由于 % 和 _ 是通配符,则需要使用 \ 进行转义
## \% 表示 %,\_ 表示 _
## and(&&) 有时在查询时为了查询结果更加精确,需要多个限条件,这时就需要 and(&&) 来连接条件
## or(||) 有时在查询时,只需要数据满足某些条件中的某一个,这时就需要使用 or(||) 来连接条件
select * from user where userName like 'lijunyang%';
### count()函数:统计记录条数 select count(记录) from 表名
select count(userId) from user;
### sum()函数:计算表中某个字段值的总和,select sum(字段名) from 表名
### avg()函数:计算表中某个字段的平均值 select avg(字段名) from 表名
### max()函数:返回表中某个字段中的最大值
### min()函数:返回表中某个字段中的最小值
### 在对数据表中的数据进行统计时,需要将数据按照一定的特征分组统计,此时就需
### 要使用分组查询 select 查询内容 from 表名 group by 分组依据 [having表达式条件]
### select 查询内容 from 表名 order by 排序条件 asc/desc,asc表示升序 desc表示降序
### 分页查询 select 查询内容 from 表名 limit 偏移量m,记录数n
where 与 having:
where 与 having关键字都用于设置条件表达式对查询结果进行过滤,区别是having后面可以跟聚合
函数,而where不能,通常having关键字都与group by 一起使用,表示对分组后的数据进行过滤
最终一对多select语句
分为几种查询方式:sql查询,一次性查出所有关联信息用下面的语句
select * from order_list o, order_goods_list ogl, goods_list ol where
o.orderId = 1 and
o.orderId = ogl.orderId and
ol.goodsId in
(select goodsId from order_goods_list where orderId = o.orderId) limit 0,3
## 查询表格型数据,与下面的面向对象型不一样
在实际工作中,配合ORM框架配置,如Mybatis等,查询要分为2次查询
## 首先查询订单信息
select * from order_list where orderId = #{orderId}
## 在查询订单id关联的所有商品信息
select * from order_goods_list ogl, goods_list ol where ogl.orderId = #{orderId} and ol.goodsId = ogl.goodsId
## 最好在将数据合并,就是一份面向对象结构的数据了
注意事项
注意,此种设计只为了学习使用,实际场景中可以使用冗余字段,将order_goods_list,包含goods_list的所有字段,形成冗余,这样只需要查询2张表即可
Goods 商品类 : 映射商品表
package com.lijunyang.model;
/**
* 商品类
* 商品名称
* 商品ID
* 商品价格
* 商品单位
* 商品介绍
* 商品图片路径ID
* */
public class Goods {
private String goodsName;
private Integer goodsId;
private Double goodsPrice;
private String goodsUnit;
private String goodsIntroduce;
private Integer pathId;
private FileSavePath filePath;
private Integer classType;
private Enumeration enumeration;
public Integer getClassType() {
return classType;
}
public void setClassType(Integer classType) {
this.classType = classType;
}
public Enumeration getEnumeration() {
return enumeration;
}
public void setEnumeration(Enumeration enumeration) {
this.enumeration = enumeration;
}
public Integer getPathId() {
return pathId;
}
public void setPathId(Integer pathId) {
this.pathId = pathId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public Double getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Double goodsPrice) {
this.goodsPrice = goodsPrice;
}
public String getGoodsUnit() {
return goodsUnit;
}
public void setGoodsUnit(String goodsUnit) {
this.goodsUnit = goodsUnit;
}
public String getGoodsIntroduce() {
return goodsIntroduce;
}
public void setGoodsIntroduce(String goodsIntroduce) {
this.goodsIntroduce = goodsIntroduce;
}
public FileSavePath getFilePath() {
return filePath;
}
public void setFilePath(FileSavePath filePath) {
this.filePath = filePath;
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", goodsId=" + goodsId +
", goodsPrice=" + goodsPrice +
", goodsUnit='" + goodsUnit + '\'' +
", goodsIntroduce='" + goodsIntroduce + '\'' +
", pathId=" + pathId +
", filePath=" + filePath +
", classType=" + classType +
", enumeration=\n" + enumeration +
'}';
}
}
OrderGoods类 订单商品表 : 映射订单商品表
package com.lijunyang.model;
import java.util.List;
/**
* 订单商品类
* 订单ID
* 商品
* 商品数量
* 商品价格
* */
public class OrderGoods {
private Integer orderId;
private List<Goods> goodsList;
private Integer goodsCount;
private Double goodsTotalPrice;
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public List<Goods> getGoodsList() {
return goodsList;
}
public void setGoodsList(List<Goods> goodsList) {
this.goodsList = goodsList;
}
public Integer getGoodsCount() {
return goodsCount;
}
public void setGoodsCount(Integer goodsCount) {
this.goodsCount = goodsCount;
}
public Double getGoodsTotalPrice() {
return goodsTotalPrice;
}
public void setGoodsTotalPrice(Double goodsTotalPrice) {
this.goodsTotalPrice = goodsTotalPrice;
}
@Override
public String toString() {
return "OrderGoods{" +
"orderId=" + orderId +
", goodsList=" + goodsList +
", goodsCount=" + goodsCount +
", goodsTotalPrice=" + goodsTotalPrice +
'}';
}
}
OrderGoods类的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.lijunyang.mapper.orderGoodsMapper">
<resultMap type="orderGoods" id="orderGoods">
<id column="orderId" property="orderId" />
<result column="goodsCount" property="goodsCount"/>
<result column="goodsTotalPrice" property="goodsTotalPrice"/>
<collection property="goodsList" javaType="ArrayList" ofType="com.lijunyang.model.Goods">
<id column="goodsId" property="goodsId" />
<result column="goodsName" property="goodsName"/>
<result column="goodsPrice" property="goodsPrice"/>
<result column="goodsUnit" property="goodsUnit"/>
<result column="goodsIntroduce" property="goodsIntroduce"/>
<result column="pathId" property="pathId"/>
<result column="classType" property="classType"/>
<association property="filePath" javaType="com.lijunyang.model.FileSavePath">
<result column="fileId" property="fileId" />
<result column="path" property="path"/>
</association>
<association property="enumeration" javaType="com.lijunyang.model.Enumeration">
<result column="enumId" property="enumId" />
<result column="typeId" property="typeId"/>
<result column="parentId" property="parentId"/>
<result column="name" property="name"/>
</association>
</collection>
</resultMap>
<select id="orderGoodsSelectMap" parameterType="java.util.Map" resultMap="orderGoods">
select * from goods_list gl, order_goods_list ogl ,fileSavePath f, enumeration_list e
<where>
ogl.orderId = #{orderId}
and ogl.goodsId = gl.goodsId
and gl.pathId = f.fileId
and e.enumId = gl.classType
</where>
</select>
<select id="orderGoodsSelectPagesMap" parameterType="java.util.Map" resultMap="orderGoods">
select * from goods_list gl, order_goods_list ogl ,fileSavePath f, enumeration_list e
<where>
ogl.orderId = #{orderId}
and ogl.goodsId = gl.goodsId
and gl.pathId = f.fileId
and e.enumId = gl.classType
</where>
limit #{page},#{pageSize}
</select>
<select id="orderGoodsSelectPagesThreeMap" parameterType="java.util.Map" resultMap="orderGoods">
select * from goods_list gl, order_goods_list ogl ,fileSavePath f, enumeration_list e
<where>
ogl.orderId = #{orderId}
and ogl.goodsId = gl.goodsId
and gl.pathId = f.fileId
and e.enumId = gl.classType
</where>
limit 0, 3
</select>
</mapper>
OrderGoods junit 单元测试
package com.lijunyang.test;
import com.lijunyang.model.Order;
import com.lijunyang.model.OrderGoods;
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 org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OrderGoodsDaoTest {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
//注册接口类
sessionFactory.getConfiguration().addMapper(Order.class);
}
@Test
public void select() {
// 创建SqlSession
SqlSession session = sessionFactory.openSession();
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("orderId", 1);
List<OrderGoods> list = session.selectList("com.lijunyang.mapper.orderGoodsMapper.orderGoodsSelectMap", map);
for (OrderGoods o : list) {
System.out.println(o);
}
} finally {
session.close();
}
}
@Test
public void selectPages() {
// 创建SqlSession
SqlSession session = sessionFactory.openSession();
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("orderId", 1);
map.put("page", 0);
map.put("pageSize", 3);
List<OrderGoods> list = session.selectList("com.lijunyang.mapper.orderGoodsMapper.orderGoodsSelectPagesMap", map);
for (OrderGoods o : list) {
System.out.println(o);
}
} finally {
session.close();
}
}
}
Order类 订单类 : 映射订单表
package com.lijunyang.model;
import java.util.Date;
import java.util.List;
/**
* 订单商品类
* 订单ID
* 用户ID
* 订单开始时间
* 确认订单时间
* 商品总数量
* 商品总价格
当用户点击下单时,orderStartTime 赋值,此时允许用户修改订单
当用户点击确认订单时,orderEndTime 赋值,此时不在允许用户修改订单
可以通过userId获取到用户未确认的订单,只允许有一个未确认订单
* */
public class Order {
private Integer orderId;
private Integer userId;
private Date orderStartTime;
private Date orderEndTime;
private Integer orderGoodsListNum;
private Double orderTotalPrice;
private OrderGoods OrderGoods;
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Date getOrderStartTime() {
return orderStartTime;
}
public void setOrderStartTime(Date orderStartTime) {
this.orderStartTime = orderStartTime;
}
public Date getOrderEndTime() {
return orderEndTime;
}
public void setOrderEndTime(Date orderEndTime) {
this.orderEndTime = orderEndTime;
}
public Integer getOrderGoodsListNum() {
return orderGoodsListNum;
}
public void setOrderGoodsListNum(Integer orderGoodsListNum) {
this.orderGoodsListNum = orderGoodsListNum;
}
public Double getOrderTotalPrice() {
return orderTotalPrice;
}
public void setOrderTotalPrice(Double orderTotalPrice) {
this.orderTotalPrice = orderTotalPrice;
}
public com.lijunyang.model.OrderGoods getOrderGoods() {
return OrderGoods;
}
public void setOrderGoods(com.lijunyang.model.OrderGoods orderGoods) {
OrderGoods = orderGoods;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", userId=" + userId +
", orderStartTime=" + orderStartTime +
", orderEndTime=" + orderEndTime +
", orderGoodsListNum=" + orderGoodsListNum +
", orderTotalPrice=" + orderTotalPrice +
", OrderGoods=" + OrderGoods +
'}';
}
}
Order类的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.lijunyang.mapper.orderMapper">
<resultMap type="order" id="order">
<id column="orderId" property="orderId" />
<result column="userId" property="userId"/>
<result column="orderStartTime" property="orderStartTime"/>
<result column="orderEndTime" property="orderEndTime"/>
<result column="orderGoodsListNum" property="orderGoodsListNum"/>
<result column="orderTotalPrice" property="orderTotalPrice"/>
<association property="orderGoods"
javaType="com.lijunyang.model.OrderGoods"
column="orderId"
select="com.lijunyang.mapper.orderGoodsMapper.orderGoodsSelectPagesThreeMap"/>
</resultMap>
<select id="orderIdSelectMap" parameterType="java.util.Map" resultMap="order">
select * from order_list o
<where>
o.orderId = #{orderId}
</where>
</select>
<select id="orderSelectMap" parameterType="java.util.Map" resultMap="order">
select * from order_list o
</select>
</mapper>
Order junit 单元测试
package com.lijunyang.test;
import com.lijunyang.model.Enumeration;
import com.lijunyang.model.Order;
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 org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OrderDaoTest {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
//注册接口类
sessionFactory.getConfiguration().addMapper(Order.class);
}
@Test
public void select() {
// 创建SqlSession
SqlSession session = sessionFactory.openSession();
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("orderId", 1);
List<Order> list = session.selectList("com.lijunyang.mapper.orderMapper.orderSelectMap", map);
for (Order o : list) {
System.out.println(o);
}
} finally {
session.close();
}
}
}
多对多
上面的表设计中,商品表,订单商品表,订单表
其中商品表与订单表的关系是, 多对多,因为,订单商品表与商品表在结构上是多对多
订单表与订单商品表的关系是,一对多
通过中间表,订单商品表维护他们之间的关系
商品表,多个商品会重复对应多条订单
订单表,这里只有一条,假设有多条的情况,如何和商品表相互对应,多条订单会重复对应多个商品
中间表(关系表)
还有一个例子是
多对多关系处理:
通过学生选课了解多对多问题的处理:
在多对多中在一个表中添加一个字段就行不通了,所以处理多对多表问题时,就要考虑建立关系表了
例:
学生表:
课程表:
关系表:**
注:所以对于多对多表,通过关系表就建立起了两张表的联系!多对多表时建立主外键后,要先删除约束表内容再删除主表内容