Spring3との統合
基本的なフレームワークとして、SpringはHibernate、MyBatisなどのバックエンドフレームワークを統合できます。
前の記事では、MyBatisの使用だけを紹介します。一般的な論理は次のとおりです。
sqlsessionFactory < - 構成ファイル(データベース接続構成を含む)
ixxxmapper <-sqlsession <-sqlsessionFactory
<-mapperインターフェイス<-mapper xml
ixxmapperを取得した後、データインタラクションの方法を呼び出すことができます。
春と統合する場合、上記のオブジェクトは豆として管理する必要があります。
DataSource Bean < - データベース接続構成
sqlSessionFactory Bean <-DataSource
< - 構成ファイル
usermapper bean <-sqlsessionfactory
< - マッパーインターフェイス
1. pom.xmlに依存関係を追加:
<properties> <mybatis.spring.version> 1.2.1 </mybatis.spring.version> <dbcp.version> 1.4 </dbcp.version> <spring.version> 3.1.2.release </spring.version> </properties> <sprups <groupid> org.mybatis </groupid> <artifactid> mybatis-spring </artifactid> <version> $ {mybatis.spring.version} </version> </dependency> <dependency> <! - springコンテキストはspring-jdbcを使用してデータベースに接続するためにspring-jdbc> <artifactid> spring-jdbc </artifactid> <version> $ {spring.version} </version> </dependency> <dependency> <! - datasourceはbasicdatasource-> <groupid> commons-dbcp <> <groupid> </Dependency> <Dependency> <GroupId> org.springFramework </groupId> <artifactid> spring-test </artifactid> <version> $ {spring.version} </version> </dependency> </dependencies> 2。ClassPathの下にBeans-Da.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" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "datasource"> < value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc:mysql:// localhost:3306 /hbatis?charatereCoding = utf8" /> <プロパティ名= "ユーザー名"値= "root" /> <プロパティ= "パスワードid = "sqlsessionfactory"> <! - sqlsessionfactory bean - > <プロパティ名= "dataSource" ref = "dataSource" /> <! - > <プロパティ名= "configlocation" value = "classpath:configuration.xml" /> <! - 構成ファイル - > name = "sqlsessionfactory" ref = "sqlsessionfactory" /> <プロパティname = "mapperinterface" value = "com.john.hbatis.mapper.iusermapper" /> <! - マッピングインターフェイス - > < /beans>
3。テストクラス:
@contextConfiguration(locations = {"classpath:beans-da.xml"})public class springintegrationtest extends abstracttestngspringcontexttests {private static final logger log = loggerfactory.getlogger(springintegrationtest.class); @resource iusermapper mapper; @test public void querytest(){user user = mappper.getuserbyid(1); log.info( "name:{}、address:{}"、user.getName()、user.getAddress()); }} SpringMVCとの統合
ここでは、spring3との統合に基づいて構築します。
1。POM.xmlにSpringMVCとFreeMarkerの依存関係を追加します。
<properties> <freemarker.version> 2.3.19 </freemarker.version> <servlet.version> 2.5 </servlet.version> </properties> <dependency> groupid> org.freemarker </groupid> <artifactid> freemarker </artifactid> </sepremarker。 <GroupId> Javax.Servlet </groupId> <ArtifActid> servlet-api </artifactid> <version> $ {servlet.version} </version> <scope>2。Web.xmlにSpringのリスナーとSpringMVCのサーブレットを追加します:
<リスナー> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> <! - コンテナイベントを聞き、Webアプリケーションコンテキストを初期化して閉じ、ContextCleanuplistenerを呼び出してリソースをクリーンアップする - > </</リスナー> <リスナー> <リスナークラス> org.springframework.web.context.contextcleanuplistener </ristener-class> <! - servletcontextでスプリング関連の破壊可能なリソースをクリーンアップします。 <Servlet-Class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <! - <init-param> <param-name> contextconfiglocation </param-name> <param-value> /web-inf/hbatis-servlet.xml < $ {project.name} -servlet.xml in the web-infディレクトリ - > <load-on-startup> 1 </load-on-startup> </servlet> <servlet-name> hbatis </servlet-name> <url-pattern>*。3. Web-INFの下で新しいものを作成します。
Spring Configuration File ApplicationContext.xml:
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/P" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:context = "http://www.springframework.org/schema/context" 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 "> <context:property-placeholder location =" classpath:/database.properties "/> <! - データベース構成ファイル - > <bean id =" drageclassclassclassclassclassclassclassclass = P:url = "$ {url}" p:username = "$ {user_name}" p:password = "$ {password}" /> <! - データソース構成 - > <bean id = "sqlsessionfactory"> <! - sqlsessionfactory object-> <propertial name = "dataSource" /<<<offert " value = "classpath:configuration.xml"/> <! - mybatis configuration file-> <! - <プロパティ名= "mapperlocations" value = "classpath*:com/hbatis/model/*。 </bean> <bean id = "mapperconfigurer"> <! - 指定されたパッケージをスキャンしてマッパーを取得します - > <プロパティ名= "basepackage" value = "com.john.hbatis.mapper"/> </bean> </bean>database.properties classpath:
DriverClassName = com.mysql.jdbc.driver url = jdbc:mysql:// localhost:3306/mybatis?charatereCoding = utf8 user_name = root password = 123456
注:MapperScannerConfigurerは、ユーザー名がユーザーのアカウントを取得し、データベース接続が失敗するため、別の値に変更されます:user_name。
springmvc構成ファイルhbatis-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:Context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc http://www.springframework.org/schema/MVC/ RequestMappingHandLermapping、RequestMappingHandlerAdapter、およびExceptionHandlerexceptionResolver @RequestMapping、@exceptionHandlerなどの注釈をサポートする - > <コンテキスト:com.john.hbatis.controller " />> <!プロセッサ - > <bean id = "viewresolverftl"> <プロパティ名= "Viewclass" value = "org.springframework.web.servlet.view.freemarker.freemarkerview"/> <Property name = "contentType" value = "text/html; charset = utf-8"/> <"" "/> <" "/>値= "false"/> <プロパティ名= "viewnames"> <array> <balue>*。ftl</value> </array> </property> <! - <プロパティ名= "value ="。ftl "/> - > <プロパティ名="注文値= "0"/> <! <プロパティ名= "TemplateloaderPaths"> <list> <value>/web-inf/ftl/</value> <! - テンプレートの読み込みパス - > </list> </property> </bean> </beans>
4。MVC:
制御層:usercontroller.java
@Controller @RequestMapping( "/article")パブリッククラスusercontroller {@autowired iusermapper mapper; @RequestMapping( "/list")public string showall(modelmap modelmap){list <porth> article = mappper.getarticlesbyuserid(1); modelmap.addattribute( "記事"、記事); "main.ftl"を返します。 }}レイヤーを表示:main.ftl:
<#list article as article> <div> $ {article.id}。 $ {article.title}:$ {article.content} </div> </#list>5.プロジェクトを開始し、ブラウザを入力します:http:// localhost:8080/hbatis/article/list.htm結果を表示します。