springboot如何集成mysql
作者:贪吃小明
这篇文章主要介绍了springboot如何集成mysql问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
springboot集成mysql
引入依赖
在pom文件中加入如下依赖:
<!--集成mysql数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
添加数据库配置
在spring boot项目的配置文件 application.properties
中添加如下配置:
#集成mysql数据库的配置 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/studentscon?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root
设计表和实体类
实体类对应数据库中的表结构
package com.test.springbootstudy.entity; public class Student { private int id; private String name; private String password; private String sex; private int age; public Student() { } public Student(int id, String name, String password, String sex, int age) { this.id = id; this.name = name; this.password = password; this.sex = sex; this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试
@Resource // 自动注入,spring boot会帮我们实例化一个对象 private JdbcTemplate jdbcTemplate; // 一个通过JDBC连接数据库的工具类,可以通过这个工具类对数据库进行增删改查 @Test public void mySqlTest(){ String sql = "select id,name,password,sex,age from students"; List<Student> students = jdbcTemplate.query(sql, new RowMapper<Student>() { @Override public Student mapRow(ResultSet resultSet, int i) throws SQLException { Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setPassword(resultSet.getString("password")); student.setSex(resultSet.getString("sex")); student.setAge(resultSet.getInt("age")); return student; } }); System.out.println("查询成功"); for(Student s : students){ System.out.println(s); } }
测试结果如下:
springboot集成mysql时报错
在使用springboot默认的mysql和mybatis时踩了一个坑,特此记录
首先,新版的mysql连接驱动要用:
com.mysql.cj.jdbc.Driver
启动后并未报错,但是访问的时候报以下错误:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
查询原因,是由于安装mysql的时候安装时区不对
解决方法如下
1.在database的url后面拼上"?serverTimezone=GMT%2B8",寓意为设置服务器时区为gmt+8;
2.修改安装文件中的my.ini,在mysqld节点下加上default-time-zone='+08:00'
修改完成后记得重启服务。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。