springboot整合mybtis实现动态代理Dao的相关配置(一对一关联查询)
作者:互联网
一
在pom.xml文件中添加依赖(这里就不详细叙述了,可以百度)。
二
在启动类中通过@MapperScan注解配置扫描mapper接口
package com.syc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//通过注解配置扫描mapper接口所在包
@MapperScan(value="com.syc.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
三
在application.yml文件中添加mybatis相关配置
mybatis:
#配置实体类别名包扫描
type-aliases-package: com.syc.domain
#配置mybatis中mapper接口对应xml文件所在路径
mapper-locations: classpath*:mapperConfig/*Mapper.xml
四
给mapper接口添加@Mapper注解(由于我之前引用了通用mapper,所以代码中@Mapper注解为全路径),正常情况下直接添加@Mapper注解就可以
package com.syc.mapper;
import com.syc.domain.Orders;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
@org.apache.ibatis.annotations.Mapper
public interface OrdersMapper extends Mapper<Orders> {
List<Orders> getOrderProduct();
}
五
编写OrdersMapper.xml文件
<?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.syc.mapper.OrdersMapper">
<resultMap id="order_product_map" type="com.syc.domain.Orders">
<id property="id" column="id"/>
<result property="ordernum" column="ordernum"/>
<result property="ordertime" column="ordertime"/>
<result property="peoplecount" column="peoplecount"/>
<result property="orderdesc" column="orderdesc"/>
<result property="paytype" column="paytype"/>
<result property="orderstatus" column="orderstatus"/>
<result property="productid" column="productid"/>
<!--association用于配置一对一关系 property:orders中product实体 javaType:product实体的数据类型(product对应的实体类)-->
<association property="product" javaType="com.syc.domain.Product">
<id property="id" column="productid"/>
<result property="productnum" column="productnum"/>
<result property="productname" column="productname"/>
<result property="cityname" column="cityname"/>
<result property="departuretime" column="departuretime"/>
<result property="productprice" column="productprice"/>
<result property="productdesc" column="productdesc"/>
<result property="productstatus" column="productstatus"/>
</association>
</resultMap>
<select id="getOrderProduct" resultMap="order_product_map">
SELECT
o.id,
o.ordernum,
o.ordertime,
o.peoplecount,
o.orderdesc,
o.paytype,
o.orderstatus,
o.productid,
p.productnum,
p.productname,
p.cityname,
p.departuretime,
p.productprice,
p.productdesc,
p.productstatus
FROM
orders o
LEFT JOIN product p
ON o.productid = p.id
</select>
</mapper>
标签:syc,mapper,mybtis,springboot,Mapper,Dao,mybatis,import,com 来源: https://blog.csdn.net/EIiejor/article/details/93396227