欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

詳解SpringBoot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

本文介紹了Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作,分享給大家

在鳩江等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網(wǎng)站建設、網(wǎng)站制作 網(wǎng)站設計制作按需網(wǎng)站建設,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,高端網(wǎng)站設計,成都全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設公司,鳩江網(wǎng)站建設費用合理。

1、修改pom.xml,添加依賴庫,本文使用的是MySQL

 <dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 
<dependency> 
<groupId>mysql</groupId> 
<artifactId>mysql-connector-java</artifactId> 
</dependency> 

2、修改配置文件application.properties,配置數(shù)據(jù)源及java持久層API相關信息

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springlearn 
spring.datasource.username = root 
spring.datasource.password = root 
spring.datasource.driverClassName = com.mysql.jdbc.Driver 
 
 
# 配置數(shù)據(jù)庫 
spring.jpa.database = MYSQL 
# 查詢時是否顯示日志 
spring.jpa.show-sql = true 
# Hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 
# Naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 
# stripped before adding them to the entity manager) 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

3、添加數(shù)據(jù)模型 UserInfo.java

package com.xiaofangtech.sunt.bean; 
 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
 
@Entity 
@Table(name="t_user") 
public class UserInfo { 
  @Id 
  @GeneratedValue(strategy = GenerationType.AUTO) 
  private int id; 
  @NotNull 
  private String name; 
   
  private String password; 
   
  private String salt; 
   
  private String role; 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public String getPassword() { 
    return password; 
  } 
 
  public void setPassword(String password) { 
    this.password = password; 
  } 
 
  public String getSalt() { 
    return salt; 
  } 
 
  public void setSalt(String salt) { 
    this.salt = salt; 
  } 
 
  public String getRole() { 
    return role; 
  } 
 
  public void setRole(String role) { 
    this.role = role; 
  } 
} 

4、添加數(shù)據(jù)訪問接口類 UserInfoRepository.java

package com.xiaofangtech.sunt.repository; 
 
import java.util.List; 
 
import org.springframework.data.jpa.repository.Query; 
import org.springframework.data.repository.CrudRepository; 
 
import com.xiaofangtech.sunt.bean.UserInfo; 
 
public interface UserInfoRepository extends CrudRepository<UserInfo, Integer>{ 
  UserInfo findUserInfoById(int id); 
  List<UserInfo> findUserInfoByRole(String role); 
   
  @Query(value = "select * from t_user limit ?1", nativeQuery =true) 
  List<UserInfo> findAllUsersByCount(int count); 
} 

5、添加UserController.java,添加用戶信息的增刪改查操作

package com.xiaofangtech.sunt.controller; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.jpa.repository.Modifying; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.xiaofangtech.sunt.bean.UserInfo; 
import com.xiaofangtech.sunt.repository.UserInfoRepository; 
import com.xiaofangtech.sunt.utils.ResultMsg; 
import com.xiaofangtech.sunt.utils.ResultStatusCode; 
 
@RestController 
@RequestMapping("user") 
public class UserController { 
  @Autowired 
  private UserInfoRepository userRepositoy; 
   
  @RequestMapping("getuser") 
  public Object getUser(int id) 
  { 
    UserInfo userEntity = userRepositoy.findUserInfoById(id); 
    ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity); 
    return resultMsg; 
  } 
   
  @RequestMapping("getusers") 
  public Object getUsers(String role) 
  { 
    List<UserInfo> userEntities = userRepositoy.findUserInfoByRole(role); 
    ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntities); 
    return resultMsg; 
  } 
   
  @Modifying 
  @RequestMapping("adduser") 
  public Object addUser(@RequestBody UserInfo userEntity) 
  { 
    userRepositoy.save(userEntity); 
    ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity); 
    return resultMsg; 
  } 
   
  @Modifying 
  @RequestMapping("updateuser") 
  public Object updateUser(@RequestBody UserInfo userEntity) 
  { 
    UserInfo user = userRepositoy.findUserInfoById(userEntity.getId()); 
    if (user != null) 
    { 
      user.setName(userEntity.getName()); 
      userRepositoy.save(user); 
    } 
    ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), null); 
    return resultMsg; 
  } 
   
  @Modifying 
  @RequestMapping("deleteuser") 
  public Object deleteUser(int id) 
  { 
    userRepositoy.delete(id); 
    ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), null); 
    return resultMsg; 
  } 
} 

6、封裝返回的結(jié)果

添加ResultMsg.java

package com.xiaofangtech.sunt.utils; 
 
public class ResultMsg { 
  private int errcode; 
  private String errmsg; 
  private Object p2pdata; 
   
