首页 > IT职业 > IT图书 > 从零开始写项目第一篇【搭建环境】
又去查了一大堆的资料,发现还是因为mysql的驱动包太高的问题。只能往下转了。
Failed to read candidate component class错误分析:
spring 3.2不支持1.8编译环境,解决办法就是降为1.7编译环境
解决连接:http://www.cnblogs.com/cxj20160928/p/5954336.html
编写BaseMapper
/**
* 将通用的方法抽取在BaseMapper中,那么就不用在每个单独的Mapper都要写对应的方法了。
* @param
*/
public interface BaseMapper {
int insert(T entity);
int insertSelective(T entity);
int deleteByPrimaryKey(String id);
T selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(T entity);
int updateByPrimaryKey(T entity);
}
编写BaseService
/**
* 将通用的业务方法抽取到BaseService中,那么实现它的Service都有这些方法了。
* @param
*/
public interface BaseService {
int insert(T entity);
int insertSelective(T entity);
int deleteByPrimaryKey(String id);
T selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(T entity);
int updateByPrimaryKey(T entity);
}
BaseServiceImpl
/**
* 使用initBaseMapper()将baseMapper实例化,service实现类是什么类型,baseMapper就是什么类型
* 将所有的Mapper都定义出来,那么子类service就可以直接使用了
* @param
*/
public class BaseServiceImpl implements BaseService {
protected BaseMapper baseMapper;
@Autowired
protected StoreHouseMapper storeHouseMapper;
@PostConstruct
private void initBaseMapper() throws Exception{
//获取泛型的信息
ParameterizedType type =(ParameterizedType) this.getClass().getGenericSuperclass();
Class clazz = (Class)type.getActualTypeArguments()[0];
//拼接成“泛型”Mapper字符串
String localField = clazz.getSimpleName().substring(0,1).toLowerCase()+clazz.getSimpleName().substring(1)+"Mapper";
//通过反射来获取成员变量的值
Field field=this.getClass().getSuperclass().getDeclaredField(localField);
Field baseField = this.getClass().getSuperclass().getDeclaredField("baseMapper");
//将baseDao来进行实例化
baseField.set(this, field.get(this));
System.out.println("------this:" + this);
System.out.println("------baseMapper:" + baseMapper);
}
public int insert(T entity) {
return baseMapper.insert(entity);
}
public int insertSelective(T entity) {
return baseMapper.insertSelective(entity);
}
public int deleteByPrimaryKey(String id) {
return baseMapper.deleteByPrimaryKey(id);
}
public T selectByPrimaryKey(String id) {
return baseMapper.selectByPrimaryKey(id);
}
public int updateByPrimaryKeySelective(T entity) {
return baseMapper.updateByPrimaryKeySelective(entity);
}
public int updateByPrimaryKey(T entity) {
return baseMapper.updateByPrimaryKey(entity);
}
}
测试Spring和Mybatis是否整合完成
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/application-context.xml"})
public class TestSpringAndMybatis {
@Autowired
private StoreHouseService hoseService;
@Test
public void add() {
StoreHouse storeHouse = hoseService.selectByPrimaryKey("1");
System.out.println(storeHouse);
}
} 
/**
* 将所有的service实现都定义出来,controller子类使用的使用就不需要定义了。
*/
public class BaseController {
@Autowired
protected StoreHouseService storeHoseService;
} StoreHouseController
@Controller
@RequestMapping("/store")
public class StoreHouseController extends BaseController {
@RequestMapping("/getById.do")
public void getById(String id ) {
StoreHouse storeHouse = storeHoseService.selectByPrimaryKey(id);
System.out.println(storeHouse);
}
} 测试spring-mvc 在测试的时候出现了: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)的异常。
上网上查看之后,****。因此我就将StoreHouseMapper.xml放到resouces下了。
同时,我们的application-context.xml加载配置文件和generator.xml逆向配置文件的位置也要修改。

说要把生成出来的mapper.xml放在resouces目录下才能识别出来
<!--配置扫描式加载SQL映射文件,那么在mybatis配置文件中就不用加载了-->
<!-- 所生成的sqlMap的影射文件的位置,默认资源包src -->
测试JSON 测试代码:
@RequestMapping("/getById.do")
@ResponseBody
public Object getById(String id ) {
StoreHouse storeHouse = storeHoseService.selectByPrimaryKey(id);
System.out.println(storeHouse);
return storeHouse;
} 
到目前为止、SSM已整合完成。
创建web常用的目录结构。
想用新版本的jar包遇到了很多的麻烦
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3932/viewspace-2800093/ ,如需转载,请注明出处,否则将追究法律责任。