整合项目地址:https://github.com/mybatis/spring
一. 依赖包:
》1.SpringMVC依赖包:
<! - SpringMVC-核心- > <依存> <groupIdを> org.springframework </ groupIdを> <たartifactId>春・ウェブ</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <依存関係> <groupIdを> org.springframework </ groupIdを> <たartifactId>春-webmvc </たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <! - SpringMVC-文件上传- > <依存関係> <groupIdを>コモンズ-ファイルアップロード</ groupIdを> <たartifactId>コモンズ-ファイルアップロード</たartifactId> <バージョン> 1.4 </ version>の </依存関係> <依存> <groupIdを>コモンズ-IO </ groupIdを> <たartifactId>コモンズ-IO </たartifactId> <バージョン> 2.6 </バージョン> </依存関係> <! - SpringMVC-のRestful - > <依存> <groupIdを> com.fasterxml.jackson.core </ groupIdを> <たartifactId>ジャクソンデータバインド</たartifactId> <バージョン> 2.9.9 </バージョン> </依存> <依存性> <のgroupId> com.fasterxml.jackson.core </のgroupId> <たartifactId>ジャクソンコア</たartifactId> <バージョン> 2.9.9 </バージョン> </依存> <依存性> < groupId> com.fasterxml.jackson.core </ groupIdを> <たartifactId>ジャクソンの注釈</たartifactId> <バージョン> 2.9.9 </バージョン> </依存関係> <!-サーブレット、JSP - > <依存性> <groupIdを>のjavax.servlet </のgroupId> <たartifactId>のjavax.servlet-API </たartifactId> <バージョン> 4.0.1 </バージョン> <スコープ>提供</スコープ> </依存> <依存性> <のgroupId> javax.servlet.jsp </のgroupId> <たartifactId> javax.servlet.jsp-API < /たartifactId> <バージョン> 2.3.3 </バージョン> <スコープ>提供</スコープ> </依存>
「2.Springの依存関係:
<! -春IOC - > <依存> <groupIdを> org.springframework </ groupIdを> <たartifactId>春・コンテキスト</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <依存> <のgroupId> org.springframework </のgroupId> <たartifactId>ばねコア</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存> <依存性> <のgroupId> org.springframework </のgroupId > <たartifactId>春豆</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <依存> <groupIdを>org.springframework </のgroupId> <たartifactId>ばねコンテキストサポート</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> <groupIdを> org.springframework </ groupIdを> </dependency> <! -文字列- AOP - > <依存> <groupIdを> org.springframework </ groupIdを> <たartifactId>春-AOP </たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <依存関係> <groupIdを> org.springframework </ groupIdを> <たartifactId>春-側面</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存関係> <! -春- JDBC - > <依存関係> <のgroupId> org.springframework </のgroupId> <たartifactId>ばねJDBC </たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存> <依存> <たartifactId>スプリング-TX </たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存> <依存性> <のgroupId> org.springframework </のgroupId> <たartifactId>ばねORM </たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> </依存> <依存性> <のgroupId>のMySQL </ groupIdを> <たartifactId>のmysql-コネクタ-javaの</たartifactId> <バージョン> 5.1.47 </バージョン> </依存関係> <依存> <groupIdを> com.mchange </ groupIdを> <たartifactId> C3P0 </たartifactId > <バージョン> 0.9.5.4 </バージョン> </依存関係> < - log4jの是具体的日志系统:!通过SLF4J-log4j12初始化Log4jに、达到最终日志的输出- > <依存> <groupId>org.apache.logging.log4j</groupId> <たartifactId> log4jのコア</たartifactId> <バージョン> 2.11.2 </バージョン> </依存> <依存性> <のgroupId> org.slf4j </のgroupId> <たartifactId> SLF4J-API </たartifactId> <バージョン> 1.7.26 </バージョン> </依存> <依存性> <のgroupId> org.slf4j </のgroupId> <たartifactId> SLF4J-log4j12 </たartifactId> <バージョン> 1.7.26 </ version>の <スコープ>テスト</スコープ> </依存関係> <! -测试- > <依存> <groupIdを> org.springframework < /のgroupId> <たartifactId>ばね試験</たartifactId> <バージョン> 5.1.7.RELEASE </バージョン> <スコープ>テスト</スコープ> </依存> <依存性> <のgroupId> JUnitの</のgroupId> <たartifactId> JUnitの</たartifactId> <バージョン> 4.12 </バージョン> <スコープ>テスト</スコープ> </依存関係>
「3.MyBatisの依存関係:
<! - MyBatisの- > <依存> <groupIdを> org.mybatis </ groupIdを> <たartifactId> MyBatisの</たartifactId> <バージョン> 3.5.1 </バージョン> </依存関係> <依存> <groupIdを>組織.mybatis </のgroupId> <たartifactId> MyBatisのスプリング</たartifactId> <バージョン> 2.0.1 </バージョン> </依存>
第二に、設定ファイルは:春、SpringMVC、MyBatisの設定ファイルを作成します。
「1. MyBatisの設定ファイル(MyBatisの-config.xmlの)とマップファイル(マッパー)を作成します。
「2.Springプロフィール:(applicationContext.xmlを)
<?xml version = "1.0"エンコード= "UTF-8"?> <豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス" のxmlns:コンテキスト = "http://www.springframework.org/schema/context" のxmlns:MVN = "http://www.springframework.org/schema/mvc" のxmlns:AOP =" http://www.springframework.org/schema/aop " のxmlns:TX = "http://www.springframework.org/schema/tx"のxmlns:MVC =" http://www.springframework.org/schema/ MVC " のxmlns:MyBatisの -spring = "http://mybatis.org/schema/mybatis-spring" のxsi:schemaLocationの ="のhttp://www.springframework.org/schema/beans ます。http://www.springframework.org/schema/beans/spring-beans.xsd ます。http:// www.springframework.org/schema/context ます。https:// www.springframework.org/schema/context/spring-context.xsd のhttp:/ / www.springframework.org/schema/mvc ます。https:// www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop ます。https://www.springframework。 ORG /スキーマ/ AOP /春-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org /スキーマ/ MyBatisのスプリング http://mybatis.org/schema/mybatis-spring.xsd 「> < -パックスキャン! - > <コンテキスト:スキャン・ベース-コンポーネントパッケージ = "cn.ll.action、cn.ll.dao、cn.ll.service" /> <! -負荷接続情報データベース所有するリソースファイル- > <コンテキスト:不動産-プレースホルダLOCATION = "CLASSPATH:database.properties" /> < -注釈コメントを有効にする- >! <AOP:AspectJの-自動プロキシ/> <! -データベース接続プール- > < ID =ビーン"データソース" クラス = "com.mchange.v2.c3p0.ComboPooledDataSource"> <プロパティ名= "driverClass"値= "$ {datasource.driver}" /> <プロパティ名= "jdbcUrlと"値=「$ datasource.url} { "/> <プロパティ名="ユーザー"値="$ {datasource.username} "/> <プロパティ名="パスワード」値= "$ {datasource.password}" /> <プロパティ名= "maxPoolSize"値 = "$ {datasource.maxPoolSize}" /> <! - 接続の最大数- > <プロパティ名= "initialPoolSize"値= "$ {datasource.initialPoolSize}" /> <! -初期接続- > <プロパティ名= "minPoolSize"値= "$ {datasource.minPoolSize}" /> <! -接続の最小数を維持- > <プロパティ名= "MaxIdleTimeエレメン"値=「$ { datasource.maxIdleTime} "/> <! -最大待機時間(ミリ秒) - > </豆> <! - jdbcTemplateを注入する- > <豆ID =" jdbcTemplate " クラス =" org.springframework.jdbc.core。 JdbcTemplate "> <プロパティ名="データソース"REF ="データソース「/> </豆> <! -トランザクション-トランザクションマネージャの設定- > <豆上記ID = "トランザクションマネージャー" クラス=「org.springframework.jdbc.datasource.DataSourceTransactionManager」> <TX:メソッド名= "ログイン*"伝播= "REQUIRED" /> <property name="dataSource" ref="dataSource"/> </豆> < -総務- AOPの間で運用構成セクションの懸念は、トランザクション管理に影響を取ることを目的とし、この時点で- !> <TX:上記のid =「txAdvice」トランザクション・マネージャー=「のTransactionManager」をアドバイス> <TX :!属性> < -運用サービス定義の方法を一致させる- > <TX:メソッド名= "追加*"伝播= "REQUIRED" /> <TX:メソッド名= "=伝播を"*作成" REQUIRED "/> <TX:メソッド名="削除* "伝播=" REQUIRED "/> <TX:メソッド名=" * "伝播=" REQUIRED削除"/> <TX:メソッド名="編集*「の伝播を= "REQUIRED" /> <TX:メソッド名= "*更新"伝播= "REQUIRED"/> <TX:メソッド名= "リスト*"伝播= "REQUIRED"読み取り専用= "真" /> <TX:メソッド名は= "* GET"伝播= "REQUIRED"読み取り専用= "trueに" /> </ TX:属性> </ TX:アドバイス> < -ビジネス-良いビジネス上の問題を設定した後、ここでは、ビジネス層のマッチングを見つける、クロス式のトランザクションの設定を検討する必要がある- >! <AOP:config>の < AOP:ポイントカットID = "servicePointcut" =表現"実行(cn.ll.service * * *(..)。)" /> <AOP:アドバイス-REF =顧問"txAdvice"ポイントカット-REF = "servicePointcut" / > <! -トランザクション制御エントリ・ポイント- > </ AOP:config>の <! - 統合MyBatisの 1 。 春はすべてのコンポーネントを管理し、マッパーの実装クラス;ときに、サービスコールダオ、@Autowired達成:自動インジェクター2 春管理慣行によってを。 - > <豆ID =「SqlSessionFactoryBean」クラス="org.mybatis.spring.SqlSessionFactoryBean" > <! -データソース- > <プロパティ名= "データソース" REF = "データソース" /> < - -すべてのMyBatisの指定されたプロファイル!> <プロパティ名= "configLocation"値=「CLASSPATH:MyBatisの-config.xml "/> <! -指定されたMapper.xmlファイルの場所- > <プロパティ名=" mapperLocations "値=" CLASSPATH :.マッパー/ * XMLが「/> </ beanが> < -すべてをスキャン!マッパーインターフェイスマッパーこれらを自動的に注入することができるように、ベース- パッケージは:パッケージ名マッパーインタフェースを指定します- > <MyBatisの-春:ベース-スキャンしたパッケージ =「cn.ll.dao」/> </豆を>
「3.SpringMVCプロファイル:(春-mvc.xml)
<?xml version = "1.0"エンコード= "UTF-8"?> <豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス" のxmlns:コンテキスト = "http://www.springframework.org/schema/context" のxmlns:MVN = "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 ます。https:// WWW .springframework.org /スキーマ/コンテキスト/バネ文脈。XSD のhttp://www.springframework.org/schema/mvc HTTPS:// www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -パックスキャン- > <コンテキスト:スキャン-ベース-コンポーネントパッケージ =" cn.ll.action「/> < -制御層注釈を有効にする- >! <MVN:注釈ドリブン/> < -ディスパッチャを使用して処理要求- >! <MVN:デフォルト -servlet-ハンドラ/> <! -定義ページ解析処理リソースクラス、接尾辞マッチングパスのプレフィックス- > <ビーンクラス =「org.springframework.web.servlet.view.InternalResourceViewResolver」> <プロパティ名=「接頭辞」値=「/ WEB-INF /ページ/ "/> <プロパティ名="サフィックス"値=。"JSP」/> </豆> <!-- 为保存在WEB-INF静态资源设置映射访问路径--> <MVN:リソースマッピング= "/ JS / **"位置= "/ WEB-INF / JS /" /> <MVN:リソースマッピング= "/ CSS / **"位置= "/ WEB-INF / CSS /" /> <MVN:リソースマッピング= "/画像/ **"の場所= "/ WEB-INF /画像/" /> <! -进行要加载的"* .propertiesファイル"配置文件路径加载、直接CLASSPATH通过加载- > <ビーンクラス = "org.springframework.context.support.ResourceBundleMessageSource"> <プロパティ名= "ベース名"> <配列> <値> cn.ll.redis.message.pages </値> <値> CN .ll.redis.message.message </ value>の </アレイ> </プロパティ> </豆> <! -解析処理ベースのコンフィギュレーションファイルのアップロード- > <豆のクラス="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置整体上传文件的最大数据量,本次允许上传最大文件量为5M --> <property name="maxUploadSize" value="5242880"/> <!-- 设置单个上传文件的最大数据量,本次允许上传最大文件量为2M --> <property name="maxUploadSizePerFile" value="253952"/> <!-- 允许占用的最大内存量,本次设置为10M --> <property name="maxInMemorySize" value="10485760"/> <!-- 设置上传文件的临时保存目录,该目录的内容在每一次请求之后都需要清空 --> <property name="uploadTempDir" value="/tmp"/> </bean> <!-- 配置全局跳转页面 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <property name="exceptionMappings"><!-- 异常映射 --> <props> <支えるキー= "org.springframework.web.multipart.MaxUploadSizeExceededException">プラグイン/エラー</小道具> </小道具> </ property>の </豆> </豆>
web.xmlの「4.スタート春とSpringMVC:
<xmlのバージョン= "1.0"エンコード= "UTF-8"?> の<web-appのxmlns = "http://xmlns.jcp.org/xml/ns/javaee" のxmlns:XSI = "のhttp:// WWW .w3.org / 2001 / XMLスキーマ・インスタンス" のxsi:schemaLocationの =" http://xmlns.jcp.org/xml/ns/javaee のhttp:// xmlns.jcp.org/xml/ns/javaee/web-app_4_0 .xsd」 バージョン= "4.0"> < - WEB容器启动的时候实现春容器的启动- >! <コンテキスト-param>の の<param-name>のcontextConfigLocation </ PARAM名> の<param-value>のクラスパス:スプリング/ application.xmlの</ PARAM値> </コンテキスト-param>の <リスナー> <listener- クラス > org.springframework.web.context.ContextLoaderListener </ listener-クラス > </リスナー> <! - SpringMVC的启动- > の<servlet> の<servlet-name>ののDispatcherServlet </サーブレット名> <servlet- クラス > org.springframework.web.servlet.DispatcherServlet </ servlet- クラス > の<init-param>の の<param-name>のcontextConfigLocation </ PARAM名> の<param-value>のクラスパス:春/春-mvc.xml </ PARAM値> </ initの-param>の <のload-on-startup > 1 </のload-on-startup> </サーブレット> <のservlet-mapping> の<servlet-name>ののDispatcherServlet </サーブレット名> <URLパターン> / </ URLパターン> </servlet-mapping> <! -エンコーディングフィルタ- > <フィルター> <フィルタ名> characterEncodingFilter </フィルタ名> <フィルター- クラス > org.springframework.web.filter.CharacterEncodingFilter </フィルター- クラス > の<init-param>要素 の<param-name>のエンコーディング</ param- >名前 の<param-value>のUTF-8 </ PARAM値> </ initの-param>の の<init-param>の の<param-name>のforceEncoding </ PARAM名> の<param-value>の真 </ PARAM値> </ INIT-param>の </フィルタ> <フィルタマッピング> <フィルタ名>characterEncodingFilter </フィルタ名> の<url-pattern> / * </のurl-pattern> </のfilter-mapping> </ web-app>の