1. 程式人生 > >spring,springmvc,jpa,mybatis整合配置

spring,springmvc,jpa,mybatis整合配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<servlet>
<servlet-name>gy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gy</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>

..-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<context:component-scan base-package="cn.gson" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />


<!-- 指定靜態資源的路徑對映 -->
<mvc:resources location="/WEB-INF/images/" mapping="/images/**" />


<!-- 檢視解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


<!-- 配置攔截器 -->


<mvc:interceptors> 預設攔截所有請求 <bean class="cn.gson.spring.mvc3.NatureInterceptor" 
/> 可以配置攔截指定請求 <mvc:interceptor> 需要攔截的請求 <mvc:mapping path="/user" /> <mvc:mapping 
path="/user/*" /> 排除的請求 <mvc:exclude-mapping path="/user/add" /> <bean class="cn.gson.spring.mvc3.LoginInterceptor" 
/> </mvc:interceptor> </mvc:interceptors> 


<!-- 整合自定義的轉換器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 
<property name="converters"> <list> 自定義的型別轉換器 <bean class="cn.gson.spring.mvc3.PhoneConverter" 
/> <bean class="cn.gson.spring.mvc3.DateConverter" /> </list> </property> 
</bean> 


<!-- 驗證框架的支援配置 -->


<!-- 指定訊息資原始檔 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:ValidationMessages"></property>
<property name="fileEncodings" value="utf-8"></property>
<property name="cacheSeconds" value="120" />
</bean>


<!-- 驗證框架 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource"></property>
</bean>


<!-- 為mvc註冊型別轉換器和驗證器 -->
<!-- <mvc:annotation-driven validator="validator" conversion-service="conversionService" 
/> -->




<!-- 檔案上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 檔案最大上傳大小100M->104857600 -->
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>


<!-- 繼承fastjson輸出 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 配置Fastjson支援 -->
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>




<!-- 資料來源的配置 -->
<!-- 資料來源公共部分 -->
<!-- 載入配置檔案 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db-resource.properties</value>
</list>
</property>
</bean>


<!-- 建立一個帶連線池的dataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${db.driver.class}" />
<property name="driverUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
<property name="minimumConnectionCount" value="${proxool.minConnCount}" />
<property name="statistics" value="${proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
<property name="trace" value="${proxool.trace}" />
</bean>
</property>
</bean>


<!-- jpa相關的整合 start -->
<!-- 指定jpa的Dao所在根包 -->
<jpa:repositories base-package="cn.gson.gy_crm.model.dao.jpa" />


<!-- Entity管理工廠,指定資料來源 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>


<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 開啟註解事務,指定事務管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- jpa相關的整合 end -->


