前言
本文是对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 yangyang@dalaoyang.cn * @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 yangyang@dalaoyang.cn * @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方法
具体使用方法还有很多,请参考文档:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/jdbctemplate.html
注意
出现下图错误不要担心,如图
出现这个错误是因为sql在参数问号的时候多写了引号造成的,这也是我在写demo的时候犯下的错误。
源码下载 : 大老杨码云
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://juejin.im/post/5b583c9de51d451984699900
查看更多关于SpringBoot使用JdbcTemplate操作数据库的详细内容...