Spring、SpringMVC與MyBatis整合(SSM整合)

整合思路

Spring與SpringMVC之間是不需要整合的。

springmvc+mybaits的系統架構:



 

第一步:整合dao層

mybatis和spring整合,通過spring管理mapper接口。

使用mapper的掃描器自動掃描mapper接口在spring中進行註冊。

 

第二步:整合service層

通過spring管理 service接口。

使用配置方式將service接口配置在spring配置文件中。

實現事務控制。

 

第三步:整合springmvc

由於springmvc是spring的模塊,不需要整合。


準備環境

數據庫:MySql 5.5

Jdk:1.8

eclipse Neon

Springmvc版本:spring3.2

 (以上只是我的環境,版本有小的出入也無妨)

所需要的jar包:

數據庫驅動包:mysql5.1

mybatisjar

mybatisspring整合包

log4j

dbcp數據庫連接池包

spring3.2所有jar

jstl


項目結構:


db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root

log4j.properties

# Global logging configuration\uFF0C\u5EFA\u8BAE\u5F00\u53D1\u73AF\u5883\u4E2D\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

整合Dao

mybatisspring進行整合。


sqlMapConfig.xml

classpath下創建mybatis/sqlMapConfig.xml

<span style="font-family:SimSun;font-size:14px;color:#000000;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <!-- 全局的setting,根據需要添加 -->

	<!-- 配置別名 -->
	<typeAliases>
		<!-- 批量掃描 -->
		<package name="cn.ssm.po" />
	</typeAliases>
<!-- 使用自動掃描器時,mapper.xml文件如果和mapper.java接口在一個目錄則此處不用定義mappers -->
<mappers>
<package name="cn.ssm.mapper" />
</mappers>
</configuration></span>



2.applicationContext-dao.xml

配置數據源、事務管理,配置SqlSessionFactory、mapper掃描器。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.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 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-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 name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="maxActive" value="30"/>
		<property name="maxIdle" value="5"/>
</bean>	


<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 數據庫連接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加載mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
<!-- mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="cn.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>

</beans>


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