前面已經說了,springMVC+spring+Mybatis的整合思路了,現在就照着這個思路來做一下:
在開始之前先來看一下工程的目錄結構:

config這個目錄底下放的都是配置文件:
mybatis-config.xml是Mybatis的全局配置文件
applicationContext.xml是spring的配置文件
dispatch-servlet.xml是springMVC的配置文件
另外兩個是數據庫的文件和日志文件
首先是jar,對於jar我就不貼出來了(這里整合的都是3.x的)記得把該有的jar都添加進去就好了
整合dao(spring和mybatis的整合過程):就是在applicationContext.xml中添加底下的bean
配置數據源的文件路徑
配置數據源
配置SqlSessionFactory
Mapper的管理(掃描的過程)
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置數據源的文件的位置 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置數據源 可以使用DriverManagerDataSource 或ComboPooledDataSource 后者比較強大:com.mchange.v2.c3p0.ComboPooledDataSource 需要c3p0的包 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 數據庫連接池(需要數據源連接池的可以自己配置) --> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 指定Mybatis配置文件的路徑 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> </bean> <!-- 配置mapper的掃描包 --> <!-- 去掃描bean的時候,有一個取名字的規則:bean的名字=原先類的類名小寫,這個是為了做測試的時候看看spring和Mybatis有沒有整合成功的時候需要使用到 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.etc.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
db.properties配置文件的配置信息
#數據源的配置信息 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/jc?characterEncoding=utf-8 jdbc.username=root jdbc.password=root
log4j.properties的配置信息
log4j.rootLogger=DEBUG, stdout log4j.appender.CONSOLE.Encoding=UTF-8 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%n log4j.appender.syslog.encoding=UTF-8
到這來spring和Mybatis就整合完成了,完成之后,建議大家建立一個測試類,測試一下spring和Mybatis是否整合成功,否則后期一旦出錯,就要從頭開始查,比較麻煩
測試類代碼如下:因為我們在spring的配置中是采用掃描包的方式進行mapper.xml文件是掃描,所以必須要遵循包掃描的幾個規則這里重復一遍:
規則:1.mapper接口和mapper.xml映射文件的名稱必須一致
2.mapper接口和mapper.xml映射文件必須在同一個目錄
public class TestUser { @Test public void findAll() { //獲取spring配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext.xml"); //得到mapper.xml:bean的值就是你類名的小寫,並且要求你dao接口和實現的xml名字必須一樣 UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper"); List<User>list=userMapper.findAll(); for (User user : list) { System.out.println(user); } } }
這里的測試類只要你有查詢出數據的東西,就說明你整合的spring和Mybatis沒有問題,就可以繼續往下面走了。
整合service(讓spring管理sevice的過程):這個也很簡單就是添加配置文件就OK,但是這里有兩種方式:第一種是注釋的事務配置,第二種是非注解的事務配置
事務的配置
通知(哪一些事務需要提交,哪一些還不需要)
Aop
注解的配置(配置文件):
<!-- 4. 事務管理 : DataSourceTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 5. 使用聲明式事務 --> <tx:annotation-driven transaction-manager="txManager" />
service中注解的使用:(在方法上加上@Transactional)
@Transactional//要在你的方法上加上這個注解
public void insertUser(User user) {
userMapper.insertUser(user);
}
非注解的配置:
<!-- 事物的配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 數據源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice transaction-manager="transactionManager" id="txAdvice"> <!-- 傳播行為:什么方法需要用到事物 --> <tx:attributes> <!--<tx:method name="*" rollback-for="RunTimeException"/>--> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- Aop 告訴spring哪里需要用到事物--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.etc.service.impl.*.*(..))"/> </aop:config>
整合springmvc(不需要整合,只要丟配置文件就好了):把springMVC的配置文件放進去就行
前置過濾器
默認的注解支持
視圖解析器
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 默認的注解映射支持 --> <mvc:annotation-driven/> <!-- 啟用自動掃描 --> <context:component-scan base-package="com.etc.*"></context:component-scan> <!-- 配置ViewResolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
在web.xml中加載spring的配置文件和指定springMVC的文件位置(默認是在WEB-INF底下的servlet名字-servlet.xml,現在被我們放到了config底下了)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring 容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring 配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext.xml</param-value> </context-param>
<!--前置過濾器的配置--> <servlet> <servlet-name>dispatch</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 默認的配置文件的地址:/WEB-INF/servlet的名字-servlet.xml <init-param>--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/dispatcher-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <!-- 第一種:*.html,*.action 就是過濾以.xxx結尾的 第二種:/ 所有的地址都會進入這個進行解析,靜態的資源,需要添加配置 第三種:/* 他就是變態,連jsp頁面都進來,結果就是報錯XXXXX別用 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
到此整合結束,下一篇會把增刪改查的demo直接貼代碼給大家
