美文网首页
第一章 SpringBoot初接触(入门篇)

第一章 SpringBoot初接触(入门篇)

作者: Xia0JinZi | 来源:发表于2017-12-27 09:56 被阅读0次

    SpringBoot入门

    标签(空格分隔): springboot java springmvc


    工程创建

    • idea新建工程
    new project ->
    spring initializr ->next ->
    gorupid(jdk版本,maven,等等选择) -> next ->
    web - >web(勾上)->next ->
    填写项目路径
    
    • 项目基础配置参数
    ## 将文件poperties改为后缀为yml的文件
    server:
      context-path: /sell 项目路径
      port: 8080端口
    
    • 获取配置文件参数
    //方式一
    @Value(“${变量名}”)
    
    //方式二
    @Data
    @ConfigurationProperties(prefix = "excel") //文件对应yml中的
    @Component
    public class ExcelConfig {
    
        /** 导出路径 .*/
        private String outPath;
    
        /** 模板名称 .*/
        private String templePath;
    }
    
    excel:
      outPath: D:/temp/out.xls
      templePath: classpath:excel-templates/web-info-template.xls
    
    • 关于开发环境和部署环境
    ##新建一个yml文件
    application.yml文件中
    spring:
        profiles:
            active:dev # 如果是dev就用dev如果是pro就填pro
            
    application-dev.yml文件
    server:
      context-path: /sell 项目路径
      port: 8080端口
      
    application-pro.yml文件
    server:
      context-path: /sell 项目路径
      port: 8090端口
    

    控制层注解说明

    • Controller
    注解 说明
    @Controller 处理http请求
    @RestController spring 4 新添加注解即原先的@ResponseBody和@Controller使用
    @RequestMapping("/user") 配置url映射

    备注: @Controller 注解则需要配合一个模板来使用,其中需要引用模板类型,同时,就要有一个返回试图模板。

    • 传参使用
    注解 说明
    @PathVariable 获取url参数
    @RequestParam 获取请求参数
    @GetMapping get请求缩写(@RequestMapping(method="GET"))
    @PostMapping post请求缩写(@RequestMapping(method="POST"))

    备注:@PathVariable 获值方式url = http://....com/form/15645/apply @RequestMapping(value = "form/{id}/apply")
    @PathVariable("id") String id;@RequestParam 获值方式url:http://....com/test?id=123456 @RequestParam(value="id",required=false(默认不传值是可以得),default=150)

    • restfulApi 请求方式
    请求类型 请求方式 功能
    GET /girls 获取列表
    GET /girls/id 获取id的女生
    POST /girls 创建一个女生
    PUT /girls/id 更新一个女生操作
    DELETE /girls/id 删除一个女生操作

    数据库操作

    • 原课程:用mysql结合jpa,如果是其他的则使用其他数据库驱动
    <!--jpa-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!--数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
    • yml文件数据库参数配置
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver 驱动
        url: jdbc:mysql://192.168.1.142:3306/wechat_order?characterEncoding=utf-8&useSSL=false 链接地址
        username: root 用户名
        password: 123456 密码
      jpa:
        hibernate:  
            #是否自动创建sql语句 切忌使用Create 每次程序启动都会覆盖之前创建的表格 重新创建一个新的表格
            #使用update则存在的是实现更新 不存在的进行创建
            ddl-auto: create 
            #是否显示sql调试
            show-sql: true 
    
    

    备注:JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据,结合其他ORM的使用,能达到简化开发流程的目的,使开发者能够专注于实现自己的业务逻辑上。Spring Jpa 能够简化创建 JPA 数据访问层和跨存储的持久层功能,用户的持久层Dao接口只需要继承他自己定义好的(仓库)接口,无需再写实现类,就可以实现对象的CRUD操作,还有分页排序等功能。

    • 实体类创建
    @Table(name="product_category") //对应数据库中的表
    @Entity //dao层注解
    @DynamicUpdate //日期自动更新
    @Data //lombok工具无需添加get和set方法
    public class ProductCategory {
        /** id. */
        @Id //主键
        @GeneratedValue //自动增长值
        private Integer categoryId;
    
        /** 类目名称. */
        private String categoryName;
    
        /** 类目编号. */
        private Integer categoryType;
    
        public ProductCategory() {
        }
    
        public ProductCategory(String categoryName, Integer categoryType) {
            this.categoryName = categoryName;
            this.categoryType = categoryType;
        }
    }
    
    • 数据库操作需要添加一个Repository继承jpa
    //JpaRepository<OrderMaster,String> 第一个为返回值类型,第二个为表主键
    //默认会有一些增删该查
    //findOne(),findList(),save()....(这些都是依据主键或者是全部查询,或者存储)
    //如果需要用根据某个字段查询则需要按规定方式书写例如下面根据openid查询
    //数据库中字段名buyer_opendid则需要findByBuyerOpenid(String openid);
    public interface OrderMasterRepository extends JpaRepository<OrderMaster,String> {
        /** 根据买家openid分页查询订单. */
        Page<OrderMaster> findByBuyerOpenid (String buyerOpenid, Pageable pageable);
    }
    
    • 对于事物则用
    @Transactional 注解方式
    

    备注:事物管理保证执行同时操作时,例如两条数据同时插入,其中有一个插入失败,另一个也插入失败。


    • 原视频UP主慕课网(两小时入门SpringBoot)
    • 本篇博客撰写人: XiaoJinZi 个人主页 转载请注明出处
    • 学生能力有限 附上邮箱: 986209501@qq.com 不足以及误处请大佬指责

    相关文章

      网友评论

          本文标题:第一章 SpringBoot初接触(入门篇)

          本文链接:https://www.haomeiwen.com/subject/utbygxtx.html