码迷,mamicode.com
首页 > 编程语言 > 详细

springboot注解加深

时间:2019-01-14 18:48:05      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:rac   static   gap   构造   求和   作用   oss   path   enc   

以标签模块为例

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
//这里是一个启动类
@SpringBootApplication
public class BaseApplication {
    public static void main(String[]args){
        SpringApplication.run(BaseApplication.class, args);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker();
    }
}

包结构

技术分享图片

总体来说比以前那种模式来说更简单了没有了实现类,

一、先从pojo来说

作用在实体类上的注解有 @Entity   @Table(name = "表名") 这两个 如果是多个服务器之前就要实现 Serializable 在io流中传输

作用在属性上主键 id 的 注解有  @Id  其他的正常生成构造函数和getset方法还有tostring方法

二、controller层

作用在类上的注解有

@RestController 可以将请求和响应的 json或者实体转为想要的

@CrossOrigin 跨域

@RequestMapping("/label") 一个总的请求地址

直接贴代码简单的增删改查

package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelSerivce;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelSerivce labelSerivce;
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",labelSerivce.findAll());
    }
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public  Result findById(@PathVariable("labelId")String id){
        return new Result(true, StatusCode.OK,"查询成功",labelSerivce.findById(id));
    }
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label){
        labelSerivce.save(label);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result update(@RequestBody Label label,@PathVariable("labelId") String id){
        label.setId(id);
        labelSerivce.update(label);
        return new Result(true, StatusCode.OK,"更新成功");
    }
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("labelId") String id){
        labelSerivce.deleteById(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

}

 三、service层代码

package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.List;

@Service
@Transactional
public class LabelSerivce {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;
    public List<Label> findAll(){
        return labelDao.findAll();
    }
    public Label findById(String id){
        return labelDao.findById(id).get();
    }
    public void save (Label label){
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }
    public void update (Label label){
        labelDao.save(label);
    }
    public void deleteById(String  id){
        labelDao.deleteById(id);
    }
}

四、dao层代码

 

package com.tensquare.base.dao;

import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * <Lable,String> 第一个参数是lable实体,第二个参数是id 更新删除查找要用到
 * 这里继承的JpaSpecificationExecutor用来分页等复杂操作
 */
public interface LabelDao extends JpaRepository <Label,String>,JpaSpecificationExecutor<Label>{
}

五、配置文件yml文件   application.yml

server:
  port: 9001
spring:
  application:
    name: tensquare-base
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/tensquare_base?characterEncoding=utf-8&useSSL=false
    password: root
    username: root

  jpa:
    database: mysql
    show-sql: true

 

 




 

 

springboot注解加深

标签:rac   static   gap   构造   求和   作用   oss   path   enc   

原文地址:https://www.cnblogs.com/inbeijing/p/10268209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!