談到SSM,我在網(wǎng)上看了很多整合教程,我也跟著一起整合過,都有大大小小的問題,所以今天元旦假期,我就抽一上午寫出我自己的教程,一是Spring和MyBatis的整合,二是加上SpringMVC,即SSM大功告成。
首先我得說一下我的版本(我覺得版本是次要的,只要你弄清楚配置文件的關(guān)系,即怎么配置配置文件,什么版本都一樣,只是版本最大的問題我覺得是與jdk和tomcat這些有關(guān))
MyBatis 3.2.7
Spring 3.2.0
再給大家數(shù)據(jù)庫吧,我是MySQL,我把sql文件給你們,直接放數(shù)據(jù)庫運行就可以。
廢話不多說,先談?wù)?strong>mybatis和spring整合的思路
1、讓spring管理SqlSessionFactory2、讓spring管理mapper對象和dao。 使用spring和mybatis整合開發(fā)mapper代理及原始dao接口。 自動開啟事務(wù),自動關(guān)閉 sqlsession.3、讓spring管理數(shù)據(jù)源( 數(shù)據(jù)庫連接池)
在eclipse創(chuàng)建Java工程就行(還沒到SpringMVC呢)
先看看目錄結(jié)構(gòu)圖,因為我裝了Spring的插件,所以項目會有個S。dao層是為了實現(xiàn)dao和Mapper兩種不同的開發(fā),也可以先忽略,config是資源文件夾。
加入jar包
在這里就把所有jar包都一次給你,包括SpringMVC的。
mybatis與spring整合全部jar包(包括springmvc
1、mybatis3.2.7本身的jar包
2、數(shù)據(jù)庫驅(qū)動包
3、spring3.2.0
4、spring和mybatis整合包
從mybatis的官方下載spring和mybatis整合包
log4j.properties這個也是Spring依賴的日志文件,大家如果需要自行下載工程查看哈。
SqlMapconfig.xml
mybatis配置文件:別名、settings,數(shù)據(jù)源不在這里配置(因為有了Spring來管理)
configuration> typeAliases> package name='com.hust.springmybatis.po'/> typeAliases> mappers> mapper resource='sqlmap/User.xml' /> mapper resource='mapper/UserMapper.xml' /> mappers>configuration>
applicationContext.xml
1、數(shù)據(jù)源(dbcp連接池)(db.properties自行下載工程查看哈,再說這個一般人都會)
2、SqlSessionFactory
3、mapper或dao
<>xml version='1.0' encoding='UTF-8'?>beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd'> context:property-placeholder location='classpath:db.properties'/> bean id='dataSource' class='org.apache.commons.dbcp.BasicDataSource' destroy-method='close'> property name='driverClassName' value='${jdbc.driver}'>property> property name='url' value='${jdbc.url}'>property> property name='username' value='${jdbc.username}'>property> property name='password' value='${jdbc.password}'>property> property name='maxActive' value='10'>property> property name='maxIdle' value='5'>property> bean> bean id='sqlSessionFactory' class='org.mybatis.spring.SqlSessionFactoryBean'> property name='dataSource' ref='dataSource'>property> property name='configLocation' value='classpath:mybatis/SqlMapConfig.xml'>property> bean> bean id='userDao' class='com.hust.springmybatis.dao.UserDaoImpl'> property name='sqlSessionFactory' ref='sqlSessionFactory'>property> bean> bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> property name='basePackage' value='com.hust.springmybatis.mapper'>property> property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'>property> bean>beans>
配置SqlSessionFactory
在 applicationContext.xml配置SqlSessionFactory
開發(fā)dao,這里就舉findUserById這個例子
dao接口
dao的實現(xiàn)類,在這兒調(diào)用配置文件配置的東西(test是配置文件的namespace)
user.xml配置文件,這個id必須和dao里面的方法名一致
最有別忘了去Spring的配置文件applicationContext.xml里面配置dao
以上完成之后,就可以開始測試dao接口
這樣Dao的開發(fā)就大功告成,不過不要驚喜,我們還要實現(xiàn)Mapper的代理方法。
開發(fā)mapper.xml和mapper.java
一種是使用MapperFactoryBean,使用此方法對于每個mapper都需要配置,比較繁瑣。所以我們使用第二種,MapperScannerConfigurer(掃描mapper)
在spring的配置文件配置掃描mapper
開發(fā)mapper,還是以findUserById為例
UserMapper.java
UserMapper.xml
這個xml可以放在和UserMapper.java一個包里面,就會被Spring掃描到,這個工程,我是單獨放在資源文件的mapper里面,所以得在在SqlMapConfig.xml里面配置
測試mapper接口
至此,Spring與MyBaits的整合與開發(fā)都可以了。