一些好玩的网站网站搭建平台都有哪些
目录:
1.什么是关联关系映射:
一对一和多对多的区别
2.mybaits中的一对一&一对多关联关系配置
配置generatoeConfig文件
插件自动生成
编辑
写sql语句
创建 Ordermapper类
编写接口类
编辑 编写接口实现类
编写测试类
测试结果
一对一
编辑 测试结果:
3.mybatis中的多对多的关联关系配置
创建 HBookVo
编写Sql
定义HBookMapper 接口
编写HBookBiz 接口
HBookBizImpl 接口实现类
编写测试类
测试结果
1.什么是关联关系映射:
MyBatis是一个Java持久化框架,它提供了一种将数据库表与Java对象之间的关联关系进行映射的方式。关联关系映射是指将数据库表中的列与Java对象中的属性进行对应,以实现数据的读取和写入。通过MyBatis的关联关系映射,可以方便地进行数据库操作,包括查询、插入、更新和删除等操作。
一对一和多对多的区别
一对一和多对多是数据库中常见的关联关系类型。
一对一关系是指两个实体之间存在唯一的对应关系。在数据库中,可以通过在两个表之间共享相同的主键或外键来建立一对一关系。例如,一个人只能有一个身份证号码,而一个身份证号码也只能对应一个人。
多对多关系是指两个实体之间存在多个对应关系。在数据库中,可以通过引入第三个关联表来实现多对多关系。例如,一个学生可以选择多门课程,而一门课程也可以被多个学生选择。
总结来说,一对一关系是一种唯一的对应关系,而多对多关系是一种多个对应关系。
2.mybaits中的一对一&一对多关联关系配置
配置generatoeConfig文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration><!-- 引入配置文件 --><properties resource="jdbc.properties"/><!--指定数据库jdbc驱动jar包的位置--><classPathEntry location="C:\\temp2\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/><!-- 一个数据库一个context --><context id="infoGuardian"><!-- 注释 --><commentGenerator><property name="suppressAllComments" value="true"/><!-- 是否取消注释 --><property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 --></commentGenerator><!-- jdbc连接 --><jdbcConnection driverClass="${jdbc.driver}"connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/><!-- 类型转换 --><javaTypeResolver><!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 01 指定javaBean生成的位置 --><!-- targetPackage:指定生成的model生成所在的包名 --><!-- targetProject:指定在该项目下所在的路径 --><javaModelGenerator targetPackage="com.zking.model"targetProject="src/main/java"><!-- 是否允许子包,即targetPackage.schemaName.tableName --><property name="enableSubPackages" value="false"/><!-- 是否对model添加构造函数 --><property name="constructorBased" value="true"/><!-- 是否针对string类型的字段在set的时候进行trim调用 --><property name="trimStrings" value="false"/><!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --><property name="immutable" value="false"/></javaModelGenerator><!-- 02 指定sql映射文件生成的位置 --><sqlMapGenerator targetPackage="com.zking.mapper"targetProject="src/main/java"><!-- 是否允许子包,即targetPackage.schemaName.tableName --><property name="enableSubPackages" value="false"/></sqlMapGenerator><!-- 03 生成XxxMapper接口 --><!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 --><!-- type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 --><!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --><javaClientGenerator targetPackage="com.zking.mapper"targetProject="src/main/java" type="XMLMAPPER"><!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --><property name="enableSubPackages" value="false"/></javaClientGenerator><!-- 配置表信息 --><!-- schema即为数据库名 --><!-- tableName为对应的数据库表 --><!-- domainObjectName是要生成的实体类 --><!-- enable*ByExample是否生成 example类 --><!--<table schema="" tableName="t_book" domainObjectName="Book"--><!--enableCountByExample="false" enableDeleteByExample="false"--><!--enableSelectByExample="false" enableUpdateByExample="false">--><!--<!– 忽略列,不生成bean 字段 –>--><!--<!– <ignoreColumn column="FRED" /> –>--><!--<!– 指定列的java数据类型 –>--><!--<!– <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> –>--><!--</table>--><table schema="" tableName="t_hibernate_book" domainObjectName="HBook"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></table><table schema="" tableName="t_hibernate_book_category" domainObjectName="HBookCategory"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></table><table schema="" tableName="t_hibernate_category" domainObjectName="HCategory"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></table><table schema="" tableName="t_hibernate_order" domainObjectName="Order"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></table><table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></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 List<BookCategory> bookc = new ArrayList<>();}
编写Sql
<resultMap id="HBookVoMap" type="com.ycxw.vo.HBookVo" ><result column="book_id" property="bookId"></result><result column="book_name" property="bookName"></result><result column="price" property="price"></result><collection property="bookc" ofType="com.ycxw.model.Category"><result column="category_id" property="categoryId"></result><result column="category_name" property="categoryName"></result></collection></resultMap><!--根据书籍的id查询书籍的信息及所属属性--><select id="selectByBookId" resultMap="HBookVoMap" parameterType="java.lang.Integer">SELECT*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());}
}