版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LuuvyJune/article/details/90288474
首先,记录一下springboot整合mybatis的使用:
pom.xml:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
建立mapper类:
package com.xt.sell.dataobject.mapper;
import com.xt.sell.dataobject.ProductCategory;
public interface ProductCategoryMapper {
ProductCategory selectByCategoryType(Integer categoryType);
}
建立对应的xml文件:
文件路径在resource目录下:
<?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.xt.sell.dataobject.mapper.ProductCategoryMapper">
<resultMap id="BaseResultMap" type="com.xt.sell.dataobject.ProductCategory">
<id column="category_id" property="categoryId" jdbcType="INTEGER"/>
<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">
select category_id,category_name,category_type
from product_category
where category_type = #{category_type,jdbcType=INTEGER}
</select>
</mapper>
当然,参考SSM的配置应该还需要配置扫描到mapper类:在springboot启动类中进行配置mapperScan
package com.xt.sell;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.xt.sell.dataobject.mapper")
public class SellApplication {
public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}
}
以及还需要配置mapper中xml文件路径:在application.yml中配置
mybatis:
mapper-locations: classpath:mapper/*.xml
最后进行单元测试:
package com.xt.sell.dataobject.mapper;
import com.xt.sell.dataobject.ProductCategory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryMapperTest {
@Autowired
private ProductCategoryMapper mapper;
@Test
public void selectByCategoryType() {
ProductCategory productCategory = mapper.selectByCategoryType(3);
Assert.assertNotNull(productCategory);
}
}
运行此方法时,报错:
Error creating bean with name 'xxxx'
因为在IDEA中 mapper注入的时候抛红了,所以一直以为是mapper没有正确注入:
于是仔细检查了mapperScan中路径有没有写错,重新复制路径再次运行还是报错:
No enum constant org.apache.ibatis.type.JdbcType."INTEGER"
意思就是JdbcType中没有"INTEGER"这种类型,那就是xml文件中出错了,仔细检查原来一开始我的sql写成了这样:
<select id="selectByCategoryType" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select category_id,category_name,category_type
from product_category
where category_type = #{category_type,jdbcType="INTEGER"}
</select>
jdbcType="INTEGER",这里不需要加引号(这里我是直接从resultMap的column中的jdbcType复制的),去掉之后再次运行方法,成功!