菜鸟上路 Spring Boot+Spring data jpa+swagger 做的增 删 改 查

1 .数据库用的是mysql 数据库,项目整体结构如下,项目中Hystrix和Eureka可以看看,新手不晓得怎么上传项目文件 研究下后续给补上

菜鸟上路 Spring Boot+Spring data jpa+swagger 做的增 删 改 查_第1张图片

2.用的maven 这里贴出maven 的pom配置


  
  4.0.0  
  southernfund  
  ws  
  1.1  
  jar  
   
    org.springframework.boot  
    spring-boot-starter-parent  
    1.3.5.RELEASE  
     
    
   
    UTF-8  
    1.8 
    
   
     
      biz.paluch.logging  
      logstash-gelf  
      1.11.0 
      
     
      org.springframework.boot  
      spring-boot-starter-mail 
      
     
      com.h2database  
      h2 
      
     
      junit  
      junit  
      3.8.1  
      test 
      
     
      org.springframework.boot  
      spring-boot-starter-web 
      
     
      io.springfox  
      springfox-swagger2  
      2.4.0 
      
     
      io.springfox  
      springfox-swagger-ui  
      2.4.0 
      
     
      org.springframework.cloud  
      spring-cloud-starter-eureka 
      
     
      org.springframework.cloud  
      spring-cloud-starter-eureka-server  
      test 
      
     
      org.springframework.boot  
      spring-boot-starter 
      
     
      org.springframework.boot  
      spring-boot-starter-actuator 
      
      
     
      org.springframework.cloud  
      spring-cloud-starter-hystrix 
      
     
      org.springframework.boot  
      spring-boot-starter-data-jpa 
      
     
      mysql  
      mysql-connector-java 
      
     
      org.springframework.cloud  
      spring-cloud-starter-hystrix-dashboard 
      
     
      org.crazycake  
      jdbctemplatetool  
      1.0.4-RELEASE 
      
     
      org.springframework.boot  
      spring-boot-starter-jdbc 
     
    
   
     
       
        org.springframework.cloud  
        spring-cloud-dependencies  
        Brixton.SR5  
        pom  
        import 
        
       
        org.springframework.cloud  
        spring-cloud-netflix  
        1.1.6.RELEASE  
        pom  
        import 
       
     
    
   
     
       
        org.springframework.boot  
        spring-boot-maven-plugin  
         
          com.nf.Application 
          
         
           
             
              repackage 
             
           
         
       
     
   



3.Controller ( ApiOperation 注解是 swagger 的注解更多注解请自行查询)

@RestController
@RequestMapping("/user")
public class UserController {

Logger logger= Logger.getLogger(UserController.class);

@Autowired
private Userepository userepository;

/**
* 查询所有列表
* @return
*/
@RequestMapping(value="/UserList",method=RequestMethod.GET)
@ApiOperation(value="获取所有的列表",notes="不需要传递参数")
public List UserList(){
logger.info("userList");
return userepository.findAll();
}


/**
* 增加一个用户数据
* @param users
* @param bindingResult
* @return
*/
@SuppressWarnings("unchecked")
@RequestMapping(value="/SaveUser",method=RequestMethod.POST)
@ApiOperation(value="添加一个用户",notes="标题")
@Transactional
public com.nf.entity.Result SaveUser(@Valid Users users,BindingResult bindingResult){
logger.info("SaveUser");
if(bindingResult.hasErrors()){
return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
}
return ResultUtil.success(userepository.save(users));
}

/**
* 
* 更新
* @param users
* @param bindingResult
* @return
*/
@RequestMapping(value="/userUpdate",method=RequestMethod.POST)
@ApiOperation(value="更新用户信息",notes="更新")
public Users UsersUpdate(@Valid Users users,BindingResult bindingResult){
logger.info("userUpdate----");
if(bindingResult.hasErrors()){
return users;
}
return userepository.save(users);
}


/**
* 删除用户信息
* @param id
* no Result
*/
@RequestMapping(value="/delteUser/{id}",method=RequestMethod.DELETE)
@ApiOperation(value="删除用户信息",notes="Delete")
public void UserDelete(@PathVariable("id") final Integer id) {
userepository.delete(id);
}

@ApiOperation(value="根据姓名查找!",notes="就是查询姓名!",code=1,produces="2")
@RequestMapping(value="/findByname/{name}",method=RequestMethod.GET)
public List findByName(@PathVariable("name") final String name){
return userepository.findByAddressLike(name);
}
     
}



4.实体类

@Entity
public class Users {
@Id
@GeneratedValue
private Integer id;
private String name;
@NotBlank(message="性别必填")
private String sex;
@NotBlank(message="地址必填")
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", sex=" + sex + ", address=" + address + "]";
}
public Users(Integer id ,String sex,String name,String address){
this.id=id;
this.sex= sex;
this.name=name;
this.address=address;
}
public Users(){}

}


5.repository 类

public interface Userepository extends JpaRepository {
public List findByAddressLike(final String name);
}


6.util result 以及 resultUtil

public class ResultUtil {
public static Result success(Object object) {
       Result result = new Result();
       result.setCode(0);
       result.setMsg("成功");
       result.setData(object);
       return result;
   }


   public static Result success() {
       return success(null);
   }


   public static Result error(Integer code, String msg) {
       Result result = new Result();
       result.setCode(code);
       result.setMsg(msg);
       return result;
   }
}


Result 错误类

public class Result {
/** 错误码. */
    private Integer code;


    /** 提示信息. */
    private String msg;


    /** 具体的内容. */
    private T data;


    public Integer getCode() {
        return code;
    }


    public void setCode(Integer code) {
        this.code = code;
    }


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }


    public T getData() {
        return data;
    }


    public void setData(T data) {
        this.data = data;
    }
}


7.启动类


@EnableEurekaServer //服务发现
@EnableEurekaClient	
@EnableHystrixDashboard //熔断器
@SpringBootApplication
public class Application {


public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}



8. swagger配置类 好用的api工具

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //.pathMapping("/it/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.nf.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("做的一个用户CRDU的api。 - RESTful API Docs")
                .description("正在开发中...")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0")
                .build();
    }



。最后是数据库配置文件 yml 格式的

spring:
  profiles: ## 可以删除
    active: dev  ##可以删除
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/user
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update ## 有多个参数 可自行调试:create ,create-update,none
    show-sql: true


你可能感兴趣的:(菜鸟上路 Spring Boot+Spring data jpa+swagger 做的增 删 改 查)