一对多
首先利用ssm逆向生成工具,将我们所需要的表的对应的代码生成。
generatorConfig.xml
<?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:\\apache-maven\\mvn_repository\\mysql\\mysql-connector-java\5.1.44"/>
<!-- 一个数据库一个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.lg.ssm.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.lg.ssm.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.lg.ssm.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_order" domainObjectName="Order"
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_order_item" domainObjectName="OrderItem"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</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>
生成的:
接着,建立order和orderItem的vo类:
package com.lg.ssm.model.vo;
import com.lg.ssm.model.Order;
import com.lg.ssm.model.OrderItem;
import java.util.List;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-22 11:37
*/
public class OrderVo extends Order {
private List<OrderItem> orderItem;
public List<OrderItem> getOrderItem() {
return orderItem;
}
public void setOrderItem(List<OrderItem> orderItem) {
this.orderItem = orderItem;
}
}
package com.lg.ssm.model.vo;
import com.lg.ssm.model.Order;
import com.lg.ssm.model.OrderItem;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-22 11:42
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
然后在ordermapper.xml中进行一对多的配置:
<!--一对多-->
<resultMap id="OrderVoMap" type="com.lg.ssm.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItem" ofType="com.lg.ssm.model.OrderItem">
<result property="orderItemId" column="order_item_id"></result>
<result property="productId" column="product_id"></result>
<result property="quantity" column="quantity"></result>
<result property="oid" column="oid"></result>
</collection>
</resultMap>
<!--一对多方法-->
<select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{orderId}
orderMapper.java中对应的方法:
OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
orderItemMapper.xml中进行配置:
<resultMap id="OrderItemVoMap" type="com.lg.ssm.model.vo.OrderItemVo" >
<result property="orderItemId" column="order_item_id"></result>
<result property="productId" column="product_id"></result>
<result property="quantity" column="quantity"></result>
<result property="oid" column="oid"></result>
<association property="order" javaType="com.lg.ssm.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
<--方法-->
<select id="queryOrderItemVoByOrderItemId" resultMap="OrderItemVoMap" 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>
OrderItemMapper.java 中对应的方法
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
建立Many2ManyService类:
package com.lg.ssm.service;
import com.lg.ssm.model.vo.CategoryVo;
import com.lg.ssm.model.vo.HbookVo;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:47
*/
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
实现接口:
package com.lg.ssm.service.impl;
import com.lg.ssm.mapper.OrderItemMapper;
import com.lg.ssm.mapper.OrderMapper;
import com.lg.ssm.model.vo.OrderItemVo;
import com.lg.ssm.model.vo.OrderVo;
import com.lg.ssm.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 14:48
*/
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo queryOrderVoByOrderId(Integer orderId) {
return orderMapper.queryOrderVoByOrderId(orderId);
}
@Override
public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
}
}
测试方法:
package com.lg.ssm.service.impl;
import com.lg.ssm.SpringBaseTest;
import com.lg.ssm.model.OrderItem;
import com.lg.ssm.model.vo.OrderItemVo;
import com.lg.ssm.model.vo.OrderVo;
import com.lg.ssm.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 14:53
*/
public class One2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private One2ManyService one2ManyService;
@Test
public void queryOrderVoByOrderId() {
OrderVo orderVo= one2ManyService.queryOrderVoByOrderId(8);
System.out.println(orderVo.getOrderNo());
for (OrderItem orderItem : orderVo.getOrderItem()) {
System.out.println(orderItem);
}
}
@Test
public void queryOrderItemVoByOrderItemId() {
OrderItemVo orderItemVo = one2ManyService.queryOrderItemVoByOrderItemId(36);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder().getOrderNo());
}
}
测试结果:
多对多
建立HBook和HCategory的vo类
package com.lg.ssm.model.vo;
import com.lg.ssm.model.Category;
import com.lg.ssm.model.Hbook;
import java.util.List;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:23
*/
public class HbookVo extends Hbook {
private List<Category> categories;
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
package com.lg.ssm.model.vo;
import com.lg.ssm.model.Category;
import com.lg.ssm.model.Hbook;
import java.util.List;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:25
*/
public class CategoryVo extends Category {
private List<Hbook> hbook;
public List<Hbook> getHbook() {
return hbook;
}
public void setHbook(List<Hbook> hbook) {
this.hbook = hbook;
}
}
接着在这两张表的中间表进行配置:
HbookCategoryMapper
<!--多对多-->
<resultMap id="HbookVoMap" type="com.lg.ssm.model.vo.HbookVo" >
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
<collection property="categories" ofType="com.lg.ssm.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.lg.ssm.model.vo.CategoryVo" >
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbook" ofType="com.lg.ssm.model.Hbook">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
</collection>
</resultMap>
<!--方法-->
<select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
</select>
<select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
</select>
HbookCategoryMapper.java
package com.lg.ssm.mapper;
import com.lg.ssm.model.HbookCategory;
import com.lg.ssm.model.vo.CategoryVo;
import com.lg.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
public interface HbookCategoryMapper {
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(@Param("bookId") Integer bookId);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
HbookCategoryMapper接口:
package com.lg.ssm.service;
import com.lg.ssm.model.vo.CategoryVo;
import com.lg.ssm.model.vo.HbookVo;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:47
*/
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
实现接口:
package com.lg.ssm.service.impl;
import com.lg.ssm.mapper.HbookCategoryMapper;
import com.lg.ssm.model.vo.CategoryVo;
import com.lg.ssm.model.vo.HbookVo;
import com.lg.ssm.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:48
*/
@Service
public class Many2ManyServiceImpl implements Many2ManyService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBookId(Integer bookId) {
return hbookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
测试类
package com.lg.ssm.service.impl;
import com.lg.ssm.SpringBaseTest;
import com.lg.ssm.model.Category;
import com.lg.ssm.model.Hbook;
import com.lg.ssm.model.vo.CategoryVo;
import com.lg.ssm.model.vo.HbookVo;
import com.lg.ssm.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-11-23 15:51
*/
public class Many2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private Many2ManyService many2ManyService;
@Test
public void queryByBookId() {
HbookVo hbookVo = many2ManyService.queryByBookId(8);
System.out.println(hbookVo);
for (Category category : hbookVo.getCategories()) {
System.out.println(category);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = many2ManyService.queryByCid(8);
System.out.println(categoryVo);
for (Hbook hbook : categoryVo.getHbook()) {
System.out.println(hbook);
}
}
}
测试结果:
1.
2.