概述
不要直接复制黏贴,其中大部分代码都带有个人信息,提供一个完整的思路结合课本可以很容易的完成实验
大作业要求
(1)创建一张订单t_orders表,一张商品表t_goods表二者N:N的关联关系。
(2)定义t_orders、t_goods所对应的POJO类
(3)定义或修改相应的映射文件,使用MyBatis嵌套结果的方式定义一个根据订单Id查询当前订单所对应的商品信息,并写出测试类进行测试。
(4)定义或修改相应的映射文件,使用MyBatis嵌套查询的方式定义一个根据订单Id查询当前订单所对应的商品信息,并写出测试类进行测试。
(5)定义或修改相应的映射文件,增加一条订单信息,该订单中有三个已有的商品,注意订单总价格是程序计算出来的,并写出测试类进行测试。
(6)定义或修改相应的映射文件,修改(5)的订单信息,将订单中有三个的商品改成两个商品,并写出测试类进行测试。
(7)定义或修改相应的映射文件,删除一条订单信息,注意该订单中的商品不会级联删除,并写出测试类进行测试。
(1) 创建一张订单t_orders表,一张商品表t_goods表二者N:N的关联关系。
-- ----------------------------
-- Table structure for `t_goods`
-- ----------------------------
DROP TABLE IF EXISTS `t_goods`;
CREATE TABLE `t_goods` (
`id` int(11) NOT NULL auto_increment,
`goodname` varchar(20) NOT NULL default '',
`price` float NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_goods
-- ----------------------------
INSERT INTO `t_goods` VALUES ('1', 'nike shoes12-3', '560');
INSERT INTO `t_goods` VALUES ('2', '李宁15-1', '146');
INSERT INTO `t_goods` VALUES ('3', '外套', '780');
INSERT INTO `t_goods` VALUES ('4', '裤子', '360');
INSERT INTO `t_goods` VALUES ('5', '帽子', '160');
INSERT INTO `t_goods` VALUES ('6', '手表', '30000');
-- ----------------------------
-- Table structure for `t_goods_orders`
-- ----------------------------
DROP TABLE IF EXISTS `t_goods_orders`;
CREATE TABLE `t_goods_orders` (
`id` int(11) NOT NULL auto_increment,
`good_id` int(11) NOT NULL default '0',
`order_id` int(11) NOT NULL default '0',
`number` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `fr1` (`good_id`),
KEY `fr2` (`order_id`),
CONSTRAINT `fr1` FOREIGN KEY (`good_id`) REFERENCES `t_goods` (`id`),
CONSTRAINT `fr2` FOREIGN KEY (`order_id`) REFERENCES `t_orders` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_goods_orders
-- ----------------------------
INSERT INTO `t_goods_orders` VALUES ('1', '1', '1', '2');
INSERT INTO `t_goods_orders` VALUES ('2', '2', '1', '1');
INSERT INTO `t_goods_orders` VALUES ('3', '3', '1', '1');
INSERT INTO `t_goods_orders` VALUES ('4', '3', '2', '1');
INSERT INTO `t_goods_orders` VALUES ('5', '5', '2', '3');
-- ----------------------------
-- Table structure for `t_orders`
-- ----------------------------
DROP TABLE IF EXISTS `t_orders`;
CREATE TABLE `t_orders` (
`id` int(11) NOT NULL auto_increment,
`customer_id` int(11) NOT NULL default '0',
`totleprice` float default NULL,
PRIMARY KEY (`id`),
KEY `ff` (`customer_id`),
CONSTRAINT `ff` FOREIGN KEY (`customer_id`) REFERENCES `t_customer` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_orders
-- ----------------------------
INSERT INTO `t_orders` VALUES ('1', '1', '1000');
INSERT INTO `t_orders` VALUES ('2', '1', '20000');
在sql控制台直接执行命令时,中间表没有建立后来又重新建立的
执行结果:
(2) 定义t_orders、t_goods所对应的POJO类
修改了建立po类,orders的
package com.xiucai.po;
import java.util.ArrayList;
public class Orders {
private int id;
private String customer_id;
private Person totleprice;
@Override
public String toString() {
return "Orders [id=" + id + ", customer_id=" + customer_id + ", totleprice=" + totleprice + ", productsList="
+ productsList + "]";
}
//如果只需要在oders找person只写着一个
private ArrayList<Product> productsList;
public int getId() {
return id;
}
public String getCustomer_id() {
return customer_id;
}
public Person getTotleprice() {
return totleprice;
}
public ArrayList<Product> getProductsList() {
return productsList;
}
public void setId(int id) {
this.id = id;
}
public void setCustomer_id(String customer_id) {
this.customer_id = customer_id;
}
public void setTotleprice(Person totleprice) {
this.totleprice = totleprice;
}
public void setProductsList(ArrayList<Product> productsList) {
this.productsList = productsList;
}
}
编写good货物对应的Product类
package com.xiucai.po;
import java.util.ArrayList;
import com.sun.org.apache.xpath.internal.operations.Or;
public class Product {
private int id;
private String goodname;
private double price;
@Override
public String toString() {
return "Product [id=" + id + ", goodname=" + goodname + ", price=" + price + ", ordersList=" + ordersList + "]";
}
private ArrayList<Orders> ordersList;
public int getId() {
return id;
}
public String getGoodname() {
return goodname;
}
public double getPrice() {
return price;
}
public ArrayList<Orders> getOrdersList() {
return ordersList;
}
public void setId(int id) {
this.id = id;
}
public void setGoodname(String goodname) {
this.goodname = goodname;
}
public void setPrice(double price) {
this.price = price;
}
public void setOrdersList(ArrayList<Orders> ordersList) {
this.ordersList = ordersList;
}
}
(3) 定义或修改相应的映射文件,使用MyBatis嵌套结果的方式定义一个根据订单Id查询当前订单所对应的商品信息,并写出测试类进行测试。
嵌套结果语句
测试可执行
查找的关键性代码xml
<?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.xiucai.mapper.OrdersMapper">
<select id="findOrdersWithPorduct" parameterType="Integer" resultMap="OrdersWithProductResult">
select toder.*,tgood.id as tgoodid,tgood.goodname,tgood.price
from t_orders toder ,t_goods_orders tgo,t_goods tgood
where toder.id=tgo.orders_id
and tgood.id=tgo.good_id
and toder.id=#{id}
</select>
<resultMap type="Orders" id="OrdersWithProductResult">
<id property="id" column="id"/>
<result property="customer_id" column="customer_id" />
<result property="totleprice" column="totleprice" />
<collection property="productsList" column="id" ofType="Product" >
<id property="id" column="tgoodid"/>
<result property="goodname" column="goodname" />
<result property="price" column="price" />
</collection>
</resultMap>
</mapper>
测试类:
package com.xiucai.Test;
import java.io.IOException;
import javax.websocket.Session;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.xiucai.Utils.MybatisUtils;
import com.xiucai.po.Orders;
public class CollectionTest {
@Test
public void showGoosByOrderIdTest() {
try {
SqlSession session=MybatisUtils.getSession();
Orders orders=session.selectOne("com.xiucai.mapper.OrdersMapper.findOrdersWithPorduct", 1);
System.out.println(orders);
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试结果:
(4) 定义或修改相应的映射文件,使用MyBatis嵌套查询的方式定义一个根据订单Id查询当前订单所对应的商品信息,并写出测试类进行测试。
嵌套查询测试在商品xml中的sql代码
嵌套查询代码 xml
<!-- 嵌套查询 -->
<select id="findOderWithProductByNesting" parameterType="Integer" resultMap="OrdersWithProductResult1">
select * from t_orders where id=#{id}
</select>
<!-- 嵌套查询的结果 -->
<resultMap type="Orders" id="OrdersWithProductResult1">
<id property="id" column="id"/>
<result property="customer_id" column="customer_id" />
<result property="totleprice" column="totleprice" />
<collection property="productsList" column="id" ofType="Product"
select="com.xiucai.mapper.ProductMapper.findProductById">
</collection>
</resultMap>
productMapper
<?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.xiucai.mapper.ProductMapper">
<select id="findProductById" parameterType="Integer" resultType="Product">
select * from t_goods where id in(
select good_id from t_goods_orders where orders_id=#{id});
</select>
</mapper>
结果
(5) 定义或修改相应的映射文件,增加一条订单信息,该订单中有三个已有的商品,注意订单总价格是程序计算出来的,并写出测试类进行测试。
数据库设置
先设置一个更新的mapper里边的方法
<update id="updateTatlePrice" parameterType="com.xiucai.po.Orders">
update t_orders
<set>
totleprice=#{totleprice}
</set>
where id=#{id}
</update>
public void updateTatlePrice() {
try {
double totleprice=0;
SqlSession session=MybatisUtils.getSession();
//把查询到的结果返回到orders
Orders orders=session.selectOne("com.xiucai.mapper.OrdersMapper.findOrdersWithPorduct", 3);
//通过遍历结果把订单总价放在了,tatleprice中
for (Product product : orders.getProductsList()) {
totleprice=product.getPrice()+totleprice;
}
//把tatleprice,赋值给订单对象
Orders order1=new Orders();
order1.setTotleprice(totleprice);
order1.setId(3);
System.out.println(order1);
int i=session.update("com.xiucai.mapper.OrdersMapper.updateTatlePrice", order1);
//提交事务
session.commit();
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
实验结果
(6) 定义或修改相应的映射文件,修改(5)的订单信息,将订单中有三个的商品改成两个商品,并写出测试类进行测试。
编写删除方法的xml
<!-- 删除记录的,实际上把中间表的信息删除一条就可以了,目的:删除订单中的货物-->
<delete id="deleteOrdersInProduct" parameterType="Integer" >
delete from t_goods_orders where id=#{id}
</delete>
测试类
@Test
public void deleteOrdersinProduct() {
try {
SqlSession session=MybatisUtils.getSession();
session.delete("com.xiucai.mapper.OrdersMapper.deleteOrdersInProduct",7);
//调用上边函数,使得新的价格更新
updateTatlePrice();
session.commit();
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
(7) 定义或修改相应的映射文件,删除一条订单信息,注意该订单中的商品不会级联删除,并写出测试类进行测试。
在数据库中测试,删除orders中的记录因为限制条件失败了
究其原因
我们在删除时有restrict,这里作用是,如果父类有值,则不允许进行下一步动作。想要达到删除中间表记录的功能设置成,这个经过测试.
本来表
编写代码
<delete id="deleteOrders" parameterType="Integer" >
delete from t_orders where id=#{id}
</delete>
测试类
@Test
public void deleteOrders() {
try {
SqlSession session=MybatisUtils.getSession();
session.delete("com.xiucai.mapper.OrdersMapper.deleteOrders",1);
session.commit();
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
执行后
最后
以上就是不安鸡翅为你收集整理的<<mybatis>>大作业的全部内容,希望文章能够帮你解决<<mybatis>>大作业所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复