このセクションではビデオデータベース#のMyBatisのテスト動作

tk.mybatisを使用してデータベース操作

章の前にコンフィギュレーションの一連の後、我々はデータベースのMyBaits動作を使用するために必要な条件を満たしている、以下では、データベースのtk.mybatis操作を使用した例です。

我々は、例えば、ユーザ・テーブルの動作をテストする(tb_user)を有します

エントリークラスを変更します。

使用する必要があります  @MapperScan パスマッパーインタフェースを指定する注釈を

PS:注という  @MapperScan 注釈がある  tk.mybatis.spring.annotation.MapperScan; パッケージは下

パッケージcom.snake.hello.spring.boot。

輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入tk.mybatis.spring.annotation.MapperScan; 

@SpringBootApplication 
@MapperScan(basePackages = "com.funtl.hello.spring.boot.mapper" パブリック クラスHelloSpringBootApplication { 

    公共 静的 ボイドメイン(文字列[]引数){ 
        SpringApplication.run(HelloSpringBootApplication。クラス、引数)。
    } 
}

 

テストクラスを作成します。

パッケージcom.snake.hello.spring.boot。

輸入com.funtl.hello.spring.boot.entity.TbUser;
輸入com.funtl.hello.spring.boot.mapper.TbUserMapper;
輸入com.github.pagehelper.PageHelper;
輸入com.github.pagehelper.PageInfo;
輸入org.junit.Test;
輸入org.junit.runner.RunWith;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.boot.test.context.SpringBootTest;
輸入org.springframework.test.annotation.Rollback;
輸入org.springframework.test.context.junit4.SpringRunner;
インポートorg.springframework.transaction.annotation.Transactional;
 インポートtk.mybatis.mapper.entity.Example; 

インポートjava.util.Date;
 インポートjava.util.Listに、

@RunWith(SpringRunnerのクラス
@SpringBootTest(クラス = HelloSpringBootApplication クラス
@Transactional 
@Rollback 
パブリック クラスMyBatisTests { 

    / ** 
     *噴射データ問合せインタフェース
     * / 
    @Autowired 
    プライベートtbUserMapper tbUserMapper; 

    / ** 
     *テスト挿入データ
     * / 
    @Test 
    公共 ボイドて、testInsert(){
         //テストデータ構造 
        TbUser tbUser = 新しい新しいTbUser(); 
        tbUser.setUsername( "Lusifer" ); 
        tbUser.setPassword( "123456" ); 
        tbUser.setPhone( "15888888888" ); 
        tbUser.setEmail([email protected]。 COM " ); 
        tbUser.setCreated(新新A日付()); 
        tbUser.setUpdated(新新A日付()); 

        // 挿入データ
        tbUserMapper.insert(tbUser); 
    } 

    / ** 
     *削除のテストデータ
     * / 
    @Test 
    公共 のボイドTestDelete (){
         //WHEREユーザー名= tb_user DELETE 'Lusifer'からと同等の構造的条件、 
        例例= 新しい新しい例(。TbUser クラス
        。Example.createCriteria()andEqualTo( "ユーザ名"、 "Lusifer" ); 

        // データ削除
        tbUserMapper.deleteByExampleを(例); 
    } 

    / ** 
     *テスト修正されたデータ
     * / 
    @Test 
    公共 ボイドtestUpdate(){
         // 構造条件 
        例例= 新しい新しい例(TbUser。クラス
        。example.createCriteria()andEqualTo( "ユーザ名"、「Lusifer " ); 

        // テストデータを作成します
        TbUser tbUserは= 新しいTbUserを(); 
        tbUser.setUsername( "LusiferNew" ); 
        tbUser.setPassword( "123456" ); 
        tbUser.setPhone( "15888888888" )。
        tbUser.setEmail( "[email protected]" ); 
        tbUser.setCreated(新しい日付()); 
        tbUser.setUpdated(新しい日付()); 

        // 修改数据
        tbUserMapper.updateByExample(tbUser、一例)。
    } 

    / ** 
     *测试查询集合
     * / 
    @Test 
    公共 ボイドtestSelect(){ 
        リスト<TbUser> tbUsers = tbUserMapper.selectAll();
         のため(TbUser tbUser:tbUsers){ 
            System.out.printlnは(tbUser.getUsername()); 
        } 
    } 

    / ** 
     *テストページングクエリ
     * / 
    @Test 
    公共 ボイドTestPage() {
         // PageHelper非常に単純な、単にページ番号とページ表示項目を設定することができる 
        PageHelper.startPage(0、2 ); 

        // セットページ問合せ 
        例例= 新しい新しい例(TbUser。クラス); 
        PageInfo <TbUser> = PageInfo 新しい新しい PageInfo <> (tbUserMapper.selectByExample(例)); 

        //クエリ結果取得 
        一覧<TbUser> tbUsers = pageInfo.getList();
         のため(TbUser tbUser:tbUsers){ 
            System.out.printlnは(tbUser.getUsername()); 
        } 
    } 
}

 

 添付ファイル:完全POM
<?xmlのバージョン= "1.0"エンコード= "UTF-8"> 
<プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0"のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス」
         のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion> 4.0.0 </ modelVersion> 

    <groupIdを> com.funtl </ groupIdを> 
    <たartifactId>ハロー春ブート</たartifactId> 
    <バージョン> 0.0.1-SNAPSHOT </バージョン> 
    <パッケージ> JAR </パッケージ> 

    <名前>こんにちはスプリング・ブーツ</名前> 
    <記述> </記述> 

    <親> 
        <groupIdを> org.springframework。ブート</ groupIdを> 
        <たartifactId>春・ブート・スターター・親</たartifactId>
        <バージョン> 2.0.2.RELEASE </バージョン>
            <groupIdを> org.springframework.boot </ groupIdを>
        <relativePath /> <! -リポジトリから検索親- > 
    </親> 

    <プロパティ> 
        <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> 
        <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding> 
        <のjava.version> 1.8 </java.version> 
    </プロパティ> 

    <依存性> 
        <依存性> 
            <のgroupId> org.springframework.boot </のgroupId> 
            <たartifactId>ばねブートスタータ-web </たartifactId> 
        </依存> 
        <依存性> 
            <たartifactId>ばねブートスタータTomcatの</たartifactId>
        </依存関係> 
        <依存>
            <のgroupId> org.springframework.boot </のgroupId> 
            <たartifactId>ばねブートスタータthymeleaf </たartifactId> 
        </依存> 
        <依存性> 
            <のgroupId> org.springframework.boot </のgroupId> 
            <たartifactId> spring-ブートスタータアクチュエータ</たartifactId> 
        </依存> 
        <依存性> 
            <のgroupId> org.springframework.boot </のgroupId> 
            <たartifactId>ばねブートスタータ試験</たartifactId> 
            <スコープ>テスト</スコープ> 
        </依存> 

        <依存性> 依存関係> 
            <groupIdを> com.alibaba </ groupIdを>
            <たartifactId>ドルイドスプリングブートスタータ</たartifactId> 
            <バージョン> 1.1.10 </バージョン> 
        </依存関係> 
        <依存性> 
            <のgroupId> tk.mybatis </のgroupId> 
            <たartifactId>マッパースプリングブートスタータ</たartifactId> 
            <バージョン> 2.0.2 </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> com.github.pagehelper </のgroupId> 
            <たartifactId> pagehelperスプリングブートスタータ</たartifactId> 
            <バージョン> 1.2.5 </バージョン> 
        </依存> 

        <依存> 
            <groupIdを> net.sourceforge.nekohtml </ groupIdを> 
            <たartifactId> nekohtml </たartifactId> 
            <バージョン> 1.9.22 </バージョン> 
        </依存関係>
        <依存>  
            <groupIdを> mysqlの</ groupIdを>
            <たartifactId>のmysql-コネクタ-javaの</たartifactId> 
            <スコープ>ランタイム</スコープ> 
        </依存関係> 
    </依存関係> 

    <ビルド> 
        <プラグイン> 
            <プラグイン> 
                <groupIdを> org.springframework.boot </ groupId> 
                <たartifactId>春・ブートのmaven-pluginの</たartifactId> 
                <設定> 
                    <mainClass> com.funtl.hello.spring.boot.HelloSpringBootApplication </ mainClass> 
                </ configuration>の
            </プラグイン> 

            <プラグイン> 
                <groupId> org.mybatis.generator </ groupIdを>
                <たartifactId> MyBatisの発電-達人-プラグイン</たartifactId>/バージョン> 
                    </依存関係>MyBatisの-発電のMavenプラグイン- </たartifactId> 
                <バージョン> 1.3.5 </バージョン>
                <構成> 
                    <configurationFile> $ {BASEDIR} /src/main/resources/generator/generatorConfig.xml </ configurationFile> 
                    <上書き>  </上書き> 
                    <詳細>  </冗長> 
                </構成> 
                <依存性> 
                    <依存> 
                        <groupIdを>のMySQL </のgroupId> 
                        <たartifactId> MySQLのコネクタからJava </たartifactId> 
                        <バージョン> $ {mysql.version} </バージョン> 
                    <依存性>
                        <groupIdを> tk.mybatis </ groupIdを>
                        <たartifactId>マッパー</たartifactId> 
                        <バージョン> 3.4.4 </バージョン> 
                    </依存関係> 
                </依存関係> 
            </プラグイン> 
        </プラグイン> 
    </ビルド> 

</プロジェクト>

 

おすすめ

転載: www.cnblogs.com/snake107/p/11915003.html