自定义 mapper

1. 定义一个接口

public interface ItemMapper {
    
    List<Item> getItemList();
}

2. 编写  xml  文件 ,  将sql 语句填入进去,  id为接口中的方法名称,  返回类型为自定义的pojo对象

<?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.sys.search.mapper.ItemMapper" >
    <select id="getItemList" resultType="com.sys.search.pojo.Item">
        select a.id,a.title,a.sell_point,a.price,a.image,b.`name` category_name,c.item_desc from tb_item a LEFT JOIN tb_item_cat b ON a.cid=b.id LEFT JOIN tb_item_desc c ON a.id=c.item_id;
    </select>
</mapper>

3. applicationContext中配置加载 mapper 代理对象

<!-- 配置扫描包,加载mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sys.rest.mapper,com.sys.search.mapper"></property>
    </bean>

猜你喜欢

转载自www.cnblogs.com/redhat0019/p/10192049.html