<!-- MyBatis整合配置 start -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定mybatis的配置檔案的路徑 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:mappers/*.xml"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>


<!-- MyBatis Mapper介面所在包名,Spring會自動查詢其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- MyBatis DAO介面所在包名,basePackage指定的包下面的類會自動查詢 -->
<property name="basePackage" value="cn.gson.gy_crm.model.dao.mappers" />
</bean>


<!-- 配置事務管理器 -->
<bean id="mybatisTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>


<!-- 開啟註解事務,指定事務管理器 -->
<tx:annotation-driven transaction-manager="mybatisTransactionManager" />
<!-- MyBatis整合配置 end -->
</beans>

相關推薦

spring,springmvc,jpa,mybatis整合配置

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns

springspringmvcmybatis整合配置(註解)

最近整合ssm框架費了不少功夫,所以,把詳細配置過程列出來,不足的地方望大俠們指正! 專案目錄: 1.web.xml檔案配置: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="h

SpringMVC Spring MyBatis整合配置文件

isp xmlns manage where spl return 創建 lec common 1、spring管理SqlSessionFactory、mapper 1)在classpath下創建mybatis/sqlMapConfig.xml <?xml vers

Spring+SpringMVC+MyBatis整合配置檔案的搭建

SSM專案的環境搭建: 一、專案的目錄結構如下: 二、匯入的jar包:WEB-INF/lib下 三、resources下的幾個配置檔案+WEB-INF下的web.xml檔案需要自己配置: wei.xml: <?xml version="1.0" en

Spring+SpringMVC+MyBatis深入學習及搭建(十四)——SpringMVCMyBatis整合

文件拷貝 conf lips glib ide doc from ive body 轉載請註明出處:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面講到:Spring+SpringMVC+MyBatis深入學習及搭建(

springspringmvcmybatis整合ssm框架出現ORA-02289:序列不存在問題

sel 請求 開始 color 九九 pri soft 框架 服務 今天整合了一個SSM項目,完了後部署到Tomcat服務器,正常啟動。但是當我發送請求時,報錯,,如下 報錯說序列不存在,可是我明明創建了序列呀,然後我測試了一下,測試語句:select tb_user_s

springspringmvcmybatis整合(java config方式)

ada vat req style face sat roo art 實體   之前項目中使用ssm框架大多是基於xml的方式,spring3.0以後就提供java config的模式來構建項目,並且也推薦使用這種方式,自從接觸過springboot後,深深感受到這種純ja

Spring-mybatis整合配置常用的兩種方式

1.使用mapper介面,定義了mapper介面,在mapper.xml中關聯mapper檔案的。 其中mapper定義了介面,其類名與xml中的namespace一致,id與介面定義的方法名一直,這樣,把xml載入到spring中後,mybatis的初始化配置sqlsessio

SpringSpringMVCMybatis整合之工程的搭建

SSM框架整合之環境配置部分 學習完了Spring、SpringMVC、Mybatis框架,我們就可以嘗試系統將三者進行整合。整合並不複雜,我們只需要實現最基礎的配置,即可輕鬆的掌握SSM框架是如何實際專案中使用的。 基於上一篇博文:maven起步,我們應該知道了如何搭建maven專案,那麼在此

Spring+Spring MVC+Mybatis整合配置AOP不生效的解決方案以及Bean初始化重複載入兩次(疑難雜症)

之前上班做spring+spring mvc +hibernate開發, 2年之久不做想複習一下aop的使用,結果配置遇見aop不生效,解決而記錄! 先上程式碼直接看反例效果會明顯: 首先看一下我的程式碼的包路徑: 接下來看Spring-MVC的配置檔案部分程式碼:

SpringMyBatis整合--配置檔案

在JAVA專案下建立lib資料夾,將jar包放進lib資料夾,進行配置。將MyBatis的配置檔案mybatis-config.xml和Spring的配置檔案app.xml放在src目錄下。 這是mybatis的配置檔案 這是Spring的配置檔

springmvcmybatis整合關鍵配置

springmvc+mybaits的系統架構: 第一步:整合dao層          mybatis和spring整合,通過spring管理mapper介面。          使用mapper的掃描器自動掃描mapper介面在spring中進行註冊。 第二步:整合

整合SSJ(spring+springmvc+jpa)框架時遇到的No serializer問題

   在類上加註解@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"}) 第二種方案:     第一步:建立一個類  &n

springmvc和json整合配置方法

repl bin blank converter htm spring配置 tpm port 三方 配置方法一 1、導入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar。 2、spri

SpringMvcMybatis整合總結

web images bean 技術 數據庫 tro control 自己 alt 1.先配置mybatis,測試讀取數據庫 2.自己測試數據 3.配置spring和springmvc, PS:配置web.xml,這樣就不用getBean了 4.配置Controlle

SpringMVCmybatis整合

工程 信息 一、逆向工程生成基礎信息<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Confi

Spring+SpringMVC+Maven+Mybatis+MySQL項目搭建

image slf4 com autowired trac intern mvc cannot select ---恢復內容開始--- 1. 建表語句及插入數據 CREATE TABLE `book_user` ( user_id INT(11) NOT NU

SpringMvc基礎知識(二) springmvcmybatis整合

internal 前端控制器 客戶 報錯 tca json轉換 註入 配置 iso 1 springmvc和mybatis整合 1.1 需求 使用springmvc和mybatis完成商品列表查詢。 1.2 整合思路 springmvc+mybaits的系統

Mybatis-Generator插件的使用與Spring集成Mybatis配置

Mybatis Mybatis-Generator插件 Spring集成Mybatis 持久層框架 SSM Mybatis-Generator插件 Mybatis-Generator是一個用於自動生成dao層接口、pojo以及mapper xml的一個Mybatis插件,該插件有三種用法:

SSM(MyBatis+Spring+SpringMVC)之MyBatis總結

ringbuf code pad name 傳遞 hashmap tor names jdbc技術 對於SSM(MyBatis+Spring+SpringMVC)之MyBatis總結 對於ORM持久化框架之前一直是用的JDBC去連接數據庫 ,對於JDBC來連接庫來說可能存在