以下为《关联映射实验》的无排版文字预览,完整格式请下载
下载前请仔细阅读文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。
一、《MyBatis的关联映射实验》
姓名:
学号:
一、实验目的和要求
1.掌握对象之间的三种关联关系
2.掌握关联关系中的嵌套查询和嵌套结果
3.掌握关联映射
二、实验内容
题目1:查询商品的类别
请按照以下要求创建Servlet01类。
要求如下:
现有一个商品表product和一个商品类别表category,其中,商品类别表category和商品表product是一对多的关系。
商品表product的详情如下表所示。
商品编号(id)
商品名称(goodsname)
商品详情(price)
商品类别(typeid)
1
电视机
5000
1
2
冰箱
4000
2
3
空调
3000
2
4
洗衣机
2000
2
商品类别表category详情如下表所示。
商品类别编号(id)
商品类别名称(typename)
1
黑色家电
2
白色家电
根据上述内容在数据库分别创建一个商品表product和一个商品类别表category, 并通过MyBatis查询商品类别为白色家电的商品的所有信息。
实现步骤如下。
项目搭建
创建一个名称为mybatis-demo04的项目,项目的具体搭建过程请参考1.3节。
数据库准备
在名为mybatis的数据库中,创建两个数据表,分别为product和category,同时在表中预先插入几条测试数据。
POJO类准备
在com.itheima.pojo包某某,创建持久化类Category,并在类中定义
商品类别的相关属性和方法。
在com.itheima.pojo包某某,创建持久化类Product,并在类中定义相
关属性和方法。
编写映射文件
在com.itheima.mapper包某某,创建商品类别实体映射文件CategoryMapper.xml,并在文件中编写一对多关联映射查询的配置。
修改mybatis-config.xml核心配置文件
在核心配置文件mybatis-config.xml中,引入CategoryMapper.xml,将CategoryMapper.xml映射文件加载到程序中。
编写测试方法
在测试类MyBatisTest中,编写测试方法findCategoryTest()。
三、实现步骤与代码
项目搭建
创建一个名称为mybatis-demo04的项目,项目的具体搭建过程请参考1.3节。
/
数据库准备
在名为mybatis的数据库中,创建两个数据表,分别为product和category,同时在表中预先插入几条测试数据。
/
POJO类准备
在com.itheima.pojo包某某,创建持久化类Category,并在类中定义 商品类别的相关属性和方法。package com.itheima.pojo;import java.util.List;public class Category { private Integer id; private String typename; private List productList; public Category() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getTypename() { return this.typename; } public void setTypename(String typename) { this.typename = typename; } public List getProductList() { return this.productList; } public void setProductList(List productList) { this.productList = productList; } public String toString() { return "Category{id=" + this.id + ", typename='" + this.typename + ", productList=" + this.productList + '}'; }}
在com.itheima.pojo包某某,创建持久化类Product,并在类中定义相关属性和方法。package com.itheima.pojo;public class Product { private Integer id; private String goodsname; private double price; public Product() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getGoodsname() { return this.goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public double getPrice() { return this.price; } public void setPrice(double price) { this.price = price; } public String toString() { return "Product{id=" + this.id + ", goodsname='" + this.goodsname + ", price=" + this.price + '}'; }}
编写映射文件
在com.itheima.mapper包某某,创建商品类别实体映射文件CategoryMapper.xml,并在文件中编写一对多关联映射查询的配置。
修改mybatis-config.xml核心配置文件
在核心配置文件mybatis-config.xml中,引入CategoryMapper.xml,将CategoryMapper.xml映射文件加载到程序中。
/
/
/
/
编写测试方法
在测试类MyBatisTest中,编写测试方法findCategoryTest()。
测试类如下:
package Test;import com.itheima.pojo.*;import com.itheima.utils.MyBatisUtils;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.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.io.Reader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MyBatisTest { private SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; @Before public void init() { //定义读取文件名 String resources = "mybatis-config.xml"; //创建流 Reader reader = null; try { //读取mybatis-config.xml文件到reader对象中 reader = Resources.getResourceAsReader(resources); //初始化mybatis,创建SqlSessionFactory类的对象 SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); //创建session对象 sqlSession = sqlMapper.openSession(); } catch (IOException e) { e.printStackTrace(); } } @Test public void findAllStudentTest() { // SqlSession执行映射文件中定义的SQL,并返回映射结果 List list = sqlSession.selectList("com.itheima.mapper.StudentMapper." + "findAllStudent"); for (Student student : list) { System.out.println(student); } } /** * 根据客户姓名和职业组合条件查询客户信息列表 */ @Test public void findCustomerByNameAndJobsTest() { // 通过工具类获取SqlSession对象 SqlSession session = MyBatisUtils.getSession(); // 创建Customer对象,封装需要组合查询的条件 Customer customer = new Customer(); customer.setUsername("jack"); customer.setJobs("teacher"); // 执行SqlSession的查询方法,返回结果集 List customers = session.selectList("com.itheima.mapper" + ".CustomerMapper.findCustomerByNameAndJobs", customer); // 输出查询结果信息 for (Customer customer2 : customers) { // 打印输出结果 System.out.println(customer2); } // 关闭SqlSession session.close(); } /** * 根据客户姓名或职业查询客户信息列表 */ @Test public void findCustomerByNameOrJobsTest() { // 通过工具类获取SqlSession对象 SqlSession session = MyBatisUtils.getSession(); // 创建Customer对象,封装需要组合查询的条件 Customer customer = new Customer();// customer.setUsername("tom");// customer.setJobs("teacher"); // 执行SqlSession的查询方法,返回结果集 List customers = session.selectList("com.itheima.mapper" + ".CustomerMapper.findCustomerByNameOrJobs", customer); // 输出查询结果信息 for (Customer customer2 : customers) { // 打印输出结果 System.out.println(customer2); } // 关闭SqlSession session.close(); } /** * 更新客户信息 */ @Test public void updateCustomerBySetTest() { // 获取SqlSession SqlSession sqlSession = MyBatisUtils.getSession(); // 创建Customer对象,并向对象中添加数据 Customer customer = new Customer(); customer.setId(3); customer.setPhone("***"); // 执行SqlSession的更新方法,返回的是SQL语句影响的行数 int rows = sqlSession.update("com.itheima.mapper" + ".CustomerMapper.updateCustomerBySet", customer); // 通过返回结果判断更新操作是否执行成功 if (rows > 0) { System.out.println("您成功修改了" + rows + "条数据!"); } else { System.out.println("执行修改操作失败!!!"); } // 提交事务 sqlSession.commit(); // 关闭SqlSession sqlSession.close(); } /** * 更新客户信息 */ @Test public void updateCustomerByTrimTest() { // 获取SqlSession SqlSession sqlSession = MyBatisUtils.getSession(); // 创建Customer对象,并向对象中添加数据 Customer customer = new Customer(); customer.setId(3); customer.setPhone("***"); // 执行SqlSession的更新方法,返回的是SQL语句影响的行数 int rows = sqlSession.update("com.itheima.mapper" + ".CustomerMapper.updateCustomerByTrim", customer); // 通过返回结果判断更新操作是否执行成功 if (rows > 0) { System.out.println("您成功修改了" + rows + "条数据!"); } else { System.out.println("执行修改操作失败!!!"); } // 提交事务 sqlSession.commit(); // 关闭SqlSession sqlSession.close(); } /** * 根据客户id批量查询客户信息 */ @Test public void findByArrayTest() { // 获取SqlSession SqlSession session = MyBatisUtils.getSession(); // 创建数组,封装查询id Integer[] roleIds = {2, 3}; // 执行SqlSession的查询方法,返回结果集 List customers = session.selectList("com.itheima.mapper" + ".CustomerMapper.findByArray", roleIds); // 输出查询结果信息 for 内容过长,仅展示头部和尾部部分文字预览,全文请查看图片预览。 ion2.commit(); session2.close(); // 6.使用session3查询id为1的图书的信息 Book book3 = session3.selectOne("com.itheima.mapper." + "BookMapper.findBookById", 1); // 7.输出查询结果信息 System.out.println(book3.toString()); // 8.关闭SqlSession session3.close(); } @After public void destory() //提交事务 sqlSession.commit(); //关闭事务 sqlSession.close(); }}
四、实验结果与分析
没有遇到无法解决的问题。
[文章尾部最后500字内容到此结束,中间部分内容请查看底下的图片预览]
以上为《关联映射实验》的无排版文字预览,完整格式请下载
下载前请仔细阅读上面文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。