  public ResultMsg(int ErrCode, String ErrMsg, Object P2pData) 
  { 
    this.errcode = ErrCode; 
    this.errmsg = ErrMsg; 
    this.p2pdata = P2pData; 
  } 
  public int getErrcode() { 
    return errcode; 
  } 
  public void setErrcode(int errcode) { 
    this.errcode = errcode; 
  } 
  public String getErrmsg() { 
    return errmsg; 
  } 
  public void setErrmsg(String errmsg) { 
    this.errmsg = errmsg; 
  } 
  public Object getP2pdata() { 
    return p2pdata; 
  } 
  public void setP2pdata(Object p2pdata) { 
    this.p2pdata = p2pdata; 
  } 
} 

添加枚舉類ResultStatusCode.java

package com.xiaofangtech.sunt.utils; 
 
public enum ResultStatusCode { 
  OK(0, "OK"), 
  SYSTEM_ERR(30001, "System error"); 
   
  private int errcode; 
  private String errmsg; 
  public int getErrcode() { 
    return errcode; 
  } 
 
  public void setErrcode(int errcode) { 
    this.errcode = errcode; 
  } 
 
  public String getErrmsg() { 
    return errmsg; 
  } 
 
  public void setErrmsg(String errmsg) { 
    this.errmsg = errmsg; 
  } 
  private ResultStatusCode(int Errode, String ErrMsg) 
  { 
    this.errcode = Errode; 
    this.errmsg = ErrMsg; 
  } 
} 

7、工程整體結(jié)構(gòu)

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

8、運行測試,本文中測試使用的是user表,其中包含一些密碼等信息未做處理,這個讀者自行進行JsonIgnore處理

提供以下5個接口

http://localhost:8080/user/adduser

http://localhost:8080/user/updateuser

http://localhost:8080/user/getuser?id=13

http://localhost:8080/user/getusers?role=Manager

http://localhost:8080/user/deleteuser?id=13

測試運行結(jié)果

adduser接口

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

updateuser接口

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

getuser接口

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

9、調(diào)用以上接口時執(zhí)行數(shù)據(jù)庫操作時,會在內(nèi)部轉(zhuǎn)化為以下SQL語句

Hibernate: insert into t_user (name, password, role, salt) values (?, ?, ?, ?)


Hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.id=?
Hibernate: update t_user set name=?, password=?, role=?, salt=? where id=?


Hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.id=?


Hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.role=?


Hibernate: select userinfo0_.id as id1_0_0_, userinfo0_.name as name2_0_0_, userinfo0_.password as password3_0_0_, userinfo0_.role as role4_0_0_, userinfo0_.salt as salt5_0_0_ from t_user userinfo0_ where userinfo0_.id=?
Hibernate: delete from t_user where id=?


10、數(shù)據(jù)庫操作

JPA模塊支持將查詢字符串定義在方法名稱中

如上例中

根據(jù)id值查詢UserInfo實例

UserInfo findUserInfoById(int id);

根據(jù)role查詢UserInfo實例

List<UserInfo> findUserInfoByRole(String role);

也可以直接使用原生的數(shù)據(jù)庫語句

如下使用@Query注解

@Query(value = "select * from t_user limit ?1", nativeQuery =true)
List<UserInfo> findAllUsersByCount(int count);

11、在方法名中添加查詢字符串參考

詳解Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

本文源碼下載:http://xiazai.jb51.net/201707/yuanma/SpringRest_jb51.rar

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文題目:詳解SpringBoot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作
地址分享:http://aaarwkj.com/article34/iihdpe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、軟件開發(fā)、網(wǎng)站建設、網(wǎng)站內(nèi)鏈、小程序開發(fā)網(wǎng)頁設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
人妻中文字幕在线看粉嫩| 亚洲欧美久久一区二区三区| 国产欧美日韩精品av| 亚洲av最近在线观看| 国产激情久久久久久久久久久| 91精品国产综合久久男男| 久久精品国产亚洲成人av| 亚洲午夜天堂在线a毛片| 国产日韩精品在线视频| 日本一区二区电影在线看| 中文字幕一区日韩欧美| 日本国产一区二区三区在线观看 | 国产亚洲一区激情小说| 国产精品三级久久久| 99热成人精品热久久| 免费在线观看av日韩| 91国内精品手机在线高清| 欧美日韩性性在线观看| 欧美日韩亚洲精品综合网| 久久久久久精品国产免费 | 久久婷亚洲综合五月天| 久久伊人69日韩精品| 午夜福利视频在线观看| 人妻口爆视频一区二区三区| 黄色av网站在线免费| 亚洲国产理论片在线观看| 麻豆国产97在线精品一区| 亚洲精品丝袜成人偷拍| 国产精品免费网站在线观看| 精品免费av在线播放| 国产亚洲精品第一综合| 亚洲av资源一区二区| 每日更新中文字幕粉嫩av| 人妻av天堂综合一区| 男人的av天堂东京热| 精品在线免费视频观看| 亚洲一区二区精品偷拍| 青青草手机在线视频免费观看| 内射久久一区二区亚洲| 国产精品久久乱码综合| 亚洲奇米精品一区二区|