spring boot 集成 Mybatis,JPA

    相对应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);

  

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

      

      

      

      

      

          

猜你喜欢

转载自www.cnblogs.com/jingjiren/p/10334816.html