做网站都有那些步骤,网站群建设情况,WordPress音乐网页,徐州卫生人才网目录#xff1a;
1.什么是关联关系映射#xff1a;
一对一和多对多的区别
2.mybaits中的一对一一对多关联关系配置
配置generatoeConfig文件
插件自动生成
编辑 写sql语句
创建 Ordermapper类
编写接口类
编辑 编写接口实现类
编写测试类 测试结果
一对…
目录
1.什么是关联关系映射
一对一和多对多的区别
2.mybaits中的一对一一对多关联关系配置
配置generatoeConfig文件
插件自动生成
编辑 写sql语句
创建 Ordermapper类
编写接口类
编辑 编写接口实现类
编写测试类 测试结果
一对一
编辑 测试结果
3.mybatis中的多对多的关联关系配置
创建 HBookVo
编写Sql
定义HBookMapper 接口
编写HBookBiz 接口
HBookBizImpl 接口实现类
编写测试类
测试结果 1.什么是关联关系映射 MyBatis是一个Java持久化框架它提供了一种将数据库表与Java对象之间的关联关系进行映射的方式。关联关系映射是指将数据库表中的列与Java对象中的属性进行对应以实现数据的读取和写入。通过MyBatis的关联关系映射可以方便地进行数据库操作包括查询、插入、更新和删除等操作。 一对一和多对多的区别 一对一和多对多是数据库中常见的关联关系类型。 一对一关系是指两个实体之间存在唯一的对应关系。在数据库中可以通过在两个表之间共享相同的主键或外键来建立一对一关系。例如一个人只能有一个身份证号码而一个身份证号码也只能对应一个人。 多对多关系是指两个实体之间存在多个对应关系。在数据库中可以通过引入第三个关联表来实现多对多关系。例如一个学生可以选择多门课程而一门课程也可以被多个学生选择。 总结来说一对一关系是一种唯一的对应关系而多对多关系是一种多个对应关系。 2.mybaits中的一对一一对多关联关系配置
配置generatoeConfig文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE generatorConfiguration PUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtd
generatorConfiguration!-- 引入配置文件 --properties resourcejdbc.properties/!--指定数据库jdbc驱动jar包的位置--classPathEntry locationC:\\temp2\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar/!-- 一个数据库一个context --context idinfoGuardian!-- 注释 --commentGeneratorproperty namesuppressAllComments valuetrue/!-- 是否取消注释 --property namesuppressDate valuetrue/ !-- 是否生成注释代时间戳 --/commentGenerator!-- jdbc连接 --jdbcConnection driverClass${jdbc.driver}connectionURL${jdbc.url} userId${jdbc.username} password${jdbc.password}/!-- 类型转换 --javaTypeResolver!-- 是否使用bigDecimal false可自动转化以下类型Long, Integer, Short, etc. --property nameforceBigDecimals valuefalse//javaTypeResolver!-- 01 指定javaBean生成的位置 --!-- targetPackage指定生成的model生成所在的包名 --!-- targetProject指定在该项目下所在的路径 --javaModelGenerator targetPackagecom.zking.modeltargetProjectsrc/main/java!-- 是否允许子包即targetPackage.schemaName.tableName --property nameenableSubPackages valuefalse/!-- 是否对model添加构造函数 --property nameconstructorBased valuetrue/!-- 是否针对string类型的字段在set的时候进行trim调用 --property nametrimStrings valuefalse/!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法只有构造方法 --property nameimmutable valuefalse//javaModelGenerator!-- 02 指定sql映射文件生成的位置 --sqlMapGenerator targetPackagecom.zking.mappertargetProjectsrc/main/java!-- 是否允许子包即targetPackage.schemaName.tableName --property nameenableSubPackages valuefalse//sqlMapGenerator!-- 03 生成XxxMapper接口 --!-- typeANNOTATEDMAPPER,生成Java Model 和基于注解的Mapper对象 --!-- typeMIXEDMAPPER,生成基于注解的Java Model 和相应的Mapper对象 --!-- typeXMLMAPPER,生成SQLMap XML文件和独立的Mapper接口 --javaClientGenerator targetPackagecom.zking.mappertargetProjectsrc/main/java typeXMLMAPPER!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model true:com.oop.eksp.user.model.[schemaName] --property nameenableSubPackages valuefalse//javaClientGenerator!-- 配置表信息 --!-- schema即为数据库名 --!-- tableName为对应的数据库表 --!-- domainObjectName是要生成的实体类 --!-- enable*ByExample是否生成 example类 --!--table schema tableNamet_book domainObjectNameBook--!--enableCountByExamplefalse enableDeleteByExamplefalse--!--enableSelectByExamplefalse enableUpdateByExamplefalse--!--lt;!ndash; 忽略列不生成bean 字段 ndash;gt;--!--lt;!ndash; ignoreColumn columnFRED / ndash;gt;--!--lt;!ndash; 指定列的java数据类型 ndash;gt;--!--lt;!ndash; columnOverride columnLONG_VARCHAR_FIELD jdbcTypeVARCHAR / ndash;gt;--!--/table--table schema tableNamet_hibernate_book domainObjectNameHBookenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_book_category domainObjectNameHBookCategoryenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_category domainObjectNameHCategoryenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_order domainObjectNameOrderenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_order_item domainObjectNameOrderItemenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/table/context
/generatorConfiguration插件自动生成 写sql语句 创建 Ordermapper类
package com.zking.mapper;import com.zking.model.Order;
import com.zking.vo.OrderVo;
import org.apache.ibatis.annotations.Param;public interface OrderMapper {int deleteByPrimaryKey(Integer orderId);int insert(Order record);int insertSelective(Order record);Order selectByPrimaryKey(Integer orderId);int updateByPrimaryKeySelective(Order record);int updateByPrimaryKey(Order record);OrderVo selectbyoid(Param(oid) Integer oid);
}
编写接口类 编写接口实现类 编写测试类
package com.zking.biz.impl;import com.zking.biz.OrderBiz;
import com.zking.biz.OrderItemBiz;
import com.zking.vo.OrderItemVo;
import com.zking.vo.OrderVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;/*** author bing人* site* company xy集团* create 2023-09-04 9:46*/
//自动加载上下文
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations{classpath:spring-context.xml})
public class OrderBizImplTest {
Autowired
private OrderBiz orderBiz;
Autowired
private OrderItemBiz orderItemBiz;Testpublic void selectbyoid() {//更便于维护OrderVo orderVo orderBiz.selectbyoid(8);System.out.println(orderVo);orderVo.getOrderItems().forEach(System.out::println);}Testpublic void selectByOrderItemId() {OrderItemVo orderItemVo orderItemBiz.selectByOrderItemId(27);System.out.println(orderItemVo);
// System.out.println(orderItemVo.getOrder());}
} 测试结果 一对一 测试结果 3.mybatis中的多对多的关联关系配置
创建 HBookVo package com.zking.vo;import com.zking.model.BookCategory;
import com.zking.model.HBook;
import lombok.Data;import java.util.List;/*** author xy集团* site blog.csdn.net/Justw320* create 2023-0904 11:03*/
Data
public class HBookVo extends HBook {private ListBookCategory bookc new ArrayList();}
编写Sql resultMap idHBookVoMap typecom.ycxw.vo.HBookVo result columnbook_id propertybookId/resultresult columnbook_name propertybookName/resultresult columnprice propertyprice/resultcollection propertybookc ofTypecom.ycxw.model.Categoryresult columncategory_id propertycategoryId/resultresult columncategory_name propertycategoryName/result/collection/resultMap!--根据书籍的id查询书籍的信息及所属属性--select idselectByBookId resultMapHBookVoMap parameterTypejava.lang.IntegerSELECT*FROMt_hibernate_book b,t_hibernate_category c,t_hibernate_book_category bcWHEREb.book_id bc.bidAND c.category_id bc.bcidAND b.book_id #{bid}/select
定义HBookMapper 接口
HBookVo selectByBookId(Param(bid) Integer bid); 编写HBookBiz 接口
package com.zking.biz;import com.zking.vo.HBookVo;/*** author xy集团* site blog.csdn.net/Justw320* create 2023-09-04 11:12*/
public interface HBookBiz {HBookVo selectByBookId(Integer bid);
}
HBookBizImpl 接口实现类
package com.ycxw.biz.impl;import com.ycxw.biz.HBookBiz;
import com.ycxw.mapper.HBookMapper;
import com.ycxw.vo.HBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author xy集团* site blog.csdn.net/Justw320* create 2023-0904 11:18*/
Service
public class HBookBizImpl implements HBookBiz {Autowiredprivate HBookMapper hBookMapper;Overridepublic HBookVo selectByBookId(Integer bid) {return hBookMapper.selectByBookId(bid);}
}
编写测试类
package com.zking.biz.impl;import com.zking.biz.HBookBiz;
import com.zking.vo.HBookVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author xy集团* site blog.csdn.net/Justw320* create 2023-09-04 11:22*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations {classpath:spring-mybatis.xml})
public class OrderBizImplTest {Autowiredprivate HBookBiz hBookBiz;Testpublic void selectByBookId(){HBookVo hBookVo hBookBiz.selectByBookId(66);System.out.println(hBookVo);System.out.println(hBookVo.getBookc());}
}
测试结果