SpringBoot实践之---JDBC连接数据库
2018-02-05 16:48
405 查看
JDBC 连接数据库
1、属性配置文件(application.properties)
如果使用JNDI,则可以替代 spring.datasource 的 url、username、password,如:
spring.datasource.jndi-name=java:tomcat/datasources/example
1
值得一提的是,无论是Spring Boot默认的DataSource配置还是你自己的DataSource bean,都会引用到外部属性文件中的属性配置。所以假设你自定义的DataSource bean,你可以在定义bean时设置属性,也可以在属性文件中,以“spring.datasource.*”的方式使属性配置外部化。
2、pom.xml 配置maven依赖
3、Java代码范例
Student.java 实体类
StudentController.java
本文对工程添加文件后工程结构图:
这里写图片描述
然后启动项目,访问地址: http://localhost:8080/myspringboot/stu/list 响应结果如下:
[
{
id: 1,
name: “小明”,
sumScore: “252”,
avgScore: “84”,
age: 1
},
{
id: 2,
name: “小王”,
sumScore: “187”,
avgScore: “62.3”,
age: 1
},
{
id: 3,
name: “莉莉”,
sumScore: “”,
avgScore: “”,
age: 0
},
{
id: 4,
name: “柱子”,
sumScore: “230”,
avgScore: “76.7”,
age: 1
},
{
id: 5,
name: “大毛”,
sumScore: “”,
avgScore: “”,
age: 0
},
{
id: 6,
name: “亮子”,
sumScore: “0”,
avgScore: “0”,
age: 1
}
]
1、属性配置文件(application.properties)
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
如果使用JNDI,则可以替代 spring.datasource 的 url、username、password,如:
spring.datasource.jndi-name=java:tomcat/datasources/example
1
值得一提的是,无论是Spring Boot默认的DataSource配置还是你自己的DataSource bean,都会引用到外部属性文件中的属性配置。所以假设你自定义的DataSource bean,你可以在定义bean时设置属性,也可以在属性文件中,以“spring.datasource.*”的方式使属性配置外部化。
2、pom.xml 配置maven依赖
<!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring Boot JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
3、Java代码范例
StudentService.java package org.springboot.sample.service; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springboot.sample.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; /** * Studeng Service * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年1月12日 */ @Service public class StudentService { @Autowired private JdbcTemplate jdbcTemplate; public List<Student> getList(){ String sql = "SELECT ID,NAME,SCORE_SUM,SCORE_AVG, AGE FROM STUDENT"; return (List<Student>) jdbcTemplate.query(sql, new RowMapper<Student>(){ @Override public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student stu = new Student(); stu.setId(rs.getInt("ID")); stu.setAge(rs.getInt("AGE")); stu.setName(rs.getString("NAME")); stu.setSumScore(rs.getString("SCORE_SUM")); stu.setAvgScore(rs.getString("SCORE_AVG")); return stu; } }); } }
Student.java 实体类
package org.springboot.sample.entity; import java.io.Serializable; /** * 学生实体 * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年1月12日 */ public class Student implements Serializable{ private static final long serialVersionUID = 2120869894112984147L; private int id; private String name; private String sumScore; private String avgScore; private int age; // 节省文章长度,get set 方法省略 }
StudentController.java
package org.springboot.sample.controller; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springboot.sample.entity.Student; import org.springboot.sample.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/stu") public class StudentController { private static final Logger logger = LoggerFactory.getLogger(StudentController.class); @Autowired private StudentService studentService; @RequestMapping("/list") public List<Student> getStus(){ logger.info("从数据库读取Student集合"); return studentService.getList(); } }
本文对工程添加文件后工程结构图:
这里写图片描述
然后启动项目,访问地址: http://localhost:8080/myspringboot/stu/list 响应结果如下:
[
{
id: 1,
name: “小明”,
sumScore: “252”,
avgScore: “84”,
age: 1
},
{
id: 2,
name: “小王”,
sumScore: “187”,
avgScore: “62.3”,
age: 1
},
{
id: 3,
name: “莉莉”,
sumScore: “”,
avgScore: “”,
age: 0
},
{
id: 4,
name: “柱子”,
sumScore: “230”,
avgScore: “76.7”,
age: 1
},
{
id: 5,
name: “大毛”,
sumScore: “”,
avgScore: “”,
age: 0
},
{
id: 6,
name: “亮子”,
sumScore: “0”,
avgScore: “0”,
age: 1
}
]
相关文章推荐
- Spring Boot JDBC 连接数据库
- Spring Boot JDBC 连接数据库
- Spring Boot JDBC 连接数据库示例
- SpringBoot实践之---Mybatis连接数据库(mybatis-generator-plugin)
- Spring Boot JDBC 连接数据库
- SpringBoot实践之---JPA连接数据库+idea全新创建该工程
- SpringBoot - JDBC 连接数据库
- Spring Boot JDBC 连接数据库
- SpringBoot实践之---Hibernate·连接数据库
- 第一个 Spring Boot 程序 : 使用 spring jdbc 访问关系型数据库
- 二、spring Boot构建的Web应用中,基于MySQL数据库的几种数据库连接方式进行介绍
- SpringBoot 连接数据库
- springboot 多数据源(三种数据库连接池--JDBC,dbcp2,Druid)
- IDE maven 创建spring boot 用hibernate 实现连接数据库
- Spring boot 数据库连接断线重连问题
- springboot jdbc链接数据库
- Spring中使用纯JDBC连接数据库的配置
- SpringBoot开发详解(四)--JDBC操作数据库
- 利用springboot使用JdbcTemplate连接数据库
- Spring Boot JPA 连接数据库