SpringBoot uses JDBC to connect to Mysql database
There are many ways for Spring to connect to Mysql, such as JDBC, Spring JPA, Hibeirnate, Mybatis, etc. This article mainly introduces the simplest and lowest-level JDBC way to connect to Mysql database, JDBC connects to the database, mainly injecting JdbcTemplate and using JdbcTemplate to operate the database .
1. Create a user table in the test library in mysql, and insert two pieces of data to prepare for the follow-up
The table statement is as follows:
CREATE TABLE `user` (
`id` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`age` varchar(20) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `user`(`id`, `name`, `age`, `sex`) VALUES ('1', 'oyc', '18', 'male');
INSERT INTO `user`(`id`, `name`, `age`, `sex`) VALUES ('2', 'ouyang', '19', 'male');
Two, add dependency in pom.xml
<!--JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Three, add configuration file configuration database and other parameters
Add the application.properties configuration file under the resource folder and enter the database parameters, as follows:
############################################################
#
# mysql
#
############################################################
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
4. New entity class User.java, with attributes corresponding to the database user table
package com.oycbest.jdbcdemo.domain;
/**
* @author oyc
* @Description:用户实体类
* @date 2018/7/8 22:51
*/
public class User{
//用户id
private String id;
//用户名称
private String name;
//户年龄
private String age;
//用户性别
private String sex;
//此处省略setter和getter
}
Five, create a new test class to connect to the database
/**
* @author oyc
* @Description: 用户控制类
* @date 2018/7/8 22:10
*/
@Controller
@RequestMapping("/jdbc")
public class JdbcController {
@Resource
private JdbcTemplate jdbcTemplate;
@RequestMapping("/user")
@ResponseBody
public List<User> list(ModelMap map) {
String sql = "SELECT * FROM user";
List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {
User user = null;
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getString("age"));
return user;
}
});
for (User user : userList) {
System.out.println(user.toString());
}
return userList;
}
@RequestMapping("/userList")
public String userList(ModelMap map) {
String sql = "SELECT * FROM user";
List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {
User user = null;
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getString("age"));
return user;
}
});
map.addAttribute("users", userList);
return "user";
}
}
6. Add a new thymeleaf template page user.html to display a list of user information
Seven, use the browser to test
Source code address: https://github.com/oycyqr/jdbcdemo/