目录
2.2一对多:一本书对应多种书本类型,例如:西游记 -> 神话、古典、名著
2.3多对一:多本书指向一种书本类型,例如:西游记、山海经、聊斋志异 -> 神话
4、通过 mybatis-generator 插件生成dao、mapper、model
4.1配置mybatis-generator插件生成文件位位置
4.2修改generatorConfig.xml配置文件的生成目录(mapper和model)及对应生成关系
关联关系是指类之间的引用关系,如果类A与类B关联,那么类A将被定义成类B的属性。
- 例如:
- public class A{
- private String name;
- }
-
- public class B{
- private String sid;
- private Float score;
- private Address address;
- }
一对一,一对多,多对一,多对多
- -- 一对多
- -- 客户表(主表)
- create table t_customer
- (
- customer_id int primary key not null auto_increment,
- customer_name varchar(50) not null
- );
-
- -- 多对一
- -- 订单表(从表)
- create table t_order
- (
- order_id int primary key not null auto_increment,
- order_no varchar(50) not null unique,
- cid int not null,
- foreign key(cid) references t_customer(customer_id)
- );
- #一对多:一个客户对应多个订单
- private List
orders=new ArrayList(); -
- #多对一:多个订单对应一个客户(一个订单对应一个客户)
- private Customer customer;
6.1 一对多
- <resultMap id="one2many" type="Customer">
- <id column="customer_id" property="customerId"/>
- <result column="customer_name" property="customerName"/>
-
-
- <collection property="orders" ofType="Order">
- <id column="order_id" property="orderId"/>
- <result column="order_no" property="orderNo"/>
- collection>
- resultMap>
6.2 多对一
- <resultMap id="many2one" type="Order">
- <id column="order_id" property="orderId"/>
- <result column="order_no" property="orderNo"/>
- <result column="cid" property="cid"/>
-
-
-
- <association property="customer" javaType="Customer">
- <id column="customer_id" property="customerId"/>
- <result column="customer_name" property="customerName"/>
- association>
- resultMap>
- package com.xnx.ssm.mapper;
-
- import com.xnx.ssm.model.Order;
- import com.xnx.ssm.model.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);
-
- OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
-
- int updateByPrimaryKeySelective(Order record);
-
- int updateByPrimaryKey(Order record);
- }