-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
实现自己的mapper出现的问题 #532
Description
现在是ssm架构的spring 4.3.18 ,mybatis3.4.1 ,mybatis-spring1.3.1 ,通用mapper4.1.5 Intellij IDEA
通用mapper已按照wiki配置好(但是@Autowired红线? @resource不红)
我模仿https://mapperhelper.github.io/docs/5.professional/还有https://github.com/abel533/Mybatis-Spring写一个继承mapper和insertlistmapper的mymapper,再写通用service.
mymapper已经放在base-package外(有看到说4.0好像不需要还是??),并在xml里的扫描器配置好mappers=com.xxx.utils.MyMapper了,但是运行报错
使用@resource注解mapper时会报
Error creating bean with name 'adminBaseService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.lwq.util.MyMapper<?>' available: expected single matching bean but found 15: adminMapper,attendMapper,classMapper,classStudentMapper,courseMapper,courseStudentMapper,groupMapper,groupStudentMapper,leaveMapper,loginLogMapper,markMapper,messageMapper,recipientMapper,studentMapper,teacherMapper at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues
使用@Autowired注解mapper时会报
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminController': Unsatisfied dependency expressed through field 'adminBaseService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'adminBaseService' is expected to be of type 'com.lwq.service.impl.AdminBaseService' but was actually of type 'com.sun.proxy.$Proxy31'
当我写了16个空mapper时候会报
Error creating bean with name 'courseSMapper' defined in file [D:\Program Files\apache-tomcat-9.0.12\webapps\ROOT\WEB-INF\classes\com\lwq\mapper\CourseSMapper.class]: Invocation of init method failed; nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 这时候删掉几个mapper就会报上面的了
所以说现在问题在哪里,求前辈们指点一下
package com.lwq.service;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
@service
public interface CommonService {
int insert(T t);
int insertBatch(List<T> list);
int delete(T t);
int update(T t);
T selectOneByExample(Example example);
List<T> selectAll();
int selectCountByExample(Example example);
}
package com.lwq.service.impl;
import com.lwq.service.CommonService;
import com.lwq.util.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
public abstract class BaseService implements CommonService {
@Autowired
protected MyMapper myMapper;
public MyMapper<T> getMapper() {
return myMapper;
}
public int insert(T t){
return myMapper.insert(t);
}
public int delete(T t){
return myMapper.deleteByPrimaryKey(t);
}
public int update(T t){
return myMapper.updateByPrimaryKey(t);
}
public int insertBatch(List<T> list){
return myMapper.insertList(list);
}
public int selectCountByExample(Example example){
return myMapper.selectCountByExample(example);
}
public T selectOneByExample(Example example){
return myMapper.selectOneByExample(example);
}
@Override
public List<T> selectAll() {
return myMapper.selectAll();
}
}
package com.lwq.service;
import com.lwq.entity.Admin;
public interface AdminService extends CommonService{
}
package com.lwq.service.impl;
import com.lwq.entity.Admin;
import com.lwq.entity.LoginLog;
import com.lwq.mapper.LoginLogMapper;
import com.lwq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@transactional
@service("adminBaseService")
public class AdminBaseService extends BaseService implements AdminService {
@Autowired
private LoginLogMapper loginLogMapper;
public void loginSuccess(Admin admin){
LoginLog loginLog=new LoginLog();
loginLog.setUserName(admin.getUserName());
loginLog.setDate(new Date());
loginLog.setIp(admin.getLastIp());
loginLog.setType(0);
loginLogMapper.insert(loginLog);
}
@Override
public Admin selectOneByExample(Example example) {
return super.selectOneByExample(example);
}
@Override
public List<Admin> selectAll() {
return super.selectAll();
}
@Override
public int selectCountByExample(Example example) {
return super.selectCountByExample(example);
}
}