MyBatis——商品的类别

MyBatis——商品的类别

    • Resource
    • pojo层
    • utils层
    • 测试层

实验要求
本实验要求根据商品表在数据库中创建一个product表,根据商品类别表在数据库中创建一个category表,并通过MyBatis查询商品类别为白色家电的商品的所有信息。

实验内容

表1 商品表(product)
商品编号(id)商品名称(goodsname)商品单价(price)商品类别(typeid)
1电视机4999.991
2冰箱3888.882
3空调2777.772
4洗衣机1666.662
表2 商品类别表(category)
商品类别编号(id)商品类别名称(typename)
1黑色家电
2白色家电

该案例需要实现以下功能:

  • 通过MyBatis查询商品类别为白色家电的商品的所有信息

实验分析
本实验主要考查对MyBatis的关联映射的掌握。

  • 建立好实验所需的数据库及表(其中需要用到外键)。
  • 需要在项目的src/main/resource目录下创建数据库连接的配置文件和MyBatis的核心配置文件和mapper文件夹。
  • 再在src/main/java下创建一个实体类Category,编写商品类别的基本信息;实体类Goods,编写商品的基本信息。
  • 再创建工具类MyBatisUtils。
  • 最后在src/test/java下创建一个测试类Test1完成实验内容。

商品表product和商品类别表category及外键的设置
在这里插入图片描述

代码实现

Resource

db.properties(数据库连接配置文件)

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&\ characterEncoding=utf8&useUnicode=true&useSSL=false
username=root
password=1

mybatis-config.xml(MyBatis的核心配置文件)


DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="db.properties"> properties><settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/><setting name="cacheEnabled" value="true"/>settings><typeAliases><package name="com.cqust.pojo"/>typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/>dataSource>environment>environments><mappers><mapper resource="mapper/CategoryMapper.xml"/>mappers>
configuration>

CategoryMapper.xml(映射文件)


DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cqust.pojo.Category"><select id="findGoodsByTypename" resultMap="findGoodsByTypenameResult">select c.*,p.id as product_id,p.goodsname,p.price from product p,category cwhere c.id=p.typeidand c.typename=#{typename}select><resultMap id="findGoodsByTypenameResult" type="category"><id property="id" column="id"/><result property="typename" column="typename"/><collection property="goodsList" ofType="Goods"><id property="id" column="product_id"/><result property="goodsname" column="goodsname"/><result property="price" column="price"/>collection>resultMap>
mapper>

pojo层

Category类

package com.cqust.pojo;import java.util.List;public class Category {private int id;					//商品类别编号private String typename;		//商品类别名称private List<Goods> goodsList;	//商品列表public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTypename() {return typename;}public void setTypename(String typename) {this.typename = typename;}public List<Goods> getGoodsList() {return goodsList;}public void setGoodsList(List<Goods> goodsList) {this.goodsList = goodsList;}@Overridepublic String toString() {return "Category{" +"id=" + id +", typename='" + typename + '\'' +", goodsList=" + goodsList +'}';}
}

Goods类

package com.cqust.pojo;import java.util.List;public class Goods {private int id;				//商品编号private String goodsname;	//商品名称private double price;		//商品单价public int getId() {return id;}public void setId(int id) {this.id = id;}public String getGoodsname() {return goodsname;}public void setGoodsname(String goodsname) {this.goodsname = goodsname;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Goods{" +"id=" + id +", goodsname='" + goodsname + '\'' +", price=" + price +'}';}
}

utils层

MyBatisUtils类

package com.cqust.utils;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 java.io.Reader;/*** 工具类*/
public class MyBatisUtils {private static SqlSessionFactory sqlSessionFactory = null;//初始化SQLSessionFactory对象static {try{//使用MyBatis提供的Resource类加载MyBatis的配置文件Reader reader = Resources.getResourceAsReader("mybatis-config.xml");//构建SQLSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (Exception e){e.printStackTrace();}}//获取SqlSession对象的方法public static SqlSession getSession(){//若传入true表示关闭事务控制,自动提交;false表示开启事务控制return sqlSessionFactory.openSession(true);}
}

测试层

Test1(测试类)

import com.cqust.pojo.Student;
import com.cqust.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.ArrayList;
import java.util.List;public class Test1 {/*** 一对多*/@Testpublic void findGoodsByTypenameTest(){//通过工具类生成SqlSession对象SqlSession session = MyBatisUtils.getSession();//使用session查询typename为白色家电的的商品Category category = session.selectOne("findGoodsByTypename","白色家电");//在控制台输出查询结果System.out.println(category);//关闭SqlSessionsession.close();}
}

实验运行结果:
上面的内容为日志输出

实验小结
  本实验主要考察了首先对开发中涉及的数据表之间,以及对象之间的关联关系,并由此引出了MyBatis框架中对关联关系的处理;然后通过此案例对MyBatis框架处理实体对象之间的3种关联关系进行了详细讲解;MyBatis还讲解了MyBatis 的缓存机制,包括一级缓存和二级缓存。通过学习MyBatis关联映射和缓存机制的内容,读者可以了解数据表之间及对象之间的3种关联关系,熟悉MyBatis 的缓存机制,并能够在MyBatis框架中熟练运用3种关联关系进行查询,熟练配置MyBatis缓存,从而提高项目的开发效率。

谢谢浏览!


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部