加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Spring Boot连接MySQL数据库

发布时间:2020-12-15 01:52:55 所属栏目:大数据 来源:网络整理
导读:上篇 已经构建了一个Spring Boot项目,本文在此基础上进行连接MySQL数据库的操作。 1. pom.xml添加依赖 org.springframework.boot spring-boot-starter-data-jpa dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId /dependency /p

上篇 已经构建了一个Spring Boot项目,本文在此基础上进行连接MySQL数据库的操作。

1. pom.xml添加依赖

org.springframework.boot spring-boot-starter-data-jpa
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>    </pre>

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

spring.datasource.url=jdbc:mysql: spring.datasource.username==123456--name=spring.jpa.properties.hibernate.hbm2ddl.auto=<span style="color: #000000;">update
spring.jpa.properties.hibernate.dialect
=<span style="color: #000000;">org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show
-sql= <span style="color: #0000ff;">true

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

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

3. 添加实体类

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

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

<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.Serializable;

<span style="color: #0000ff;">import<span style="color: #000000;"> javax.persistence.Column;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.persistence.Entity;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.persistence.GeneratedValue;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.persistence.Id;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.persistence.Table;

@Entity
@Table(name = "user"<span style="color: #000000;">)
<span style="color: #0000ff;">public <span style="color: #0000ff;">class User <span style="color: #0000ff;">implements<span style="color: #000000;"> Serializable {

</span><span style="color: #0000ff;"&gt;private</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;final</span> <span style="color: #0000ff;"&gt;long</span> serialVersionUID = 1L<span style="color: #000000;"&gt;;

@Id
@GeneratedValue
</span><span style="color: #0000ff;"&gt;private</span><span style="color: #000000;"&gt; Long id;
@Column(name </span>= "username"<span style="color: #000000;"&gt;)
</span><span style="color: #0000ff;"&gt;private</span><span style="color: #000000;"&gt; String userName;
@Column(name </span>= "password"<span style="color: #000000;"&gt;)
</span><span style="color: #0000ff;"&gt;private</span><span style="color: #000000;"&gt; String passWord;

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; User() {
    </span><span style="color: #0000ff;"&gt;super</span><span style="color: #000000;"&gt;();
}

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; User(String userName,String passWord) {
    </span><span style="color: #0000ff;"&gt;super</span><span style="color: #000000;"&gt;();
    </span><span style="color: #0000ff;"&gt;this</span>.userName =<span style="color: #000000;"&gt; userName;
    </span><span style="color: #0000ff;"&gt;this</span>.passWord =<span style="color: #000000;"&gt; passWord;
}

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; Long getId() {
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; id;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; setId(Long id) {
    </span><span style="color: #0000ff;"&gt;this</span>.id =<span style="color: #000000;"&gt; id;
}

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; String getUserName() {
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; userName;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; setUserName(String userName) {
    </span><span style="color: #0000ff;"&gt;this</span>.userName =<span style="color: #000000;"&gt; userName;
}

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; String getPassWord() {
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; passWord;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; setPassWord(String passWord) {
    </span><span style="color: #0000ff;"&gt;this</span>.passWord =<span style="color: #000000;"&gt; passWord;
}

}

4. 添加Dao

Dao层主要用来实现对数据库的增、删、查、改。?dao只要继承JpaRepository类就可以,几乎可以不用写方法,可以根据方法名来自动的生产SQL,比如findByUserName?会自动生产一个以?userName?为参数的查询方法。
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.data.jpa.repository.JpaRepository;

<span style="color: #0000ff;">import<span style="color: #000000;"> com.example.demo.domain.User;

<span style="color: #0000ff;">public <span style="color: #0000ff;">interface UserRepository <span style="color: #0000ff;">extends JpaRepository<User,Long><span style="color: #000000;"> {

User findByUserName(String userName);

}

5. 添加Controller

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.ArrayList;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.List;

<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.beans.factory.annotation.Autowired;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.RequestMapping;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.ResponseBody;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.RestController;

<span style="color: #0000ff;">import<span style="color: #000000;"> com.example.demo.dao.UserRepository;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.example.demo.domain.User;

@RestController
@RequestMapping("user"<span style="color: #000000;">)
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> UserController {

@Autowired
</span><span style="color: #0000ff;"&gt;private</span><span style="color: #000000;"&gt; UserRepository userRepository;

@RequestMapping(</span>"/getAllUser"<span style="color: #000000;"&gt;)
@ResponseBody
</span><span style="color: #0000ff;"&gt;public</span> List<User><span style="color: #000000;"&gt; findAll() {
    List</span><User> list = <span style="color: #0000ff;"&gt;new</span> ArrayList<User><span style="color: #000000;"&gt;();
    list </span>=<span style="color: #000000;"&gt; userRepository.findAll();
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; list;
}

@RequestMapping(</span>"/getByUserName"<span style="color: #000000;"&gt;)
@ResponseBody
</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; User getByUserName(String userName) {
    User user </span>=<span style="color: #000000;"&gt; userRepository.findByUserName(userName);
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; user;
}

}

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

6. 新建数据库

新建数据库?,必须的一个步骤。hibernate虽然会自动新建表,但是数据库还是要手动建好的。
使用Navicat新建本地数据库,连接名上面右键- >新建数据库 ->填写数据库信息 - > 确定。

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

7. 测试

启动项目。用Postman发送请求进行测试:
http://localhost:8080//user/getAllUser :

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读