春---- MyBatisの統合

、xml設定バージョン

  1.輸入依存度

<! -  MyBatisのとMyBatisのが提供するSpring統合パッケージ - >
<依存>
  <groupIdを> org.mybatis </ groupIdを>
  <たartifactId> MyBatisのスプリング</たartifactId>
  <バージョン> 1.3.0 </バージョン>
</依存関係>
<! -  MyBatisのコアjarファイル - >
<依存>
  <groupIdを> org.mybatis </ groupIdを>
  <たartifactId> MyBatisの</たartifactId>
  <バージョン> 3.4.1 </バージョン>
</依存関係>

  2.エンティティクラスを作成します。

パッケージcn.spring.entity。

インポートしたjava.io.Serializable;

パブリッククラス銀行はSerializable {実装
プライベート文字列card_idを。
プライベート文字列USER_NAME。
プライベート整数card_money。

パブリック文字列getCard_id(){
戻りcard_id。
}

公共ボイドsetCard_id(文字列card_id){
this.card_id = card_id。
}

パブリック文字列getUser_name(){
戻りUSER_NAME。
}

公共ボイドsetUser_name(文字列USER_NAME){
this.user_name = USER_NAME。
}

パブリック整数getCard_money(){
戻りcard_money。
}

公共ボイドsetCard_money(整数card_money){
this.card_money = card_money。
}
}

  3.ダオ層インターフェイスとそのマッピングファイルを作成します。

パッケージcn.spring.dao。

輸入cn.spring.entity.Bank;
輸入org.springframework.stereotype.Repository;

輸入はjava.util.List;

パブリックインターフェースIBankDao {
パブリックリスト<銀行> getAllBank();
}
<?xml version = "1.0"エンコード= "UTF-8"?> 
<DOCTYPEマッパー!
PUBLIC " - // mybatis.org//DTDマッパー3.0 // EN"
「http://mybatis.org/dtd/mybatis -3- mapper.dtd ">
<! -小配置根节点名前空間代表命名空间- >
<マッパーの名前空間=" cn.spring.dao.IBankDao ">
<。SELECT ID =" getAllBank」resultTypeと= "銀行">
銀行から選択*
</ select>の
</マッパー>

  レイヤインタフェースおよびその実装クラス4.Service

パッケージcn.spring.service。

輸入cn.spring.entity.Bank;

輸入はjava.util.List;

パブリックインターフェースIBankService {
パブリックリスト<銀行> getAllBank();
}
パッケージcn.spring.service.impl。

輸入cn.spring.dao.IBankDao;
輸入cn.spring.entity.Bank;
輸入cn.spring.service.IBankService;
輸入org.springframework.stereotype.Service。

輸入javax.annotation.Resource。
輸入はjava.util.List;

パブリッククラスIBankServiceImplは{IBankServiceを実装
@Resource
プライベートIBankDao iBankDao。
@Override
公共一覧<銀行> getAllBank(){
)(iBankDao.getAllBankを返します。
}

公共IBankDao getiBankDao(){
iBankDaoを返します。
}

公共ボイドsetiBankDao(IBankDao iBankDao){
this.iBankDao = iBankDao。
}

}

  5.プロファイル

<?xml version = "1.0" エンコード= "UTF-8"?>
< -  XMLファイルヘッダ(例:ノードが存在しなければならない)ファイルに拘束として作用! - >
<!DOCTYPE構成
        PUBLIC " -  // mybatis.org//DTDコンフィグ3.0 // EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<設定>

    <typeAliases>
        <パッケージ名= "com.wdksoft.entity" />
    </ typeAliases>

    <マッパー> <! - 私たちの設定マッピングファイルを管理します - >
        <パッケージ名= "com.wdksoft.dao" />
    </マッパー>

