MyBatis Spring 集成 - 某些 ResultSet 值未在 POJO 上设置
MyBatis Spring Integration - some ResultSet values not set on POJO
我正在评估 MyBatis,遇到了一个问题,这个问题困扰了我 5 个小时。我的下一个选择是调试 MyBatis 源代码,但我希望有人遇到过这个问题。
这是我的 spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="jdbc.properties"/>
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id='dataSource' class='org.apache.commons.dbcp.BasicDataSource'>
<property name='driverClassName' value='${jdbc.driverClassName}'/>
<property name='url' value='${jdbc.url}'/>
<property name='username' value='${jdbc.username}'/>
<property name='password' value='${jdbc.password}'/>
</bean>
<!-- Declare a transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis.test.mapper.ProductMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="mybatis-config.xml"/>-->
</bean>
<bean id="productService" class="com.mybatis.test.ProductService">
<property name="mapper" ref="productMapper"/>
</bean>
这是我的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>
<mappers>
<mapper resource="com/mybatis/test/mapper/ProductMapper.xml"/>
</mappers>
</configuration>
这是我的映射 XML 文件 (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.mybatis.test.mapper.ProductMapper">
</mapper>
这是我的映射器界面 (ProductMapper.java):
package com.mybatis.test.mapper;
import com.mybatis.test.pojo.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface ProductMapper {
String SELECT_ALL_PRODUCTS = "select product_id, address_id, manufacturer_name, model_reference, product_name, description, price, url, date_added, " +
"create_user, update_user from product";
@Select(SELECT_ALL_PRODUCTS)
List<Product> getAllProducts();
}
这是我的 POJO(产品 class):
package com.mybatis.test.pojo;
import java.util.Date;
public class Product {
private Integer productId;
private Integer addressId;
private String manufacturerName;
private String modelReference;
private String productName;
private String description;
private Double price;
private String url;
private Date dateAdded;
private String createUser;
private String updateUser;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
public String getModelReference() {
return modelReference;
}
public void setModelReference(String modelReference) {
this.modelReference = modelReference;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
public Integer getAddressId() {
return addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
}
这是我的单元测试:
package com.mybatis.test.sql;
import com.mybatis.test.mapper.ProductMapper;
import com.mybatis.test.mapper.ProductMapperImpl;
import com.mybatis.test.pojo.Product;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static junit.framework.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductMapperTest {
@Autowired
private ProductMapper productMapper;
private String systemUser;
public void setProductMapper(ProductMapperImpl productMapper) {
this.productMapper = productMapper;
}
@Before
public void setup() {
systemUser = UUID.randomUUID().toString();
Product product = getProduct();
productMapper.insertProduct(product);
}
@After
public void tearDown() {
productMapper.deleteProductByCreateUser(systemUser);
}
@Test
public void testGetAllProducts() {
List<Product> products = productMapper.getAllProducts();
assertNotNull("Product list is null.", products);
for (Product product : products) {
assertTrue(product.getManufacturerName().equals("Western Digital"));
}
}
private Product getProduct() {
Product product = new Product();
product.setManufacturerName("Western Digital");
product.setProductName("My Passport Ultra");
product.setModelReference("WDBZFP0010BBK-EESN");
product.setDescription("USB3.0 1TB Hard disk drive");
product.setPrice(new Double(48.99));
product.setUrl("http://www.some web site.com");
product.setDateAdded(new Date());
product.setCreateUser(systemUser);
product.setUpdateUser(systemUser);
return product;
}
}
问题是:
虽然测试setup()方法中创建的Product成功写入mySqlDB,但是测试失败,因为从DB读取products时,只有以下字段设置了值POJO(Product.class):
- 描述
- url
- 价格
所有其他 POJO 字段都为空,尽管在数据库中的产品 table 中有一个值。我对这些领域的特别之处感到困惑。如果能帮助解决此问题,我将不胜感激。
方案一:在你的mybatis-config.xml中声明如下:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
选项 2:将 SELECT_ALL_PRODUCTS 更改为:
String SELECT_ALL_PRODUCTS = "select product_id as productId, address_id as addressId, manufacturer_name as manufacturerName, model_reference as modelReference, product_name as productName, description, price, url, date_added as dateAdded, " +
"create_user as createUser, update_user as updateUser from product";
我正在评估 MyBatis,遇到了一个问题,这个问题困扰了我 5 个小时。我的下一个选择是调试 MyBatis 源代码,但我希望有人遇到过这个问题。
这是我的 spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="jdbc.properties"/>
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id='dataSource' class='org.apache.commons.dbcp.BasicDataSource'>
<property name='driverClassName' value='${jdbc.driverClassName}'/>
<property name='url' value='${jdbc.url}'/>
<property name='username' value='${jdbc.username}'/>
<property name='password' value='${jdbc.password}'/>
</bean>
<!-- Declare a transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis.test.mapper.ProductMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="mybatis-config.xml"/>-->
</bean>
<bean id="productService" class="com.mybatis.test.ProductService">
<property name="mapper" ref="productMapper"/>
</bean>
这是我的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>
<mappers>
<mapper resource="com/mybatis/test/mapper/ProductMapper.xml"/>
</mappers>
</configuration>
这是我的映射 XML 文件 (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.mybatis.test.mapper.ProductMapper">
</mapper>
这是我的映射器界面 (ProductMapper.java):
package com.mybatis.test.mapper;
import com.mybatis.test.pojo.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface ProductMapper {
String SELECT_ALL_PRODUCTS = "select product_id, address_id, manufacturer_name, model_reference, product_name, description, price, url, date_added, " +
"create_user, update_user from product";
@Select(SELECT_ALL_PRODUCTS)
List<Product> getAllProducts();
}
这是我的 POJO(产品 class):
package com.mybatis.test.pojo;
import java.util.Date;
public class Product {
private Integer productId;
private Integer addressId;
private String manufacturerName;
private String modelReference;
private String productName;
private String description;
private Double price;
private String url;
private Date dateAdded;
private String createUser;
private String updateUser;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
public String getModelReference() {
return modelReference;
}
public void setModelReference(String modelReference) {
this.modelReference = modelReference;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
public Integer getAddressId() {
return addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
}
这是我的单元测试:
package com.mybatis.test.sql;
import com.mybatis.test.mapper.ProductMapper;
import com.mybatis.test.mapper.ProductMapperImpl;
import com.mybatis.test.pojo.Product;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static junit.framework.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductMapperTest {
@Autowired
private ProductMapper productMapper;
private String systemUser;
public void setProductMapper(ProductMapperImpl productMapper) {
this.productMapper = productMapper;
}
@Before
public void setup() {
systemUser = UUID.randomUUID().toString();
Product product = getProduct();
productMapper.insertProduct(product);
}
@After
public void tearDown() {
productMapper.deleteProductByCreateUser(systemUser);
}
@Test
public void testGetAllProducts() {
List<Product> products = productMapper.getAllProducts();
assertNotNull("Product list is null.", products);
for (Product product : products) {
assertTrue(product.getManufacturerName().equals("Western Digital"));
}
}
private Product getProduct() {
Product product = new Product();
product.setManufacturerName("Western Digital");
product.setProductName("My Passport Ultra");
product.setModelReference("WDBZFP0010BBK-EESN");
product.setDescription("USB3.0 1TB Hard disk drive");
product.setPrice(new Double(48.99));
product.setUrl("http://www.some web site.com");
product.setDateAdded(new Date());
product.setCreateUser(systemUser);
product.setUpdateUser(systemUser);
return product;
}
}
问题是:
虽然测试setup()方法中创建的Product成功写入mySqlDB,但是测试失败,因为从DB读取products时,只有以下字段设置了值POJO(Product.class):
- 描述
- url
- 价格
所有其他 POJO 字段都为空,尽管在数据库中的产品 table 中有一个值。我对这些领域的特别之处感到困惑。如果能帮助解决此问题,我将不胜感激。
方案一:在你的mybatis-config.xml中声明如下:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
选项 2:将 SELECT_ALL_PRODUCTS 更改为:
String SELECT_ALL_PRODUCTS = "select product_id as productId, address_id as addressId, manufacturer_name as manufacturerName, model_reference as modelReference, product_name as productName, description, price, url, date_added as dateAdded, " +
"create_user as createUser, update_user as updateUser from product";