I’m currently working on a shopping mall project, and I’ve been in charge of the product.
So I’ve created ProductDto from Spring, created mapper.xml, created the ProductDao interface, and also created ProductDaoImpl that implemented it.
Now I’m trying to run a test code for ProductDaoImpl, but when I keep getting errors.
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="ProductDto" type="com.toy2.shop29.product.domain.ProductDto"/>
</typeAliases>
</configuration>
productMapper.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.toy2.shop29.dao.ProductMapper">
<select id="select" parameterType="int" resultType="ProductDto">
SELECT product_id,
product_name,
small_category_id,
brand_id,
price,
sale_ratio,
is_exclusive,
created_date,
created_id,
updated_date,
updated_id
FROM product
where product_id = #{product_id}
</select>
...
ProductDaoImpl.java
package com.toy2.shop29.product.dao;
import com.toy2.shop29.product.domain.ProductDto;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ProductDaoImpl implements ProductDao {
@Autowired
SqlSession session;
String namespace = "com.toy2.shop29.dao.ProductMapper.";
//read
@Override
public ProductDto select(int product_id){
return session.selectOne(namespace + "select",product_id);
}
...
ProductDaoImplTest.java
package com.toy2.shop29.product.dao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ProductDaoImplTest {
@Autowired
ProductDao productDao;
@Test
void select() {
assertTrue(productDao!=null);
}
...
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘productDaoImpl’: Unsatisfied dependency expressed through field ‘session’: Error creating bean with name ‘sqlSessionTemplate’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘sqlSessionTemplate’ parameter 0: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception with message: Failed to parse mapping resource: ‘file [C:UsersuserDesktopnew-folderKDT_DBE1_Toy_Project2shop29buildresourcesmainmapperProductMapper.xml]’
I think the problem is that ProductMapper.xml wrote resultType=”ProductDto” with full name instead of ProductDto.
I changed it to fullname, resultType=”com.to.shop2.product.domain.productDto,” instead of ProductDto, and it doesn’t show testcode failure.
I think it’s a problem caused by a bad mapping somewhere
user25686647 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.