SpringMVC+Spring+MyBatis搭建過程

在閱讀文章之前,請熟悉Spring、SpringMVC、MyBatis

Demo下載地址:SpringMVC+Spring+MyBatis(工程在Eclipse中的m2eclipse插件中構建,最新版的Eclipse for Javaee中自帶m2eclipse)

工程分爲三個部分:



1、首先是Spring和Mybatis整合,具體步驟爲jdbc.properties(數據庫連接信息)->mybatis.config(MyBatis的別名信息)->mapper(“SQL語句”和“DAO”之間的映射關係)->Spring-MyBatis(將映射關係注入到DAO層)


2、DAO層撘完搭Service層,這裏採用“註解”的方式,直接在ServiceImpl中註解@Service,裏面的屬性採用@Autowired,並在Spring-MyBatis.xml裏面添加<context:component-scan base-package="com.demo.ServiceImpl"/>就能夠自動注入了,Service層搭建成之後,進行單元測試,沒問題了再繼續


3、搭建Controller層,主要注意:web.xml要讀取Spring-MyBatis.xml。在用Spring-mvc.xml構建Controller的bean時,也採用註解方式,主要是使用@Controller@Autowired,這種方式要求把前面的DAO層配置文件中的<context:component-scan base-package="com.demo.ServiceImpl"/>刪掉,並在Spring-mvc.xml中添加<context:component-scan base-package="com.demo"/>,而且我們 採用@RequestMapping驅動Controller,在Spring-mvc.xml中添加<mvc:annotation-driven />,至此,工程搭建完畢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章