1. 程式人生 > >spring+springmvc+mybatis整合

spring+springmvc+mybatis整合

聲明 參數 stdout 加載 strong 相關 version pts check

搭建ssm框架,我們要分幾步進行,把每個配置文件分開寫,這樣看上去一目了然,有利於後期的修改維護,對自己也可以記請每一步的內容和步驟,方便記憶

一.spring-dao.xml

二.jdbc. properties

三.mybatis-config.xml

四.spring-service.xml

五.spring-web.xml

六. web.xml

七.logback.xml

詳細配置如下:

第一步:先在spring文件夾裏新建spring-dao.xml文件,因為spring的配置太多了,這裏我們分三層 dao service web 1.讀入數據庫連接的相關參數 2.配置數據連接池 3.配置連接屬性, 4.配置c3p0,只配了幾個常用的 5.配置SqlSessionFactory對象(mybatis) 6.掃描dao層接口,動態實現dao接口 spring-dao.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4         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.xsd"
> 5 <!-- 配置整合mybatis過程 --> 6 <!-- 1.配置數據庫相關參數properties的屬性:${url} --> 7 <context:property-placeholder location="classpath:jdbc.properties" /> 8 9 <!-- 2.數據庫連接池 --> 10 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
> 11 <!-- 配置連接池屬性 --> 12 <property name="driverClass" value="${jdbc.driver}" /> 13 <property name="jdbcUrl" value="${jdbc.url}" /> 14 <property name="user" value="${jdbc.username}" /> 15 <property name="password" value="${jdbc.password}" /> 16 17 <!-- c3p0連接池的私有屬性 --> 18 <property name="maxPoolSize" value="30" /> 19 <property name="minPoolSize" value="10" /> 20 <!-- 關閉連接後不自動commit --> 21 <property name="autoCommitOnClose" value="false" /> 22 <!-- 獲取連接超時時間 --> 23 <property name="checkoutTimeout" value="10000" /> 24 <!-- 當獲取連接失敗重試次數 --> 25 <property name="acquireRetryAttempts" value="2" /> 26 </bean> 27 28 <!-- 3.配置SqlSessionFactory對象 --> 29 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 30 <!-- 註入數據庫連接池 --> 31 <property name="dataSource" ref="dataSource" /> 32 <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> 33 <property name="configLocation" value="classpath:mybatis-config.xml" /> 34 <!-- 掃描entity包 使用別名 --> 35 <property name="typeAliasesPackage" value="com.soecode.lyf.entity" /> 36 <!-- 掃描sql配置文件:mapper需要的xml文件 --> 37 <property name="mapperLocations" value="classpath:mapper/*.xml" /> 38 </bean> 39 40 <!-- 4.配置掃描Dao接口包,動態實現Dao接口,註入到spring容器中 --> 41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 42 <!-- 註入sqlSessionFactory --> 43 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 44 <!-- 給出需要掃描Dao接口包 --> 45 <property name="basePackage" value="com.soecode.lyf.dao" /> 46 </bean> 47 </beans>



jdbc. properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=



因為這裏用到mybatis,所以需要mybatis核心文件,在recourse文件夾裏新建mybatis-configx.xml 1.使用自增主鍵 2.使用列別名 3.開啟駝峰命名轉換create_time ->createTime mybatis-config.xml
<?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>
        <!-- 配置全局屬性 -->
        <settings>
                <!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 -->
                <setting name="useGeneratedKeys" value="true" />

                <!-- 使用列別名替換列名 默認:true -->
                <setting name="useColumnLabel" value="true" />

                <!-- 開啟駝峰命名轉換:Table{create_time} -> Entity{createTime} -->
                <setting name="mapUnderscoreToCamelCase" value="true" />
        </settings>
</configuration>

第二步:剛弄好到層,接下來service層。在spring文件夾創建spring-service.xml 1.掃描service包所有的@Service註解 2.配置事務管理器,把事物交有spring來完成 3.配置基於註解的聲明事物,可以直接在方法上@Transaction spring-service.xml
<?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:tx="http://www.springframework.org/schema/tx"
        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.xsd      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 掃描service包下所有使用註解的類型 -->
        <context:component-scan base-package="com.soecode.lyf.service" />

        <!-- 配置事務管理器 -->
        <bean id="transactionManager"
                class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <!-- 註入數據庫連接池 -->
                <property name="dataSource" ref="dataSource" />
        </bean>

        <!-- 配置基於註解的聲明式事務 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
</beans>



第三步:配置web,在spring文件夾裏新建spring-web.xml 1.開啟SpringMVC註解模式,可以使用@RequestMapping,@PathBariable,@ResponseBady等 2.對靜態資源的處理 ,如js,css,jsp等 3.配置警示牌、顯示ViewResolver,例如controller中某個方法返回一個string類型的“login”,實際上回返回“WEB-INF/login.jsp” 4.掃描web層的@Controller註解 spring-web.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
        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.xsd      http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        <!-- 配置SpringMVC -->
        <!-- 1.開啟SpringMVC註解模式 -->
        <!-- 簡化配置:           (1)自動註冊DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter           (2)提供一些列:數據綁定,數字和日期的format @NumberFormat, @DateTimeFormat, xml,json默認讀寫支持   -->
        <mvc:annotation-driven />
       
        <!-- 2.靜態資源默認servlet配置          (1)加入對靜態資源的處理:js,gif,png          (2)允許使用"/"做整體映射  -->
        <mvc:default-servlet-handler/>
       
        <!-- 3.配置jsp 顯示ViewResolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
        </bean>
       
        <!-- 4.掃描web相關的bean -->
        <context:component-scan base-package="com.soecode.lyf.web" />
</beans>

第四步:最後就是修改web.xml文件 web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1" metadata-complete="true">
        <!-- 如果是用mvn命令生成的xml,需要修改servlet版本為3.1 -->
        <!-- 配置DispatcherServlet -->
        <servlet>
                <servlet-name>seckill-dispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <!-- 配置springMVC需要加載的配置文件                  spring-dao.xml,spring-service.xml,spring-web.xml                  Mybatis - > spring -> springmvc          -->
                <init-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value>classpath:spring/spring-*.xml</param-value>
                </init-param>
        </servlet>
        <servlet-mapping>
                <servlet-name>seckill-dispatcher</servlet-name>
                <!-- 默認匹配所有的請求 -->
                <url-pattern>/</url-pattern>
        </servlet-mapping>
</web-app>

我們在項目中經常會使用到日誌,若以這裏還有配置日誌xml,在resources文件夾裏新建logback.xml logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
                <encoder>
                        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
                </encoder>
        </appender>

        <root level="debug">
                <appender-ref ref="STDOUT" />
        </root>
</configuration>



到目前為止,我們一共寫了7個配置文件 技術分享

技術分享

spring+springmvc+mybatis整合