本文共 515 字,大约阅读时间需要 1 分钟。
最简单的说就是mybatis写Dao层,springmvc写视图层,然后通过spring耦合
单独使用mybatis的时候,我们总是要去获得sqlSession,整合的时候,spring配置文件里已经注入了,在服务层中,我们只要注入Mapper就可以使用了,
具体怎么配置我就不说了
在服务层注入持久层的mapper对象
eg
@Autowired
private userMapper userMapper;//这样就可以直接使用该接口的方法了,其他的步骤系统就已经封装好了,
然后再把服务层配置成spring的bean,这样就可以在控制层中使用服务层了,而不用去new一个对象
eg
在类上面添加
@Service("userService")把UserService配置成Spring的bean,名字为userService
最后,在控制器中注入服务层的接口就可以了
@Autowired
@Qualifier("userService")
private UserService userService;//最好加上Qualifier注解,Autowired是默认按类型查找的,当出现同类型的名字是就会出错。
大概就这样。
转载地址:http://kvjqi.baihongyu.com/