springboot+springcloud+vue学习(二)

json 接口开发:

@DeleteMapping
@PostMapping    获取body中的实体(@RequestBody)
@GetMapping("/assent/{name}")     获取路径中的参数(@PathVariable)

log日志配置:

logging.path=/user/local/log

logging.level.com.favorites=DEBUG

logging.level.org.springframework.web=INFO

logging.level.org.hibernate=ERROR

mysql 和jpa的使用:

想要使用mysql 和jpa 需要在pom.xml中加入相关的包

<dependency>

  <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

  <groupId>mysql</groupId>

  <artifactId>mysql-connector-java</artifactId>

</dependency>

在配置文件中需要配置数据库连接,和jpa

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver  //此项可检查数据库驱动是否正常

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.show-sql= true

其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构,有四个值:

  1. create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
  2. create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
  3. update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
  4. validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

dialect 主要是指定生成表名的存储引擎为InneoDB
show-sql 是否打印出自动生产的SQL,方便调试的时候查看(这里给true 使用jpa自定义接口时方便检查)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

接下来就是创建实体,继承jpa接口 ,service层逻辑处理,controller对外接口访问

例:

@Entity@Table(name = "T_SYS_USER")    //数据库表名public class Users implements Serializable{

   private static final long serialVersionUID = 1L;

@Id    @Column(name = "ID", nullable = false, length = 100)    //@GeneratedValue 主键自增长    private String id;

@Column(name = "ROLE_ID", nullable = true, length = 100)    private String role_id;

@Column(name = "ORG_ID", nullable = true, length = 100)    private String org_id;

@Column(name = "NAME", nullable = true, length = 30)    private String name;

@Column(name = "PASSWORD", nullable = true, length = 30)    private String password;

@Column(name = "EMAIL", nullable = true, length = 100)    private String email;

@Column(name = "TEL", nullable = true, length = 12)    private String tel;

@Column(name = "NICKNAME", nullable = true, length = 20)    private String nickname;

@Column(name = "CREATE_USER" , nullable = true, length = 30)    private String createUser;

@Column(name = "CREATE_DATE")    private Date createDate;

@Column(name = "UPDATE_USER" , nullable = true, length = 30)    private String updateUser;

@Column(name = "UPDATE_DATE")    private Date updateDate;对于在数据库不想生成的字段添加 @Transient 注解,数据库就不会生成相应的列

dao接口层只要继承JpaRepository类就可以

例:

public interface AssentBasicRepository extends JpaRepository<AssentBasic, String> {}  //AssentBasic为实体类,String为实体类主键的类型

继承jpa之后可以使用jpa封装好的接口 ,也可以根据jpa的约定定义方法名,jpa会根据方法名生成相应的接口

如:findByUserName jpa就会认为这个接口是一个以UserName字段为参数的全查询接口

getUsersByNameAndPassword  jpa会认为这是一个以Name和Password为参数获取Users实体的方法(具体相关定义规则,请自主查询)另外要提的就是他的修改添加操作,走save()方法
除此之外jpa 还有它分页查询自动排序等功能 (此处先空着......后期补上)
还有就是自定义sql语句:接口名不能和jpa规则要求一样
//    @Query(value="SELECT * FROM T_MAP_UA WHERE UID=?1", nativeQuery=true)//    List<UsersAssents> selectByUid(String uid);
//    @Transactional     //    @Modifying//    @Query(value = "delete from T_ASSENT_BASIC where id =?1", nativeQuery = true)//    boolean deleteAssents(String id);
除查询之外,其他操作需要开启事物,在sql语句之上需要添加 @Transactional  @Modifying  否则自定义sql语句会失败!

service层 定义需要调用的接口(完全自定义,供controller层调用)
public interface AssentBasicService {
List<AssentBasic> getAssentBasicList();

Object delAssentBasic(String id);

AssentBasic postAssentBasic(AssentBasic assent);
}定义impl类:
@Servicepublic class AssentBasicImpl implements AssentBasicService {
@AutowiredAssentBasicRepository assentBasicRepository;  //注入dao层继承jpa的方法
@Overridepublic List<AssentBasic> getAssentBasicList(){    return assentBasicRepository.findAll(); //调用jpa封装好的方法}
}定义controller类:
@RestController  //以json形式返回数据@CrossOrigin     //解决跨域问题@RequestMapping("/v1")   //前缀路径public class AssentBasicController {
@AutowiredAssentBasicService assentBasicService;  //注入service层 
@GetMapping("/assent")   //get请求方式@ResponseBodypublic List<AssentBasic> getAssentsList() {    return assentBasicService.getAssentBasicList(); //调用service 层中的方法返回json数据}
@DeleteMapping("/assent/{assentId}")@ResponseBodypublic Object delAssentBasic(@PathVariable String assentId) {  //@PathVariable 获取请求路径中的参数
    return assentBasicService.delAssentBasic(assentId);}
@PostMapping("/assent")@ResponseBodypublic AssentBasic postAssentBasic(@RequestBody AssentBasic assent) {    //@RequestBody  获取body主体中的实体对象(通常以json形式传递)
    if(assent.getId() ==null){        assent.setId(generatePrimaryKey.generatePrimaryKey());    }    return assentBasicService.postAssentBasic(assent);}
}后台接口逻辑处理完毕,启动项目访问地址即可获得数据,也可通过Postman进行测试接口



