Java 開発における SSM 統合のための Spring+mabatis
Spring と mybatis の統合
統合のアイデア
Spring はシングルトンを通じて SqlSessionFactory を管理する必要があります。
Spring と myatis の統合はプロキシ オブジェクトを生成し、SqlSessionFactory を使用して SqlSession を作成します。 (springとmybatisの統合は自動で完了します)
永続層のmapperとdaoは管理用にspringが必要です。
環境を統合し、新しい Java プロジェクトを作成します (実際の開発プロジェクト構造に近い)
jar パッケージ:
mybatis3.2.7 jar パッケージ
spring 4.3.9 jar パッケージ
mybatis と spring の統合パッケージ: 初期の ibatis と spring の統合 正式に提供by spring、mybatis および spring 統合は mybatis によって提供されます。
sqlSessionFactory
applicationContext.xmlでsqlSessionFactoryを設定します
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据源,使用dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/><!-- jdbc:mysql://localhost:3306/test?characterEncoding=utf8 --> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="10"/> <property name="maxIdle" value="5"/> </bean> <!-- sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml"/> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 原始dao接口 --> <bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
オリジナルのdao開発(Springとの統合後)
mapper.xml
<!-- 加载映射文件 --> <mappers> <mapper resource="sqlmap/User.xml"/>
dao
public interface UserDao { //根据id查询用户信息 public User findUserById(int id)throws Exception;
daoインターフェース実装クラスはSqlsessionFactoryを注入する必要がありますスプリングインジェクション。
ここで Spring は設定メソッドを宣言し、dao Bean を設定します。
UserDaoImpl実装クラスにSqlSessionDaoSupportクラスを継承させます
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public User findUserById(int id) throws Exception { //继承SqlSessionDaoSupport ,通过this.getSqlSession()得到sqlSession SqlSession sqlSession = this.getSqlSession(); User user = sqlSession.selectOne("test.findUserById", id); //释放资源 //sqlSession.close(); return user; } }
daoを設定します
applicationContext.xmlでdaoを設定します。
<!-- 原始dao接口 --> <bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
テストを実行してください
rree以上がJava 開発における SSM 統合のための Spring+mabatisの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undress AI Tool
脱衣画像を無料で

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Java.Timeパッケージのクラスを使用して、古い日付とカレンダーのクラスを置き換えます。 2。LocalDate、LocalDateTime、LocalTimeを通じて現在の日付と時刻を取得します。 3。of()メソッドを使用して特定の日付と時刻を作成します。 4.プラス/マイナスメソッドを使用して、時間を不正に増加させて短縮します。 5. ZonedDateTimeとZoneIDを使用して、タイムゾーンを処理します。 6。DateTimeFormatterを介したフォーマットおよび解析の文字列。 7.インスタントを使用して、必要に応じて古い日付型と互換性があります。現代のJavaでの日付処理は、java.timeapiを使用することを優先する必要があります。

JDBCトランザクションを正しく処理するには、最初に自動コミットモードをオフにし、次に複数の操作を実行し、結果に応じて最終的にコミットまたはロールバックする必要があります。 1。CONN.SETAUTOCOMMIT(FALSE)を呼び出して、トランザクションを開始します。 2。挿入や更新など、複数のSQL操作を実行します。 3。すべての操作が成功した場合はconn.commit()を呼び出し、データの一貫性を確保するために例外が発生した場合はconn.rollback()を呼び出します。同時に、リソースを使用してリソースを管理し、例外を適切に処理し、接続を密接に接続するために、接続の漏れを避けるために使用する必要があります。さらに、接続プールを使用してセーブポイントを設定して部分的なロールバックを達成し、パフォーマンスを改善するためにトランザクションを可能な限り短く保つことをお勧めします。

依存関係の指示(di)isadesignpatternwhere objectsreceivedenciesiesedternally、setter、orfieldinoffiction.2.springframeworkusessaNnotationslike@component、@service、@autowiredwithjava Basedconfi

Pre-formanceTartuptimeMemoryusage、quarkusandmicronautleadduetocopile-timeprocessingingandgraalvsupport、withquarkusoftentylightbetterine serverlessシナリオ。

setupamaven/gradleprojectwithjax-rsdependencieslikejersey; 2.createarestresourceingnotationssuchas@pathand@get; 3.configuretheapplicationviaapplicationubclassorweb.xml;

パフォーマンス分析ツールを使用してボトルネックを見つけ、開発とテスト段階でVisualVMまたはJProfilerを使用し、生産環境で非同期財産を優先します。 2。オブジェクトの作成を削減し、オブジェクトを再利用し、StringBuilderを使用して文字列のスプライシングを置き換え、適切なGC戦略を選択します。 3.コレクションの使用を最適化し、シーンに応じて初期容量を選択し、プリセットします。 4.同時性を最適化し、同時コレクションを使用し、ロックの粒度を低減し、スレッドプールを合理的に設定します。 5. JVMパラメーターを調整し、合理的なヒープサイズと低遅延のゴミコレクターを設定し、GCログを有効にします。 6.コードレベルでの反射を避け、ラッパークラスを基本タイプに置き換え、初期化を遅延させ、最終と静的を使用します。 7。JMHと組み合わせた連続性能テストと監視

Mavenは、Javaプロジェクト管理と建設の標準ツールです。答えは、POM.xmlを使用してプロジェクト構造、依存関係管理、建設ライフサイクルの自動化、プラグイン拡張機能を標準化するという事実にあります。 1. pom.xmlを使用して、GroupID、artifactid、バージョン、依存関係を定義します。 2。MVNCLEAN、コンパイル、テスト、パッケージ、インストール、展開などのマスターコアコマンド。 3.依存関係管理と除外を使用して、依存関係バージョンと競合を管理します。 4.マルチモジュールプロジェクト構造を通じて大規模なアプリケーションを整理し、親POMによって均一に管理されます。 5。

thejvmenablesjavaの「writeonce、runany where "capabilitybyexcuting byteCodeThethermainComponents:1。theClassLoaderSubSystemloads、links、andinitializes.classfilesusingbootStrap、拡張、およびアプリケーションクラスローロー、
