eclipse如何连接mysql_Spring Boot如何连接MySQL数据库详解!

1. pom.xml添加依赖

        org.springframework.bootspring-boot-starter-data-jpamysqlmysql-connector-java   

2. application.properties添加数据库配置

spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true

如果数据库连接写成spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot ,由于MySQL版本的问题,可能会有以下的错误,在后面加上“?serverTimezone=GMT%2B8”,设置下时区,解决。

d4a37bc092ed9bea3e0e2c342c140d5c.png

设置驱动,spring.datasource.driver-class-name=com.mysql.jdbc.Driver会有下面红色的警告信息。说的是com.mysql.jdbc.Driver被弃用了,要使用新的驱动com.mysql.cj.jdbc.Driver1,改成com.mysql.cj.jdbc.Driver以后一切正常。

Loading class com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

167574c7879feaeddcd105ce81ad4086.png

3. 添加实体类

@Entity代表这是一个实体类,@Table(name=”user”)用来对应数据库中的use表,@Id用来表达主键,@Column(name=”id”)表明一个id属性。

@GeneratedValue使主键自增,如果还有疑问,可参考@GeneratedValue源码解析。

package com.example.demo.domain;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "user")public class User implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValueprivate Long id;@Column(name = "username")private String userName;@Column(name = "password")private String passWord;public User() {super();}public User(String userName, String passWord) {super();this.userName = userName;this.passWord = passWord;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}}

4. 添加Dao

Dao层主要用来实现对数据库的增、删、查、改。 dao只要继承JpaRepository类就可以,几乎可以不用写方法,可以根据方法名来自动的生产SQL,比如findByUserName 会自动生产一个以 userName 为参数的查询方法。

package com.example.demo.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.example.demo.domain.User;public interface UserRepository extends JpaRepository<User, Long> {User findByUserName(String userName);}

5. 添加Controller

package com.example.demo.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import com.example.demo.dao.UserRepository;import com.example.demo.domain.User;@RestController@RequestMapping("user")public class UserController {@Autowiredprivate UserRepository userRepository;@RequestMapping("/getAllUser")@ResponseBodypublic List<User> findAll() {List<User> list = new ArrayList<User>();
list = userRepository.findAll();return list;}@RequestMapping("/getByUserName")@ResponseBodypublic User getByUserName(String userName) {User user = userRepository.findByUserName(userName);return user;}}

工程添加文件后工程结构图:

98df810c783f38eb0a50265f0bf911bf.png

6. 新建数据库

新建数据库mysql://localhost:3306/spring_boot ,必须的一个步骤。hibernate虽然会自动新建表,但是数据库还是要手动建好的。

使用Navicat新建本地数据库,连接名上面右键- >新建数据库 ->填写数据库信息 – > 确定。

05e9411463e8dba741f4720962452e7d.png

在user表中,插入两条测试数据:

700e498f5d0e83d99b61bd082aa9ca67.png

7. 测试

启动项目。用Postman发送请求进行测试:

http://localhost:8080//user/getAllUser :

e16e967da15ca090e428c44382fe247a.png

http://localhost:8080//user/getByUserName?userName=Turing :

5a04b5e5d96f08e7a205153daef52b06.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中使用EclipseMySQL数据库连接并存储图片,可以按照以下步骤进行操作: 1. 首先,需要在MySQL数据库中创建一个表来存储图片。表的结构可以如下所示: ``` CREATE TABLE `image_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `image` longblob NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 2. 在Java中使用JDBC连接MySQL数据库,并编写代码来插入图片到数据库中。代码示例: ``` String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; String insertSQL = "INSERT INTO image_table (name, image) VALUES (?, ?)"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement ps = conn.prepareStatement(insertSQL)) { File imageFile = new File("path/to/image.jpg"); String imageName = imageFile.getName(); InputStream is = new FileInputStream(imageFile); ps.setString(1, imageName); ps.setBinaryStream(2, is, imageFile.length()); int rowsInserted = ps.executeUpdate(); if (rowsInserted > 0) { System.out.println("Image inserted successfully!"); } } catch (SQLException | IOException e) { e.printStackTrace(); } ``` 3. 使用Java和JDBC从数据库中取出图片,并将其保存到本地。代码示例: ``` String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; String selectSQL = "SELECT image FROM image_table WHERE id = ?"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement ps = conn.prepareStatement(selectSQL)) { int id = 1; ps.setInt(1, id); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { Blob imageBlob = rs.getBlob("image"); InputStream is = imageBlob.getBinaryStream(); File outputFile = new File("path/to/output.jpg"); try (OutputStream os = new FileOutputStream(outputFile)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } System.out.println("Image retrieved successfully!"); } else { System.out.println("No image found with ID " + id); } } } catch (SQLException | IOException e) { e.printStackTrace(); } ``` 以上就是使用Java和Eclipse连接MySQL数据库,并存储图片的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值