少し前に、SpringbootとSpring-Data-JPAを使用して開発されたプロジェクトを引き継ぎました。その後、JPAはMyBatisよりも使用が難しすぎると言って、新しい友人が追加されました。また、複数のテーブルでクエリを書くことも困難であるため、MyBatisのサポートを追加しました。
最初に
@configuration@enablejparepositories( "com.xxx.xxx.repository")class jpaconfig
この方法を使用してJPAを構成する場合、遭遇する問題の1つは、選択できますが保存できないため、構成ファイルに変更されます。
以下は、構成ファイルを直接紹介します。
1。スプリング構成
<?xmlバージョン= "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschem a-instance "xmlns:context =" http://www.springframework.org/schema/context "xmlns:tx =" http:// www。 springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:task = "http://www.springframework.org/schema/task" xsi:schemalocation = "http://www.springframework.or g/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xdhttp://www.springframework.or g/schema/context/spring-context.xsdhttp://www.springframework.org/schema/context/spring-context.xsdhttp ://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhtt P://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" id = "springcontextholder" lazy-init = "false"> </bean> <bean id = "configproperties" class = "org.springframework.beans.factory.config.propertiesfactorybean"> <プロパティname = "locations"> <list> <value> classpath*:jdbc.properties </value> <balut> classpath*:app.properties </value> </list> </property> </bean> <bean id = "propertyconfigurer" class = "org.springframework.beans.factory.config.properfig.properfig.config.prefererence.properhold.figurn name = "SystemPropertiesModename" value = "system_properties_mode_override"/> <プロパティname = "filecoding" value = "utf-8"/> <プロパティ名= "ref =" configproperties "/> </bean> <! - bean id =" init-method "" detasoud = "close-method"基本プロパティURL、ユーザー、パスワード - > < value = "$ {ds.password}"/> <! - 初期化サイズ、最小および最大値を構成 - > <プロパティ名= "hientsize" value = "$ {ds.InitialSize}"/> <プロパティ名= "$ {ds.minidle}"/>> <プロパティ "接続がタイムアウトするのを待つ - > <プロパティ名= "maxwait" value = "$ {ds.maxwait}"/> <! - 一度チェックするのにかかる時間を構成します。接続がプールで生き残るための最小時間をMilliseconds - > <プロパティ名= "minevictableidletimemillis" value = "$ {ds.minevictableidletimemillis}"/> <Property name = "validationquery" value = "$ {ds.validationquery}" value = "$ {ds.testhiledle}"/> <プロパティ名= "testonborrow" value = "$ {ds.testonborrow}"/> <プロパティname = "testonreturn" value = "$ {ds.testonreturn} value = "$ {ds.poolpreparedStatements}"/> <Property name = "maxpoolporedStatementperconnectionsize" value = "$ {ds.maxpoolpreparedStatementperconnectionSize}"/> <!放棄された接続を閉じるときのエラーログ - > <プロパティname = "logabandeded" value = "$ {ds.logabandened}"/> </bean> <! - spring and mybatisは完全に統合されており、mybatisはmybatisを必要としません - > <bean id = "sqlsessionfactory"> <proprity name " name = "configlocation" value = "classpath:mybatis-config.xml"/> <プロパティname = "typealiasespackage" value = "com.xxx.culture.domain"/> <! - 自動的にmapping.xmlファイルを自動的にスキャンします - > <プロパティ名= <property name = "mapperlocations"値DAOインターフェイスがある場合、Springは自動的にクラスを見つけます - > <bean> <プロパティ名= "basepackage" value = "com.xxx.xxx.dao"/>> </bean> <! - (トランザクション管理)トランザクションマネージャー、グローバルTXにjtatransactionmanagerを使用 - > <> <bean id = "transactionManager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager"> <プロパティ名= "datasource" ref = "dataSource"/> </bean> < Transaction-Manager = "TransactionManager"> <tx:aTtributes> <! - 例外がある限り、挿入、更新、削除、ロールバックから始まるトランザクション管理メソッド - > <tx:method name = "insert*" propagation = "rollback-for =" java.lang.throwable "/> <tx:method name =" "uppretagation =" " Rollback-for = "Java.lang.Throwable"/> <tx:method name = "delete*" propagation = "rollback-for =" java.lang.lang.throwable "/> <tx:method name =" remove*"remove*" ropegation = "lollback-for-for =" java.lang.lang.throwable " lollback-for = "java.lang.throwable"/> <tx:method name = "add*" ropagation = "expegation =" experagation = "method name =" flush*"propagation =" selection "/> <! - select、count、get、find、enabable read only fort nefument cumporation performance- name = "count*" read-only = "true"/> <tx:method name = "get*" read-only = "true"/> <tx:method name = "read*" read name = "true"/> <tx: "find*" read- "/> <tx:method name =" method name = "read"/> <transixemagements for "/> < name = "*"/> </tx:属性> </tx:アドバイス> <! - トランザクションaop構成com.xxx.smp.service ..*empl。*(..)) - > <aop:config> <aop: "servicemethods" expression = "execution(*com.xx.xxx.service .. Advice-ref = "txadvice" pointcut-ref = "servicemethods"/> </aop:config> <! - springを構成するようにcglib proxyを使用 - > <aop:aspectj-autoproxy proxy-target-class = "true"/> <! - トランザクションアノテーション - > <tx:<tx: "annoction-driver-driver-driver-manager resource = "applicationContext-jpa.xml"/> </beans>2。JPA構成
最初は問題に遭遇しました:JPA org.hibernate.lazyInitializationException:プロキシを初期化できませんでした - セッションなし
構成は次のとおりです
<?xmlバージョン= "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:///ww.springframewo rk.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> <bean id =" entitymanagerfactory "class =" org.springframework.orm.jpa.localcontainerentity managmanagoryfacturfactebean "> <! ref = "dataSource"/> <! - エンティティエンティティクラスパッケージパスを指定します - > <プロパティ名= "packageStoscan"> <list> <value> com.xxx.xxx.jpadomain </value> </list> </property> <! - jpa属性を指定します; hibernateにSQL、方言などを表示するかどうか - > <プロパティ名= "jpavendoradapter"> <bean> <! - ddlファイルを生成するかどうか - > <プロパティ名= "generateddl" value = "true"/> <! - name = "databaseplatform" value = "org.hibernate.dialect.mysqldialect"/> <! - mysql、select by by by by by by - > <property name = "database" value = "mysql"/> </bean> </property> <property name = "jpaproperties"> <props> prop key = "hibernate.ejb.naming_strategy"> org.hibernate.cfg.improvednamingstrategy </prop> <prop key = "hibernate.hbm2ddl.auto"> update </prop> <prop key = "hibernate.enable_lazy_load_load_no_trans"> true </prop> </props> </property> </bean> <! - spring data jpa configuration-> <! - プロキシファクトリ-class = "com.monk.base.jpa.peakpeakpitoryfactoryfactoryclass =" com.monk.base.jpa.peakepositoryfationefactoryclass = "com.monk.base。書くことなくすべてのパッケージに直接注釈を付けることができます - > <jpa:リポジトリベースパッケージ= "com.xxx.xxx.repository" transaction-manager-ref = "transactionmanager-manager-manager-factory-ref =" entitymanagectory "/> <! ID = "TransactionManager"> <Property name = "EntityManagerFactory" Ref = "EntityManagerFactory"/> </bean> <! - ターンオンアノテーショントランザクション - > <TX:Annotation-Driven Transaction-Manager = "TransactionManager" Proxy-Target-Class = "True"/> </beans>
上記は、編集者が紹介したスプリングJPAとMyBatisの統合で発生した問題の分析です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!