</設定>
jdbc.driver =はcom.mysql.jdbc.Driver
jdbc.url = JDBCます。mysql:// localhostを:3306 /銀行useUniCode =真&characterEncoding = UTF-8?
jdbc.username =ルート
jdbc.password = 12345
<?xml version = "1.0"エンコード= "UTF-8"?> 
<豆のxmlns = "http://www.springframework.org/schema/beans"
のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス"のxmlns:AOP = "http://www.springframework.org/schema/aop"
のxmlns:P = "http://www.springframework.org/schema/p"のxmlns:コンテキスト=" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/contextます。http:// www.springframework.org/schema/context/spring-context.xsd ">

<!- 負荷プロファイル - >
<コンテキスト:プロパティプレースホルダ位置= "クラスパスjdbc.properties" />
<! -配置数据源- >
<ビーンID = "データソース"クラス= "com.alibaba.druid.pool.DruidDataSource">
の<property名前= "driverClassName"値= "$ {jdbc.driver}" />
<プロパティ名= "URL"の値= "$ {jdbc.url}" />
<プロパティ名= "ユーザ名"値= "$ {JDBC。ユーザー名} "/>
<プロパティ名="パスワード"値= "$ {jdbc.password}"/>
</豆>
<! -配置SqlSessionFactoryBean - >
<豆ID = "sqlSessionFactoryBean"クラス=" org.mybatis .spring.SqlSessionFactoryBean ">
<プロパティ名= "データソース" refは= "データソース" />
<! -加载MyBatisの配置文件- >
<プロパティ名= "configLocation"値= "クラスパス:MyBatisの-config.xmlの" />
</豆>
<! -扫描ダオ层、省略代码- >
<beanクラス= "org.mybatis.spring.mapper.MapperScannerConfigurer">
<プロパティ名= "basePackage"値= "cn.spring.dao" / >
</豆>
<! -注入ダオ层- >
<豆ID = "iBankDao"クラス= "org.mybatis.spring.mapper.MapperFactoryBean">
<プロパティ名= "mapperInterface"値= "cn.spring。 dao.IBankDao "/>
<プロパティ名=" sqlSessionFactory "REF = "sqlSessionFactoryBean"/>
</豆>
<! -サービス层- >
<豆ID = "iBankService"クラス=" cn.spring.service.impl .IBankServiceImpl ">
<プロパティ名= "iBankDao" REF = "iBankDao" />
</豆>


</豆>

第二に、版ノート

  Spring構成ファイル:

<! - パックスキャナ - >
    <コンテキスト:コンポーネント・スキャンベースパッケージ=「com.wdksoft」/>
    <! - ロード・プロファイル - >
    <コンテキスト:プロパティプレースホルダ位置= "クラスパス:jdbc.properties" />
    <! - データソースの構成 - >
    <ビーンID = "データソース" クラス= "com.alibaba.druid.pool.DruidDataSource">
        <プロパティ名= "driverClassName" 値= "$ {jdbc.driver}" />
        <プロパティ名= "URL" の値= "$ {jdbc.url}" />
        <プロパティ名= "ユーザ名" 値= "$ {jdbc.username}" />
        <プロパティ名= "パスワード" 値= "$ {jdbc.password}" />
    </豆>
    <! - 配置SqlSessionFactoryBean  - >
    <ビーンID = "sqlSessionFactoryBean" クラス= "org.mybatis.spring.SqlSessionFactoryBean">
        <プロパティ名= "データソース" REF = "データソース" />
        <! - ロードMyBatisのプロフィール - >
        <プロパティ名= "configLocation" 値= "クラスパス:MyBatisの-config.xmlの" />
    </豆>
    <! - スキャンダオ層が省略されているコード - >
    <beanクラス= "org.mybatis.spring.mapper.MapperScannerConfigurer">
        <プロパティ名= "basePackage" 値= "com.wdksoft.dao" />
    </豆>

  に対応した他の追加の注釈

 

 

 

 

 

 

 

おすすめ

転載: www.cnblogs.com/F017/p/11800044.html