MyBatis是一个Java持久化框架,它提供了一种将数据库表与Java对象之间的关联关系进行映射的方式。关联关系映射是指将数据库表中的列与Java对象中的属性进行对应,以实现数据的读取和写入。通过MyBatis的关联关系映射,可以方便地进行数据库操作,包括查询、插入、更新和删除等操作。
一对一和多对多是数据库中常见的关联关系类型。
一对一关系是指两个实体之间存在唯一的对应关系。在数据库中,可以通过在两个表之间共享相同的主键或外键来建立一对一关系。例如,一个人只能有一个身份证号码,而一个身份证号码也只能对应一个人。
多对多关系是指两个实体之间存在多个对应关系。在数据库中,可以通过引入第三个关联表来实现多对多关系。例如,一个学生可以选择多门课程,而一门课程也可以被多个学生选择。
总结来说,一对一关系是一种唯一的对应关系,而多对多关系是一种多个对应关系。
- "1.0" encoding="UTF-8" ?>
- "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
-
-
"jdbc.properties"/> -
-
-
"C:\\temp2\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/> -
-
-
"infoGuardian"> -
-
-
"suppressAllComments" value="true"/> -
"suppressDate" value="true"/> -
-
-
-
"${jdbc.driver}" - connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
-
-
-
-
-
"forceBigDecimals" value="false"/> -
-
-
-
-
-
"com.zking.model" - targetProject="src/main/java">
-
-
"enableSubPackages" value="false"/> -
-
"constructorBased" value="true"/> -
-
"trimStrings" value="false"/> -
-
"immutable" value="false"/> -
-
-
-
"com.zking.mapper" - targetProject="src/main/java">
-
-
"enableSubPackages" value="false"/> -
-
-
-
-
-
-
"com.zking.mapper" - targetProject="src/main/java" type="XMLMAPPER">
-
-
"enableSubPackages" value="false"/> -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
"" tableName="t_hibernate_book" domainObjectName="HBook"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
"" tableName="t_hibernate_book_category" domainObjectName="HBookCategory"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
"" tableName="t_hibernate_category" domainObjectName="HCategory"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
"" tableName="t_hibernate_order" domainObjectName="Order"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
"" tableName="t_hibernate_order_item" domainObjectName="OrderItem"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
- 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;
- @Test
- public void selectbyoid() {
- //更便于维护
- OrderVo orderVo = orderBiz.selectbyoid(8);
- System.out.println(orderVo);
- orderVo.getOrderItems().forEach(System.out::println);
- }
- @Test
- public void selectByOrderItemId() {
- OrderItemVo orderItemVo= orderItemBiz.selectByOrderItemId(27);
- System.out.println(orderItemVo);
- // System.out.println(orderItemVo.getOrder());
- }
- }
-
- 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 List
bookc = new ArrayList<>(); -
- }
-
"HBookVoMap" type="com.ycxw.vo.HBookVo" > -
"book_id" property="bookId"> -
"book_name" property="bookName"> -
"price" property="price"> -
"bookc" ofType="com.ycxw.model.Category"> -
"category_id" property="categoryId"> -
"category_name" property="categoryName"> -
-
-
-
-
- SELECT
- *
- FROM
- t_hibernate_book b,
- t_hibernate_category c,
- t_hibernate_book_category bc
- WHERE
- b.book_id = bc.bid
- AND c.category_id = bc.bcid
- AND b.book_id = #{bid}
-
HBookVo selectByBookId(@Param("bid") Integer bid);
- 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);
- }
- 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 {
- @Autowired
- private HBookMapper hBookMapper;
- @Override
- public 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 {
- @Autowired
- private HBookBiz hBookBiz;
-
-
- @Test
- public void selectByBookId(){
- HBookVo hBookVo = hBookBiz.selectByBookId(66);
- System.out.println(hBookVo);
- System.out.println(hBookVo.getBookc());
- }
- }