1. 程式人生 > >spring boot 集成 Mybatis,JPA

spring boot 集成 Mybatis,JPA

stat 清除 back ransac 框架 mybatis com 並不是 encoding

    相對應MyBatis, JPA可能大家會比較陌生,它並不是一個框架,而是一組規範,其使用跟Hibernate 差不多,原理層面的東西就不多講了,主要的是應用。 Mybatis就不多說了,SSM這三個框架現在基本上都是基本框架了。 MyBatis 與 Spring boot 整合時除了添加必要的jar, 插件。在applicatoin.properties/application.yml 中添加相應的配置。

      註意的一點就是在啟動類中記得添加@MapperScan("com.spSystem.mapper") 註解,註解中填寫的是需要掃描的mapper 文件路徑。其余與SSM 運用時差不多

      閱讀此篇博客請先去閱讀本人的spring boot 常用註解,JPA常用註解。spring boot 項目要部署到 linux上tomcat 中 請去閱讀本人的spring boot tomcat 部署

    application.properties:

      技術分享圖片

    application.yml:

      技術分享圖片

    application.properties其中的mybatis相關配置:

  mybatis.typeAliasesPackage=com.spSystem.model      #  pojo 存放的路徑
  mybatis.mapperLocations=classpath\:mapper/*.xml    #  mapper.xml存放的路徑(相當於resource來說)
  server.port=8088                       #  項目的端口

  #  DataSource 相關配置
  spring.datasource.url=jdbc:mysql://localhost:3306/123?useUnicode=true&characterEncoding=utf8
  spring.datasource.username=root
  spring.datasource.password=123
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    啟動類:也就是普通類加上springboot註解,其中寫個main 方法。啟動的時候通過main 方法啟動程序就行了

      技術分享圖片

    spring boot 集成JPA: 其余都相差不多,pojo 需要根據JPA常用註解自己寫。

      

技術分享圖片

/*註解用於提交事務,若沒有帶上這句,會報事務異常提示*/
@Transactional
/*自動清除實體裏保存的數據*/
@Modifying(clearAutomatically = true)
/*JPA 集成中的SQL 沒有的就需要自己寫原生SQL*/
@Query(value = "update info p set p.status =?1 where p.id = ?2",nativeQuery = true)
 int updateStatusById( String status,  String id);

  

    在此,希望此篇博客能幫助到一些人

      

      

      

      

      

          

spring boot 集成 Mybatis,JPA