【Springboot] springbootは、データベースMyBatisの操作に使用します

新springbootプロジェクトは、良いウェブ、MyBatisの、JDBCを選択してください

内に配置されたApplication.propertiesまたはapplication.yml

春:
  データソース:
    ドライバー・クラス名:com.mysql.cj.jdbc.Driverの
    URLます。jdbc:mysqlの:// localhostを:3306 / studentManagerBoot useUnicode =本当?&characterEncoding = UTF-8 &useSslオプション =真&serverTimezone = UTCの
    ユーザ名:ルート
    パスワード:

学生のエンティティクラス:

パッケージcom.hj.studentmanagerboot.user.entity。

パブリック クラス学生{
     プライベート int型stuId。
    プライベート int型stuNum。
    プライベート文字列のstuName。
    プライベート文字列stuBirthday。
    プライベート int型stuEnterYear。
    プライベート int型stiState。

    @Override 
    パブリック文字列のtoString(){
         戻り "生徒{" + 
                "stuId =" + stuId + 
                "stuNum =" + stuNum + 
                "stuName =" + stuName + 
                」、stuBirthday = '」
                "stuEnterYear =" + stuEnterYear +
                "stiState =" + stiState + 
                '}' 
    } 
    パブリック文字列getStuName(){
         戻りstuNameと、
    } 

    公共 ボイドsetStuName(文字列stuName){
         この .stuName = stuName。
    } 

    公共 INT getStuId(){
         戻りstuIdと、
    } 

    公共 ボイド setStuId(INT stuId){
         この .stuId = stuId。
    } 

    公共 INT getStuNum(){
         戻りstuNumと、
    }

    公共 ボイド(定住をint {回)
         この .stuNum = 時間; 
    } 

    パブリック文字列getStuBirthday(){
         戻りstuBirthdayと、
    } 

    公共 ボイドsetStuBirthday(文字列stuBirthday){
         この .stuBirthday = stuBirthday。
    } 

    パブリック INT getStuEnterYear(){
         戻りstuEnterYearと、
    } 

    公共 ボイド setStuEnterYear(INT stuEnterYear){
         この .stuEnterYear = stuEnterYear。
    }

    公共 INT getStiState(){
         戻りstiStateと、
    } 

    公共 ボイド setStiState(INT stiState){
         この .stiState = stiState。
    } 
}
コードの表示

getStudent(int型stuId)メソッドを追加した新しいStudentMapperのDAOインタフェースパッケージを書きます

パッケージcom.hj.studentmanagerboot.user.dao。

輸入com.hj.studentmanagerboot.user.entity.Student。
輸入org.springframework.stereotype.Repository; 

@Repository 
パブリック インターフェースStudentMapper { 
    生徒getStudent(INT stuId)。
}

新しいサービスパッケージgetStudentService、および自動組立StudentMapper、getStudentServiceを添加する方法

パッケージcom.hj.studentmanagerboot.user.service。

輸入com.hj.studentmanagerboot.user.dao.StudentMapper。
輸入com.hj.studentmanagerboot.user.entity.Student。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.stereotype.Service。

@Service 
パブリック クラスStudentServiceTest { 
    @Autowired 
    StudentMapper studentMapper。

    公共学生getStudent(int型stuId){
         返すstudentMapper.getStudent(stuIdを)。
    } 
}

自動組立StudentServiceのコントローラ

パッケージcom.hj.studentmanagerboot.user.handler。


輸入com.hj.studentmanagerboot.user.service.StudentServiceTest;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.web.bind.annotation.PathVariable;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RestController。

@RestController 
@RequestMapping( "testStudent" のパブリック クラスStudentHandlerTest { 
    @Autowired 
    プライベートStudentServiceTest studentServiceTest。
    @RequestMapping( "のgetUser / {ID}"パブリック文字列のgetUser(@PathVariable( "ID")INT stuId){
         戻りstudentServiceTest.getStudentを(stuId).toString(); 
    } 

}

注釈を高めるMyBatisの、プログラムはSpringBootで、入口DAOパッケージをスキャン

@MapperScan( "com.hj.studentmanagerboot.user.dao" パブリック クラス... { 

}

リソースフォルダストアMapper.xmlの新マッパーファイル、および保存されている情報を増やす中application.ymlまたはapplication.propertiesでマッパー設定ファイルをMyBatisの:

MyBatisの:
  マッパー-場所:クラスパス:マッパー/ * Mapper.xmlの
  型のエイリアスパッケージ:com.hj.studentmanagerboot.user.entity

コンフィギュレーション情報を書き込むStudentMapper.xml

<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
<!DOCTYPEマッパー
        PUBLIC " - // mybatis.org//DTDマッパー3.0 // EN" 
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 

< マッパー名前空間= "com.hj.studentmanagerboot。 user.dao.StudentMapper」> 
    < 選択ID = "getStudent" resultTypeと= "com.hj.studentmanagerboot.user.entity.Student" > 
        SELECT * FROM stuInfoからstuId =#{stuId} 
    </ 選択> 
</ マッパー>

 

おすすめ

転載: www.cnblogs.com/to-red/p/11368292.html