前言
本文是對springboot使用jdbctemplate操作數據庫的一個介紹,提供一個小的demo供大家參考。
操作數據庫的方式有很多,本文介紹使用springboot結合jdbctemplate。
新建項目
新建一個項目。pom文件中加入jdbc依賴,完整pom如下:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.dalaoyang</groupid> <artifactid>springboot_jdbc</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>springboot_jdbc</name> <description>springboot_jdbc</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
##端口號 server.port= 8888 ##數據庫配置 ##數據庫地址 spring.datasource.url=jdbc:mysql: //localhost:3306/test?characterencoding=utf8&usessl=false ##數據庫用戶名 spring.datasource.username=root ##數據庫密碼 spring.datasource.password= 123456 ##數據庫驅動 spring.datasource.driver- class -name=com.mysql.jdbc.driver |
新建一個實體類user,其中需要注意的是,user類實現了rowmapper類,重寫了maprow方法,完整代碼如下:
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
|
package com.dalaoyang.entity; import org.springframework.jdbc.core.rowmapper; import java.sql.resultset; import java.sql.sqlexception; /** * @author dalaoyang * @project springboot_learn * @package com.dalaoyang.entity * @email [email protected] * @date 2018/7/25 */ public class user implements rowmapper<user> { private int id; private string user_name; private string pass_word; public user( int id, string user_name, string pass_word) { this .id = id; this .user_name = user_name; this .pass_word = pass_word; } public user() { } public user(string user_name, string pass_word) { this .user_name = user_name; this .pass_word = pass_word; } public int getid() { return id; } public void setid( int id) { this .id = id; } public string getuser_name() { return user_name; } public void setuser_name(string user_name) { this .user_name = user_name; } public string getpass_word() { return pass_word; } public void setpass_word(string pass_word) { this .pass_word = pass_word; } @override public user maprow(resultset resultset, int i) throws sqlexception { user user = new user(); user.setid(resultset.getint( "id" )); user.setuser_name(resultset.getstring( "user_name" )); user.setpass_word(resultset.getstring( "pass_word" )); return user; } } |
常用curd操作大致使用以下三個方法:
1.execute方法,用于直接執行sql語句
2.update方法,用戶新增修改刪除操作
3.query方法,用于查詢方法
本文和往常一樣,用controller進行測試,注入jdbctemplate。完整代碼如下,下面會對測試方法進行介紹:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package com.dalaoyang.controller; import com.dalaoyang.entity.user; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; import java.util.arraylist; import java.util.list; import java.util.map; /** * @author dalaoyang * @project springboot_learn * @package com.dalaoyang.controller * @email [email protected] * @date 2018/7/25 */ @restcontroller public class usercontroller { @autowired private jdbctemplate jdbctemplate; //http://localhost:8888/createtable @getmapping ( "createtable" ) public string createtable(){ string sql = "create table `user` (\n" + " `id` int(11) not null auto_increment,\n" + " `user_name` varchar(255) default null,\n" + " `pass_word` varchar(255) default null,\n" + " primary key (`id`)\n" + ") engine=innodb auto_increment=7 default charset=utf8;\n" + "\n" ; jdbctemplate.execute(sql); return "創建user表成功" ; } //http://localhost:8888/saveusersql @getmapping ( "saveusersql" ) public string saveusersql(){ string sql = "insert into user (user_name,pass_word) values ('dalaoyang','123')" ; int rows= jdbctemplate.update(sql); return "執行成功,影響" +rows+ "行" ; } //http://localhost:8888/saveuser?username=lisi&password=111 @getmapping ( "saveuser" ) public string saveuser(string username,string password){ int rows= jdbctemplate.update( "insert into user (user_name,pass_word) values (?,?)" ,username,password); return "執行成功,影響" +rows+ "行" ; } //http://localhost:8888/updateuserpassword?id=1&password=111 @getmapping ( "updateuserpassword" ) public string updateuserpassword( int id,string password){ int rows= jdbctemplate.update( "update user set pass_word = ? where id = ?" ,password,id); return "執行成功,影響" +rows+ "行" ; } //http://localhost:8888/deleteuserbyid?id=1 @getmapping ( "deleteuserbyid" ) public string deleteuserbyid( int id){ int rows= jdbctemplate.update( "delete from user where id = ?" ,id); return "執行成功,影響" +rows+ "行" ; } //http://localhost:8888/batchsaveusersql @getmapping ( "batchsaveusersql" ) public string batchsaveusersql(){ string sql = "insert into user (user_name,pass_word) values (?,?)" ; list<object[]> paramlist = new arraylist<>(); for ( int i = 0 ; i < 10 ; i++) { string[] arr = new string[ 2 ]; arr[ 0 ] = "zhangsan" +i; arr[ 1 ] = "password" +i; paramlist.add(arr); } jdbctemplate.batchupdate(sql,paramlist); return "執行成功" ; } //http://localhost:8888/getuserbyusername?username=zhangsan0 @getmapping ( "getuserbyusername" ) public list getuserbyusername(string username){ string sql = "select * from user where user_name = ?" ; //寫法很多種 //下面列舉兩種寫法,都可以實現 //list<user> list= jdbctemplate.query(sql,new object[]{username}, new beanpropertyrowmapper(user.class)); list<user> list= jdbctemplate.query(sql, new user(), new object[]{username}); return list; } //http://localhost:8888/getmapbyid?id=1 @getmapping ( "getmapbyid" ) public map getmapbyid(integer id){ string sql = "select * from user where id = ?" ; map map= jdbctemplate.queryformap(sql,id); return map; } //http://localhost:8888/getuserbyid?id=1 @getmapping ( "getuserbyid" ) public user getuserbyid(integer id){ string sql = "select * from user where id = ?" ; user user= jdbctemplate.queryforobject(sql, new user(), new object[]{id}); return user; } } |
測試方法介紹
1.createtable方法
使用execute方法創建user表
2.saveusersql方法
使用update方法,傳入參數sql語句,直接執行插入操作
3.saveuser方法
使用update方法,傳入sql語句和對應字段值,進行插入操作
4.updateuserpassword方法
使用update方法,傳入sql語句和對應字段值,進行修改操作
5.deleteuserbyid方法
使用update方法,傳入sql語句和對應字段值,進行刪除操作
6.batchsaveusersql方法
使用batchupdate方法,傳入sql和參數集合,進行批量更新
7.getuserbyusername方法
使用query方法,傳入sql,實體對象,查詢參數,這里就用到了實體類重寫的maprow方法
8.getmapbyid方法
使用queryformap方法,傳入sql和參數,返回map
9.getuserbyid方法
使用queryforobject方法,傳入sql,實體對象,查詢參數,返回user實體類,這里也用到了實體類重寫的maprow方法
具體使用方法還有很多,請參考文檔:
注意
出現下圖錯誤不要擔心,如圖
出現這個錯誤是因為sql在參數問號的時候多寫了引號造成的,這也是我在寫demo的時候犯下的錯誤。
源碼下載 :大老楊碼云
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://juejin.im/post/5b583c9de51d451984699900