MyBatis框架mapper配置文件问题

都先生 2017-05-16 04:20:42
1、mapper.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.jubobo.dcmanager.mapper.BrandMapper">
<select id="getAllBrands" resultType="Brand">
select MesId,Name,Prod_typ,LastTime,Mask from brand where 1=1
</select>
<select id="getBrandByMesID" resultType="Brand">
select MesId,Name,Prod_typ,LastTime,Mask from brand where MesId=#{mesid}
</select>
<insert id="Insert">
insert into brand (MesId,Name,Prod_typ,LastTime,Mask)
values(#{MesId},#{Name},#{Prod_typ},#{LastTime},#{Mask})
</insert>
<delete id="Delete">
delete from brand where MesId=#{mesid}
</delete>
<update id="Update">
update brand set MesId=#{MesId},Name=#{Name},Prod_typ=#{Prod_typ},LastTime=#{LastTime},Mask=#{Mask}
where MesId=#{mesid}
</update>
</mapper>


2、bean
package com.jubobo.dcmanager.entities;

import java.sql.Timestamp;

import org.apache.ibatis.annotations.Param;

/*
* 牌号数据
*/
public class Brand{

/*
* Mes系统唯一标识
*/
private int MesId;
/*
* 牌号名称
*/
private String Name;
/*
* 产品类型(如:卷包)
*/
private String Prod_typ;
/*
* 最后修改时间
*/
private String LastTime;
/*
* 是否屏蔽
*/
private String Mask;

public Brand(int mesid,String name,String prod_type,String lasttime,String mask)
{
super();
this.MesId = mesid;
this.Name = name;
this.Prod_typ = prod_type;
this.LastTime = lasttime;
this.Mask = mask;

}

public int getMesId() {
return MesId;
}
public void setMesId(int mesId) {
MesId = mesId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getProd_typ() {
return Prod_typ;
}
public void setProd_typ(String prod_typ) {
Prod_typ = prod_typ;
}
public String getLastTime() {
return LastTime;
}
public void setLastTime(String lastTime) {
LastTime = lastTime;
}
public String getMask() {
return Mask;
}
public void setMask(String mask) {
Mask = mask;
}


}


3、服务实现
package com.jubobo.dcmanager.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jubobo.dcmanager.entities.Brand;
import com.jubobo.dcmanager.mapper.BrandMapper;

@Service
public class BrandService{
@Resource
BrandMapper branddao;

public List<Brand> getAllBrands()
{
return branddao.getAllBrands();
}

public Brand getBrandByMesID(String mesid)
{
return branddao.getBrandByMesID(mesid);
}

public int Insert(Brand brand)throws Exception
{
if(brand.getMesId()<0){
throw new Exception("MesId不能小于0");
}
if(brand.getName().equals("")||brand.getName()==null){
throw new Exception("牌号名称不能为空");
}
if(brand.getProd_typ().equals("")||brand.getProd_typ()==null){
throw new Exception("牌号类型不能为空");
}
if(brand.getMask().equals("")||brand.getMask()==null){
throw new Exception("屏蔽属性不能为空");
}
return branddao.Insert(brand);

}

public int Insert(Brand entity1,Brand entityBak){
int rows=0;
rows=branddao.Insert(entity1);
rows=branddao.Insert(entityBak);
return rows;
}

public int Delete(String id)
{
return branddao.Delete(id);
}

/**
* 多删除
*/
public int Delete(String[] ids){
int rows=0;
for (String idStr : ids) {
//int id=Integer.parseInt(idStr);
rows+=Delete(idStr);
}
return rows;
}

public int Update(Brand brand)
{
return branddao.Update(brand);
}

}


4、JUnit测试代码
package com.jubobo.dcmanager.test;

import static org.junit.Assert.*;

import java.sql.Timestamp;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jubobo.dcmanager.entities.Brand;
import com.jubobo.dcmanager.service.BrandService;

public class TestBrandService {
static BrandService brandService;

@Before
public void before()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
brandService = ctx.getBean(BrandService.class);
}

@Test
public void testGetAllBrands()
{
List<Brand> brands = brandService.getAllBrands();
assertNotNull(brands);
}
}


4、异常提示
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.jubobo.dcmanager.entities.Brand matching [java.lang.Integer, java.lang.String, java.lang.String, java.sql.Timestamp, java.lang.String]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
at com.sun.proxy.$Proxy13.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:231)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy14.getAllBrands(Unknown Source)
at com.jubobo.dcmanager.service.BrandService.getAllBrands(BrandService.java:20)
at com.jubobo.dcmanager.test.TestBrandService.testGetAllBrands(TestBrandService.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in com.jubobo.dcmanager.entities.Brand matching [java.lang.Integer, java.lang.String, java.lang.String, java.sql.Timestamp, java.lang.String]
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:650)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:599)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:574)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:379)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:338)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:313)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:286)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:183)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 32 more

...全文
251 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
都先生 2017-05-16
  • 打赏
  • 举报
回复
楼上,知道是这个出问题。应该怎么配置呢? bean实体的类型和数据库字段的类型不一样
a3393723 2017-05-16
  • 打赏
  • 举报
回复
<select id="getBrandByMesID" resultType="Brand"> 这个出问题。后面你自己找
都先生 2017-05-16
  • 打赏
  • 举报
回复
数据库返回的类型与bean定义的类型不一致。很多说法说可以不一致。请大家帮忙找下原因。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