目录
一,一对多关联关系
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
通过以上表得以下代码
一对多的单方
<?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="D:\\maven mulu\\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.ruojuan.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.ruojuan.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.ruojuan.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">
<!-- 忽略列,不生成bean 字段 -->
<!-- <ignoreColumn column="FRED" /> -->
<!-- 指定列的java数据类型 -->
<!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
</table>
<table schema="" tableName="t_hibernate_category" domainObjectName="Category"
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>
</context>
</generatorConfiguration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ruojuan.mapper.OrderItemMapper" >
<resultMap id="BaseResultMap" type="com.ruojuan.model.OrderItem" >
<constructor >
<idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
</constructor>
</resultMap>
<resultMap id="OrderVoMap" type="com.zlp.model.vo.OrderItemVo" >
<result column="order_item_id" property="orderItemId"></result>
<result column="oid" property="oid"></result>
<result column="product_id" property="productId"></result>
<result column="quantity" property="quantity"></result>
<collection property="order" javaType="com.ruojuan.model.Order">
<result column="order_id" property="orderId"></result>
<result column="order_no" property="orderNo"></result>
</collection>
</resultMap>
<sql id="Base_Column_List" >
order_item_id, product_id, quantity, oid
</sql>
<select id="selectByPrimaryKeys" resultMap="OrderVoMap" parameterType="java.lang.Integer" >
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid and oi.order_item_id=#{orderItemId}
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.zlp.model.OrderItem" >
insert into t_hibernate_order_item (order_item_id, product_id, quantity,
oid)
values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER},
#{oid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.zlp.model.OrderItem" >
insert into t_hibernate_order_item
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
order_item_id,
</if>
<if test="productId != null" >
product_id,
</if>
<if test="quantity != null" >
quantity,
</if>
<if test="oid != null" >
oid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
#{orderItemId,jdbcType=INTEGER},
</if>
<if test="productId != null" >
#{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
#{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
#{oid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zlp.model.OrderItem" >
update t_hibernate_order_item
<set >
<if test="productId != null" >
product_id = #{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
quantity = #{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
oid = #{oid,jdbcType=INTEGER},
</if>
</set>
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.zlp.model.OrderItem" >
update t_hibernate_order_item
set product_id = #{productId,jdbcType=INTEGER},
quantity = #{quantity,jdbcType=INTEGER},
oid = #{oid,jdbcType=INTEGER}
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
</mapper>
我们需要建立一个Vo类
package com.zlp.ssm.model.vo;
import com.zlp.ssm.model.Order;
import com.zlp.ssm.model.OrderItem;
import java.util.List;
/**
* @author Zlp
* @site 030boboz
* @company 聚星有限公司
* @create 2022-08-16 20:31
*
* Vo类不仅仅包含当前表的信息、还能包含相关联的表的信息
*
* 当前订单会有一个或者多个订单项,多个订单先必须要集合进行接受
*/
public class OrderVo extends Order {
//需求1:通过订单号查询本次订单的详情信息及对应的所有订单
private List<OrderItem> orderItems;
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}
OrderItemVo
package com.zlp.ssm.model.vo;
import com.zlp.ssm.model.Order;
import com.zlp.ssm.model.OrderItem;
import lombok.Data;
/**
* @author Zlp
* @site 030boboz
* @company 聚星有限公司
* @create 2022-08-16 20:51
*
* 某一个订单项一定属于某个订单的
*/
@Data
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
package com.zlp.mapper;
import com.zlp.model.OrderItem;
import com.zlp.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
public interface OrderItemMapper {
int deleteByPrimaryKey(Integer orderItemId);
int insert(OrderItem record);
int insertSelective(OrderItem record);
OrderItem selectByPrimaryKey(Integer orderItemId);
OrderItemVo selectByPrimaryKeys(@Param("orderItemId") Integer orderItemId);
int updateByPrimaryKeySelective(OrderItem record);
int updateByPrimaryKey(OrderItem record);
}
package com.zlp.biz;
import com.zlp.model.Order;
import org.apache.ibatis.annotations.Param;
/**
* @author Zlp
* @site 030boboz
* @company 聚星有限公司
* @create 2022-08-16 19:25
*/
public interface OrderBiz{
int deleteByPrimaryKey(Integer orderId);
int insert(Order record);
int insertSelective(Order record);
Order selectByPrimaryKey(Integer orderId);
int updateByPrimaryKeySelective(Order record);
int updateByPrimaryKey(Order record);
}
package com.zlp.biz;
import com.zlp.model.OrderItem;
import com.zlp.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
/**
* @author Zlp
* @site 030boboz
* @company 聚星有限公司
* @create 2022-08-16 19:54
*/
public interface OrderItemBiz {
int deleteByPrimaryKey(Integer orderItemId);
int insert(OrderItem record);
int insertSelective(OrderItem record);
OrderItem selectByPrimaryKey(Integer orderItemId);
OrderItemVo selectByPrimaryKeys( Integer orderItemId);
int updateByPrimaryKeySelective(OrderItem record);
int updateByPrimaryKey(OrderItem record);
}
package com.zlp.biz.impl;
import com.zlp.biz.OrderItemBiz;
import com.zlp.mapper.OrderItemMapper;
import com.zlp.mapper.OrderMapper;
import com.zlp.model.OrderItem;
import com.zlp.model.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Zlp
* @site 030boboz
* @company 聚星有限公司
* @create 2022-08-16 19:11
*/
@Service
public class OrderItemBizImpl implements OrderItemBiz {
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public int deleteByPrimaryKey(Integer orderItemId) {
return 0;
}
@Override
public int insert(OrderItem record) {
return 0;
}
@Override
public int insertSelective(OrderItem record) {
return 0;
}
@Override
public OrderItem selectByPrimaryKey(Integer orderItemId) {
return null;
}
@Override
public OrderItemVo selectByPrimaryKeys(Integer orderItemId) {
return orderItemMapper.selectByPrimaryKeys(orderItemId);
}
@Override
public int updateByPrimaryKeySelective(OrderItem record) {
return 0;
}
@Override
public int updateByPrimaryKey(OrderItem record) {
return 0;
}
}
二,多对多关系关联
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ruojuan.mapper.HbookMapper" >
<resultMap id="BaseResultMap" type="com.ruojuan.model.Hbook" >
<constructor >
<idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
book_id, book_name, price
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_book
where book_id = #{bookId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_book
where book_id = #{bookId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ruojuan.model.Hbook" >
insert into t_hibernate_book (book_id, book_name, price
)
values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
</insert>
<insert id="insertSelective" parameterType="com.zlp.model.Hbook" >
insert into t_hibernate_book
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bookId != null" >
book_id,
</if>
<if test="bookName != null" >
book_name,
</if>
<if test="price != null" >
price,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bookId != null" >
#{bookId,jdbcType=INTEGER},
</if>
<if test="bookName != null" >
#{bookName,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=REAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zlp.model.Hbook" >
update t_hibernate_book
<set >
<if test="bookName != null" >
book_name = #{bookName,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=REAL},
</if>
</set>
where book_id = #{bookId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.zlp.model.Hbook" >
update t_hibernate_book
set book_name = #{bookName,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where book_id = #{bookId,jdbcType=INTEGER}
</update>
</mapper>
package com.zlp.mapper;
import com.zlp.model.Hbook;
public interface HbookMapper {
int deleteByPrimaryKey(Integer bookId);
int insert(Hbook record);
int insertSelective(Hbook record);
Hbook selectByPrimaryKey(Integer bookId);
int updateByPrimaryKeySelective(Hbook record);
int updateByPrimaryKey(Hbook record);
}
package com.zlp.biz;
import com.zlp.model.HbookCategory;
import com.zlp.model.vo.CategoryVo;
import com.zlp.model.vo.HbookVo;
/**
* @author
* @site o3oboboz
* @company 聚星有限公司
* @create 2022-08-15 20:16
*/
public interface HbookCategoryBiz {
int deleteByPrimaryKey(Integer bcid);
int insert(HbookCategory record);
int insertSelective(HbookCategory record);
HbookCategory selectByPrimaryKey(Integer bcid);
int updateByPrimaryKeySelective(HbookCategory record);
int updateByPrimaryKey(HbookCategory record);
HbookVo queryByBookId( Integer bookId);
CategoryVo queryByCid( Integer cid);
}
package com.zlp.biz.impl;
import com.zlp.biz.HbookCategoryBiz;
import com.zlp.mapper.HbookCategoryMapper;
import com.zlp.model.HbookCategory;
import com.zlp.model.vo.CategoryVo;
import com.zlp.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public int deleteByPrimaryKey(Integer bcid) {
return 0;
}
@Override
public int insert(HbookCategory record) {
return 0;
}
@Override
public int insertSelective(HbookCategory record) {
return 0;
}
@Override
public HbookCategory selectByPrimaryKey(Integer bcid) {
return null;
}
@Override
public int updateByPrimaryKeySelective(HbookCategory record) {
return 0;
}
@Override
public int updateByPrimaryKey(HbookCategory record) {
return 0;
}
@Override
public HbookVo queryByBookId(Integer bookId) {
return hbookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
package com.zlp.biz.impl;
import com.zlp.biz.HbookCategoryBiz;
import com.zlp.model.vo.CategoryVo;
import com.zlp.model.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;
import static org.junit.Assert.*;
/**
* @author
* @site o3obobz
* @company 聚星有限公司
* @create 2022-08-15 20:23
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class HbookCategoryBizImplTest {
@Autowired
private HbookCategoryBiz hbookCategoryBiz;
@Test
public void queryByBookId() {
HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
System.out.println(hbookVo);
hbookVo.getCategories().forEach(System.out::println);
}
@Test
public void queryByCid() {
CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
System.out.println(categoryVo);
categoryVo.getHbooks().forEach(System.out::println);
}
}