首先肯定还是引入mybatis依赖
1 2 3 4 5 6 | <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> |
在启动类处扫描mybatis的mapper类
写一个mapper接口类
定义好查询方法
在resources下编写mapper接口类对应的xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?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开始就是自己定义的了--> <mapper namespace="com.chan.wechatshop.dataobject.mapper.ProductCategoryMapper"> <!--对应的mapper类的包路径--> <resultMap id="baseResultMap" type="com.chan.wechatshop.dataobject.ProductCategory"> <!--返回查询结果对应的实体类--> <id column="category_id" property="categoryId" jdbcType="INTEGER"/> <!--这里的id代表主键--> <id column="category_name" property="categoryName" jdbcType="VARCHAR"/> <id column="category_type" property="categoryType" jdbcType="INTEGER"/> </resultMap> <select id="selectByCategoryType" resultMap="baseResultMap" parameterType="java.lang.Integer"> <!--传参是对象的话parameterType就写那个对象的路径,这边是int的type--> select category_id, category_name, category_type, create_time, update_time from product_category where category_type = #{category_type, jdbcType=INTEGER } </select> </mapper> |
在yml中配置,可以扫描到xml文件
1 2 | mybatis: mapper-locations: classpath:mapper/*.xml #配置mybatis扫描mapper文件xml的路径 |