最近在学习使用Spring Boot连接数据库,今天学习了使用jdbctemplate 操作MYSQL数据库,下面就留个笔记
不废话,先来代码
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
66
67
68
69
70
71
|
< 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 >test</ groupId >
< artifactId >test</ artifactId >
< version >0.0.1-SNAPSHOT</ version >
< packaging >jar</ packaging >
< name >test</ name >
< url >http://maven.apache.org</ url >
< properties >
< project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
</ properties >
< dependencies >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter</ artifactId >
< version >1.4.2.RELEASE</ version >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-jdbc</ artifactId >
< version >1.4.2.RELEASE</ version >
</ dependency >
< dependency >
< groupId >mysql</ groupId >
< artifactId >mysql-connector-java</ artifactId >
< version >5.1.21</ version >
</ dependency >
</ dependencies >
</ project >
|
配置文件:application.properties(springboot框架默认使用这个名字,放在resources下面)
1
2
3
4
5
6
7
8
9
10
11
|
spring.datasource.url=jdbc:mysql://localhost:3306/service_lucky_draw?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.application.name = @pom.artifactId@
server.port=33333
|
启动类:
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
|
package versionUpdate;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
public class ApplicationMain implements CommandLineRunner {
private Logger log = Logger.getLogger(ApplicationMain. class );
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(ApplicationMain. class );
springApplication.run(args);
}
@Override
public void run(String... args) throws Exception {
String queryMerchandiseInfoSql = "SELECT id,worth,channel_id,template_id FROM merchandise_info" ;
List<Map<String, Object>> list = jdbcTemplate.queryForList(queryMerchandiseInfoSql);
log.debug(list);
}
}
|
至此一个简单的SpringBoot+Jdbctemplate+MYSQL的DEMO搭建完成;
如果不想在启动类里面直接进行数据库操作,可以按照下面的方式:
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
|
package versionUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
/** 获取jdbctemplate实例 */
@Component
public class EnterJdbcTemplate {
private static JdbcTemplate jdbcTemplate;
@Autowired
public EnterJdbcTemplate(JdbcTemplate jdbcTemplate) {
this .jdbcTemplate = jdbcTemplate;
}
public static JdbcTemplate getJdbcTemplate(){
return jdbcTemplate;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package versionUpdate;
import org.springframework.jdbc.core.JdbcTemplate;
/** 操作数据库 */
public class Movedata extends EnterJdbcTemplate{
public Movedata(JdbcTemplate jdbcTemplate) {
super (jdbcTemplate);
}
public static void ccc(){
System.out.println( "++++++++++++++++++" +getJdbcTemplate().queryForMap( "SELECT * FROM channel_info WHERE channel_id = ? " , "cccc" ));
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/ning-blogs/p/6078703.html