前の記事で3つの方法を要約しましたが、SQLに注釈を付ける2つの方法があります。この方法は非常に混乱しているため、誰もがあまり使用していません。一般的に使用される2つの方法を要約しましょう。
1。DAOを書くことなく、動的プロキシ実装クラス
この方法は比較的簡単です。 DAOレイヤーを実装する必要はありません。インターフェイスを定義するだけです。ここでは、構成ファイルを記録するためだけのため、プログラムは非常に簡単です。
1。全体的な構造図:
2。3つの構成ファイルと1つのマッピングファイル
(1)プログラムの入り口とフロントエンドコントローラー構成Web.xml
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/nns/javaee xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ns/javaee/web-app_0.0.0.xsd" "id" "id" id "id" webaee/wweb-app_0.xsdd <display-name> website1 </display-name> <! - リスニングをセットアップして、Webコンテナが起動したときにアプリケーションの構成情報を自動的に組み立てます - > <リスナー> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </< <param-name> contextconfiglocation </param-name> <param-value> classpath:config/springmvc-servlet.xml、classpath:config/applicationcontext.xml </param-value> </context-param> <! <filter-class> org.springframework.web.filter.characterencodingfilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> utf-8 </param-value> </init-param> <init-name> farm-name> form-name> form-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingfilter </filter-name> <url-pattern>*。do </url-pattern>*。 <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value> classpath:config/springmvc-servlet.xml </param- <load-on-startup> 1 </load-on-startup> </servlet mapping> <servlet-mapping> <servlet-name> springmvc </servlet-name> <! - Intercept request-> <url-pattern>*。 <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> default.jsp </welcome-file> </welcome-file-list> </web-app>
(2)スキャン制御層、自動インジェクション、ビューパーサー構成SpringMVC-Servlet.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:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:util = "http://www.springframework.org/schema/util" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:jdbc = "http://www.springframework.org/schema/jdbc" " xmlns:cache = "http://www.springframework.org/schema/cache" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/spring/spring/spring/spring/d.d.chema/ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.sspringframework.org/schema/spring-f.ling-f. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cach http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <! - annotation driver-> <MVC:annotation-driven/> <! - <context:annotation-config/> - > <!-コンテキスト:コンポーネント - > <! <コンテキスト:Component-Scanベースパッケージ= "com.website.controller"> </context:component-scan> <! - view parser-> <bean id = "viewresolver"> <property name = "prepertix" value = "/web-inf/"> </property name> <property name = "suffix"。
(3)データソース、サービス自動スキャンとインジェクション、Spring Managed MyBatissQlSessionFactory、DAOレイヤーインターフェイスダイナミックプロキシ、およびトランザクション構成ApplicationContext.xml
ここには複数の構成ファイルがあります
1)単一のデータソース、動的プロキシは、DAOレイヤーインターフェイスを実装する際に、SQLSessionFactoryBeanNameまたはSQLSessionTemplateBeanNameの2つのプロパティの値を設定しません。
<?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:tx = "http://www.springframework.org/schema/tx" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-/spring-context-/spring-contex http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! - ロード構成JDBCファイル - > id = "dataSource"> <プロパティ名= "driverclassname"> <value> $ {jdbc.driverclassname} </value> </property> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name "> <value> </</</</</</</</</</</</</</propetienm name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <! - アノテーション構成をオンにします。 base-package = "com.website.service" /> <! - mybatisを使用する場合、springはsqlsessionfactorybeanを使用してmybatisのsqlsessionfactoryを管理します - > <bean id = "sqlsessionfactory" /> <! - エンティティクラスマップファイルパス、そのうちの1つのみが最後まで書き込まれ、複数のものをmybatisに置き換えることができます/*。xml-> <プロパティ名= "mapperlocations" value = "classpath:mybatis/usermapper.xml"/> </bean> <! - <bean id = "sqlsession" < bean = "sqlsessionfactory"/> </constructor-arg> </bean> - > <! - ダイナミックプロキシ実装はDAOを書く必要はありません - > <bean id = "mapperscannerconfigurer"> <! - basepackage dao layer interface powerは指定されています。 value = "com.website.dao" /> <! - データソースが1つしかない場合、指定できませんが、複数のデータソースがある場合は、指定する必要があります - > <! - <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory" />> - > <! name = "sqlSessionTemplateBeanName" value = "sqlsession" /> - > < /bean> <! - トランザクションマネージャー - > <bean id = "transactionmanager"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean>: /> < /beans>2)単一のデータソースに対してsqlsessionfactorybeannameのプロパティ値を構成する
<?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:tx = "http://www.springframework.org/schema/tx" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-/spring-context-/spring-contex http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! - ロード構成JDBCファイル - > id = "dataSource"> <プロパティ名= "driverclassname"> <value> $ {jdbc.driverclassname} </value> </property> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name "> <value> </</</</</</</</</</</</</</propetienm name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <! - アノテーション構成をオンにします。 base-package = "com.website.service" /> <! - mybatisを使用する場合、springはsqlsessionfactorybeanを使用してmybatisのsqlsessionfactoryを管理します - > <bean id = "sqlsessionfactory" /> <! - エンティティクラスマップファイルパス、そのうちの1つのみが最後まで書き込まれ、複数のものをmybatisに置き換えることができます/*。xml-> <プロパティ名= "mapperlocations" value = "classpath:mybatis/usermapper.xml"/> </bean> <! - <bean id = "sqlsession" < bean = "sqlsessionfactory"/> </constructor-arg> </bean> - > <! - ダイナミックプロキシ実装はDAOを書く必要はありません - > <bean id = "mapperscannerconfigurer"> <! - basepackage dao layer interface powerは指定されています。 value = "com.website.dao" /> <! - データソースが1つしかない場合は指定できませんが、複数のデータソースがある場合は、指定する必要があります - > <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory" /> <! name = "sqlSessionTemplateBeanName" value = "sqlsession" /> - > < /bean> <! - トランザクションマネージャー - > <bean id = "transactionmanager"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean>: /> < /beans>3)単一のデータソースのsqlsessionTemplateBeanNameプロパティ値を構成する
<?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:tx = "http://www.springframework.org/schema/tx" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-/spring-context-/spring-contex http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! - ロード構成JDBCファイル - > id = "dataSource"> <プロパティ名= "driverclassname"> <value> $ {jdbc.driverclassname} </value> </property> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name "> <value> </</</</</</</</</</</</</</propetienm name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <! - アノテーション構成をオンにします。 base-package = "com.website.service" /> <! - mybatisを使用する場合、springはsqlsessionfactorybeanを使用してmybatisのsqlsessionfactoryを管理します - > <bean id = "sqlsessionfactory" /> <! - エンティティクラスマップファイルパス、そのうちの1つのみが最後まで書き込まれ、複数のものはmybatis/*。xml-> <プロパティ名= "balue =" classpath:classpath:classpath:clasermapper.xml "/> </bean> <bean id =" sqlsession " bean = "sqlsessionfactory"/> </constructor-arg> </bean> <! - 動的プロキシの実装ではdao-> <bean id = "mapperscannerconfigurer"> <! - ここでDAOレイヤーインターフェイス電力が指定されており、ここではDAOインターフェースは、財産= <> <> < value = "com.website.dao" /> <! - データソースが1つしかない場合は指定できませんが、複数のデータソースがある場合は、指定する必要があります - > <! - <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory" />> - > <! name = "sqlsessiontemplatebeanname" value = "sqlsession" /> < /bean> <! - トランザクションマネージャー - > <bean id = "transactionmanager"> <プロパティ名= "dataSource" ref = "datasource" /> < /bean> <! - 完全な解決トランザクションを使用 - </豆>4)複数のデータソース
マルチデータソースの場合は、SQLSessionFactoryBeanNameまたはSQLSessionTemplateBeanNameを使用して、特定のデータソースを指定する必要があることに注意してください。上記の構成で気づいたかどうかはわかりません。 sqlsessionTemplateBeanNameを使用する場合は、必要です
<bean id = "sqlsession"> <constructor-arg index = "0"> <ref bean = "sqlsessionfactory"/> </constructor-arg> </bean>
特定のインスタンスを作成し、sqlSessionTemplateBeanNameプロパティに割り当てます。
(4)、MyBatis SQLマッピングファイルUSERMAPPER.XML:
<?xml version = "1.0" encoding = "utf-8"?> <!doctype mapper public " - // mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"このデモでは、名前空間はuserdao.javaの完全なパスです - > <mapper namespace = "com.website.dao.userdao"> <! - IDはインターフェイスのメソッドの名前です - > <inserce id = "saveuser" parametertype = "java.util.utid" </insert> </mapper>
わかりました、構成ファイルに移動して、それを完了しましょう。コントロールレイヤー、ビジネスロジックレイヤー、DAOレイヤーコードを見てみましょう。
3。コントローラーレイヤー
パッケージcom.website.controller; java.util.hashmapをインポートします。 java.util.mapをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.controllerをインポートします。 org.springframework.web.bind.annotation.requestmappingをインポートします。 org.springframework.web.bind.annotation.RequestMethodをインポートします。 com.website.service.userserviceをインポートします。 @Controller @RequestMapping(value = "/user")public class usercontroller {// inject userservice object @autowired private userservice userservice; @RequestMapping(value = "/save.do"、method = requestmethod.get)public string saveuser(httpservletrequest request、httpservletResponse応答){string id = request.getParameter( "id");文字列名= request.getParameter( "name"); map <string、string> map = new hashmap <string、string>(); map.put( "id"、id); map.put( "name"、name); userservice.saveuser(map); 「インデックス」を返します。 }}4。サービスレイヤー
パッケージcom.website.service; java.util.mapをインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.serviceをインポートします。 Import org.springframework.transaction.annotation.transactional; com.website.dao.userdaoをインポートします。 @service( "userservice")@transactional public class userservice {//インジェクションDAOインターフェイス実装クラスインスタンス//両方のインジェクションメソッドは@autowired private userdao userdaoになります。 public void saveuser(map <string、string> map){int end = userdao.saveuser(map); system.out.println( "end:" + end); }}5。DAOレイヤーインターフェイス
パッケージcom.website.dao; java.util.mapをインポートします。 //com.website.dao.userdao public interface userdao {int saveuser(map <string、string> map); } DAOインターフェイスの完全なパスは、このDAOインターフェイスに対応するマッピングファイルの名前空間であり、メソッド名はIDの値です
OK、この構成方法は終了し、完全な小さなデモもあります。以下に簡単に要約しましょう。
以前の構成方法(以下に記述される)と比較して、DAOレイヤーインターフェイスの動的プロキシメソッドを使用していることです。以前は、DAOレイヤーにDAOレイヤーを手動で実装し、SQLSessionTemplateインスタンスを自動的に挿入して、挿入( "" ""、 "")selectone( "" ""、 ")などの特定のメソッドを呼び出します。最初のパラメーターはマッピングファイルのアドレス:namespace+id、2番目のパラメーターは渡された条件です。このようにして、MyBatisは、解析とクエリのために渡した2つのパラメーターに従って特定のマッピングファイルを見つけます。ここで動的プロキシを使用すると、DAOインターフェイスを実装するステップが排除されますが、Springまでに実装しました。問題があります。クエリ条件パラメーターを渡しましたが、マッピングファイルの特定のパスは次のとおりです。NAMESPCE+IDです。通過しない場合はどうすればよいですか?つまり、マッピングファイルの名前空間はインターフェイスクラスのフルネームでなければならず、IDはインターフェイスのメソッド名でなければなりません。そうすれば、ダイナミックプロキシがパスを見つけてパラメーターを持つことができます。同じだと思いますか?ハハハ!
2。DAOレイヤーインターフェイスを手動で実装します
最初に、DAOレイヤーを手動で実装するための構成とコードを見てみましょう。
1。トピックの構造図
2。3つの構成ファイルとマッピングファイル
(1)プログラムエントリ、フロントエンドコントローラー構成Web.xml
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/nns/javaee xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ns/javaee/web-app_0.0.0.xsd" "id" "id" id "id" webaee/wweb-app_0.xsdd <display-name> website2 </display-name> <! - スプリングコンテナ構成 - > <リスナー> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </ristener> </< classPath:config/springmvc-servlet.xml、classpath:config/applicationcontext.xml </param-value> </context-param> <! - 文字エンコードフィルター - > <filter> <filter> <filter> <filter-name> encodingfilter </filter-name> <filter-class> org.springframework.fil. <param-name> encoding </param-name> <param-value> utf-8 </param-value> </init-param> <init-param> <init-balam> form-value> true </param-value> </init-param> </filter> <フィルターマッピング> <フィルターマッピング> <フィルターマッピング> <フィルターマッピング> <url-pattern>*。do</url-pattern> </filter-mapping> <! - フロントエンドコントローラー - > <servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servet-name <param-value> classpath:config/springmvc-servlet.xml </param-value> </init-param> <! - この構成ファイルは、コンテナが起動するときにロードされます - > <load-on-startup> 1 </load-on-startup> </servet <url-pattern>*。</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> index.jsp </welcome-file> <welcome-file> <welcome-file> <welcome-file> default.jsp </welcome-file> </welcome-file-list> </web-app>
(2)スキャン制御層、自動インジェクション、ビューパーサー構成SpringMVC-Servlet.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:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:util = "http://www.springframework.org/schema/util" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:jdbc = "http://www.springframework.org/schema/jdbc" " xmlns:cache = "http://www.springframework.org/schema/cache" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/spring/spring/spring/spring/d.d.chema/ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.sspringframework.org/schema/spring-f.ling-f. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cach http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <! - annotation driver-> <MVC:annotation-driven/> <! - <Context:annotation-config/> - > < base-package = "com.website.controller"> </context:component-scan> <! - view parser-> <bean id = "ViewResolver"> <Property name = "prefix" value = "/web-inf/view/"> </property name> <property name = "couphix" value = "。JSP>
(3)データソース、サービス自動スキャンとインジェクション、Spring Managed MyBatissQlSessionFactoryおよびTransaction Configuration ApplicationContext.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:tx = "http://www.springframework.org/schema/tx" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-/spring-context-/spring-contex http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! - ロード構成JDBCファイル - > id = "dataSource"> <プロパティ名= "driverclassname"> <value> $ {jdbc.driverclassname} </value> </property> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name "> <value> </</</</</</</</</</</</</</propetienm name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <! - ターンオンアノテーション構成、つまり自動化されます - > <! - コンポーネント-scanは注釈configの関数を持っています。使用する前にBeanをスキャン - > <コンテキスト:Component-ScanSycanベースパッケージ= "com.website.service、com.website.dao" /> <! - springはsqlsessionfactorybeanを使用してmybatisのsqlsessionfactoryを管理します。いくつかの方法を提供します - > <bean id = "sqlsessionfactory"> <プロパティ名= "datasource" ref = "dataSource" /> <! - mybatis configuration file path-> <プロパティ名= "configlocation" value = "" /> <! - エンティティクラスマップファイルパス、開発中の複数のマップファイルがあります。 value = "classpath:mybatis/usermapping.xml"/> </bean> <! - 実際、このクラスの例はmybatisのsqlsessionです - > <bean id = "sqlsession"> <constructor-arg index = "0" "> <ref bean =" sqlsessionfactory "/> < <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <! - 完全なアノテーショントランザクションを使用 - > <tx:annotation-driven transaction-manager = "transactionManager" /> < /beans>(4)MyBatisマッピングファイル
<?xml version = "1.0" encoding = "utf-8"?> <!doctype mapper public " - // mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> DAOレイヤーに完全なパスを書いた後、MyBatisはこのファイルの関連するSQLステートメントのマッピングです - > <Mapper NameSpace = "com.website.usermapper"> <! - パラメータ型はあなたが受け入れるパラメーターのタイプです - > <! - ユーザー情報の追加 - > user(id、name、password)values(#{id}、#{name}、#{password})</insert> </mapper>ここのマッピングファイルの名前空間 +IDは、DAOレイヤーインターフェイスの完全なクラス名 +IDの代わりにカスタマイズされていることがわかります。
3。コントローラー
パッケージcom.website.controller; java.util.hashmapをインポートします。 java.util.mapをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.controllerをインポートします。 org.springframework.web.bind.annotation.requestmappingをインポートします。 com.website.service.userserviceをインポートします。 /*** @author whd data 2016年6月5日*/@controller @requestmapping(value = "/user")public class usercontroller {@autowired private userservice userservice; @RequestMapping(value = "/save.do")public string saveuser(httpservletrequest request、httpservletresponse応答){string id = request.getParameter( "id");文字列名= request.getParameter( "name");文字列パスワード= request.getParameter( "password"); map <string、string> map = new hashmap <string、string>(); map.put( "id"、id); map.put( "name"、name); map.put( "パスワード"、パスワード); userservice.saveuser(map); 「インデックス」を返します。 }}4。ビジネスロジックレイヤーサービス
パッケージcom.website.service; java.util.mapをインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.serviceをインポートします。 Import org.springframework.transaction.annotation.transactional; com.website.dao.userdaoをインポートします。 / *** @author whd data 2016年6月5日*/ @service( "userservice")@transactional public class userservice {@autowired private userdao userdao; public void saveuser(map <string、string> map){userdao.saveuser(map); }}5。DAOレイヤー
パッケージcom.website.dao; java.util.mapをインポートします。 import org.mybatis.spring.sqlsessionTemplate; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.stereotype.repository; / *** @author whd data 2016年6月5日*/ @repository( "userdao")public class userdao {@autowired private sqlsessiontemplate sqlsession; public void saveuser(map <string、string> map){int end = sqlsession.insert( "com.website.usermapper.insertuser"、map); System.out.println( "end" + end); }}逆DAO層のSQLSessionTemplateを見ます。これは、実際にはMyBatisのSQLSessionオブジェクトです。 ApplicationContext.xmlで構成されているため、使用するとSpringが自動的に挿入されます。自分で作成せずに直接使用できます。これはいわゆる制御反転です。 OKこれら2つのファイルの構成方法は終了しました。
要約します
上記は、あなたに紹介された春とmybatisの構成方法の詳細な説明です(一般的に使用される2つの方法)。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!