一定要下載源碼自己去琢磨,自己配置一遍談到SSM,我在網(wǎng)上看了很多整合教程,我也跟著一起整合過,都有大大小小的問題,所以今天元旦假期,我就抽一上午寫出我自己的教程,一是Spring和MyBatis的整合,二是加上SpringMVC,即SSM大功告成。 首先我得說一下我的版本(我覺得版本是次要的,只要你弄清楚配置文件的關(guān)系,即怎么配置配置文件,什么版本都一樣,只是版本最大的問題我覺得是與jdk和tomcat這些有關(guān)) MyBatis 3.2.7 再給大家數(shù)據(jù)庫吧,我是MySQL,我把sql文件給你們,直接放數(shù)據(jù)庫運(yùn)行就可以。 廢話不多說,先談?wù)?strong>mybatis和spring整合的思路 1、讓spring管理SqlSessionFactory2、讓spring管理mapper對(duì)象和dao。 使用spring和mybatis整合開發(fā)mapper代理及原始dao接口。 自動(dòng)開啟事務(wù),自動(dòng)關(guān)閉 sqlsession.3、讓spring管理數(shù)據(jù)源( 數(shù)據(jù)庫連接池)在eclipse創(chuàng)建Java工程就行(還沒到SpringMVC呢) 先看看目錄結(jié)構(gòu)圖,因?yàn)槲已b了Spring的插件,所以項(xiàng)目會(huì)有個(gè)S。dao層是為了實(shí)現(xiàn)dao和Mapper兩種不同的開發(fā),也可以先忽略,config是資源文件夾。 加入jar包 在這里就把所有jar包都一次給你,包括SpringMVC的。 mybatis與spring整合全部jar包(包括springmvc 1、mybatis3.2.7本身的jar包 log4j.properties這個(gè)也是Spring依賴的日志文件,大家如果需要自行下載工程查看哈。 重點(diǎn)(配置文件),別急著復(fù)制配置文件,因?yàn)榫唧w的要看開發(fā)情況,先了解一下SqlMapconfig.xml configuration> typeAliases> package name='com.hust.springmybatis.po'/> typeAliases> mappers> mapper resource='sqlmap/User.xml' /> mapper resource='mapper/UserMapper.xml' /> mappers>configuration>applicationContext.xml <>xml version='1.0' encoding='UTF-8'?>beans xmlns='http://www./schema/beans' xmlns:xsi='http://www./2001/XMLSchema-instance' xmlns:context='http://www./schema/context' xmlns:aop='http://www./schema/aop' xsi:schemaLocation='http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context-3.2.xsd http://www./schema/aop http://www./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>整合開發(fā)原始dao接口配置SqlSessionFactory 在 applicationContext.xml配置SqlSessionFactory 開發(fā)dao,這里就舉findUserById這個(gè)例子 dao接口 dao的實(shí)現(xiàn)類,在這兒調(diào)用配置文件配置的東西(test是配置文件的namespace) user.xml配置文件,這個(gè)id必須和dao里面的方法名一致 最有別忘了去Spring的配置文件applicationContext.xml里面配置dao 以上完成之后,就可以開始測(cè)試dao接口 這樣Dao的開發(fā)就大功告成,不過不要驚喜,我們還要實(shí)現(xiàn)Mapper的代理方法。 開發(fā)mapper.xml和mapper.java 一種是使用MapperFactoryBean,使用此方法對(duì)于每個(gè)mapper都需要配置,比較繁瑣。所以我們使用第二種,MapperScannerConfigurer(掃描mapper) 在spring的配置文件配置掃描mapper 開發(fā)mapper,還是以findUserById為例 UserMapper.java UserMapper.xml 這個(gè)xml可以放在和UserMapper.java一個(gè)包里面,就會(huì)被Spring掃描到,這個(gè)工程,我是單獨(dú)放在資源文件的mapper里面,所以得在在SqlMapConfig.xml里面配置
測(cè)試mapper接口
至此,Spring與MyBaits的整合與開發(fā)都可以了。 一定要下載源碼自己去琢磨,自己配置一遍 |
|
|