微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot连接MySQL数据库

上篇 已经构建了一个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'以后一切正常。
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.

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 :

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