原文地址:https://www.cnblogs.com/zhangxiaan/p/10219222.html

时间: 2024-07-31 16:45:34

springboot+springcloud+vue学习(二)的相关文章

springboot+springcloud+vue学习(三)

thymeleaf模板 :(前端了解不多,主要使用vue,后期再做介绍) springboot 对jsp不是很友好,官方推荐使用thymeleaf模板 Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用. 需要特别注意的是Thymeleaf对于URL的处理是通过语法@{-}来处理的.Thymeleaf支持绝对路径URL:<a th:href="@{http://www.thymeleaf.org}">Thymeleaf<

微服务架构【SpringBoot+SpringCloud+VUE】二 || 项目架构简介

目录 1.概述 2.开发环境 3.部署环境 4.项目管理 5.后端主要技术栈 6.前端主要技术栈 7.项目开发环境服务规划 8.学习交流QQ群[883210148] 1.概述 本项目是一个基于SpringBoot2.x+vue2.X的分布式微服务架构项目,项目会不断进行迭代更新.该项目后端利用SpringCloudAlibaba微服务架构解决方案进行重构.项目前端利用vue框架开发,页面展示主要为pc端和手机端(微信公众号). 2.开发环境 开发工具:IntellijIDEA.VSCode Ja

vue学习二:用 Vue.js + Vue Router 创建单页应用的几个步骤

通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项目中创建如下对应的文件 ├── App.vue // APP入口文件 ├── api // 接口调用工具文件夹 │ └── index.js // 接口调用工具 ├── components // 组件文件夹,目前为空 ├── config // 项目配置文件夹 │ └── index.js // 项目配置文

二、基于【 springBoot +springCloud+vue 项目】二 || 项目架构目录

1.项目架构目录 API工具类 ---commons-parent ------commons-api(api接口统一返回与统一异常处理) ------commons-core(业务通用工具类) ------commons-base(项目通用工具类) ------commons-test(工具类测试) API网关解决方案 ---theme-parent ------theme-common ---------theme-common-gateway-core(网关解决方案) ---------t

vue学习二:

vue的常用标签: 1.<router-link to=''>主要实现跳转链接功能,属性to='/'即是跳转到path为'/'的路径. 2.v-bind动态绑定指令,格式为:v-bind:你要动态变化的值="表达式" 3.v-for列表渲染 例: <tr v-for="item in peoples"> <td> {{item.name}} </td>  </tr> 4.v-show指令与v-if指令的区别

微服务架构【SpringBoot+SpringCloud+VUE】一 || 微服务简介

目录 1.什么是微服务 2.Spring Cloud是什么 3.服务注册中心 4.学习交流QQ群[883210148] 1.什么是微服务 在开发项目之前,我们有必要了解一下,什么是微服务?简单来说:微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成.系统中的各个微服务可被独立部署,各个微服务之间是松耦合的.每个微服务仅关注于完成一件任务并很好地完成该任务.在所有情况下,每个任务代表着一个小的业务能力. 2.Spring Cloud是什么 Spring Cloud是一个微服务框架的规

vue学习(二):自定义过滤器和自定义指令

#自定义指令 自定义指令的参数有: el: 指令所绑定的元素,可以用来直接操作 DOM . binding: 一个对象,包含以下属性: name: 指令名,不包括 v- 前缀. value: 指令的绑定值, 例如: v-my-directive="1 + 1", value 的值是 2. oldValue: 指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用.无论值是否改变都可用. expression: 绑定值的字符串形式. 例如 v-my-di

SpringBoot+SpringCloud+vue+Element开发项目——数据库设计

1.用户表(sys_user) CREATE TABLE `sys_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(50) NOT NULL COMMENT '用户名', `nick_name` varchar(150) DEFAULT NULL COMMENT '昵称', `avatar` varchar(150) DEFAULT NULL COMMENT '头像', `password`

SpringBoot+SpringCloud+vue+Element开发项目——集成Druid数据源

添加依赖 pom.xml <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> 添加配置文件 application.yml server: port: 800