本文介紹了spring boot實戰之rest接口開發及數據庫基本操作,分享給大家
1、修改pom.xml,添加依賴庫,本文使用的是mysql
1
2
3
4
5
6
7
8
|
<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,配置數據源及java持久層api相關信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 # 配置數據庫 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、添加數據模型 userinfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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、添加數據訪問接口類 userinforepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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,添加用戶信息的增刪改查操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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、封裝返回的結果
添加resultmsg.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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、工程整體結構
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
測試運行結果
adduser接口
updateuser接口
getuser接口
9、調用以上接口時執行數據庫操作時,會在內部轉化為以下sql語句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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、數據庫操作
jpa模塊支持將查詢字符串定義在方法名稱中
如上例中
根據id值查詢userinfo實例
1
|
userinfo finduserinfobyid( int id); |
根據role查詢userinfo實例
1
|
list<userinfo> finduserinfobyrole(string role); |
也可以直接使用原生的數據庫語句
如下使用@query注解
1
2
|
@query (value = "select * from t_user limit ?1" , nativequery = true ) list<userinfo> findallusersbycount( int count); |
11、在方法名中添加查詢字符串參考
本文源碼下載:springrest.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/sun_t89/article/details/51912905