菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
27
0

spring Boot 简单的登录功能,利用了jdbcTemplate.class完成sql语句的执行,无需service层、dao层和.xml文件

原创
05/13 14:22
阅读数 40819

1、搭建SpringBoot项目
首先我们先在IDEA上创建一个SpringBoot的Web项目
(1)file ——> new ——> project——> Spring Initializr

(2)——> next

这里是包路径前缀,可修改也可不修改(默认)com.example.demo
(3)——> next

这里要选择包依赖关系,我们这里就先选择这一个:
(4)web:选择web
(5)——> next
后面还要再加包的话,可以在pom文件里加。

(6)——> finish
经过这6步,便已经创建了一个SpringBoot的项目。

让我们来启动一下看下是否创建成功。

可以在启动类右键Run’DemoApplication’或者点击右上方启动。
启动之后,我们可以看到控制台输出如下,并没有报错,启动成功:


2、连接Mysql数据库
将项目连接上MySql数据库
首先我们将需要的包导入,这几个包都是我们稍后要用到的:
(1) 在pom.xml依赖里加入如下:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

导入包成功后,我们可以来修改配置文件了:
(2)将application.properties文件重命名为application.yml
这个也是官方推荐的,yml文件的好处,天然的树状结构,一目了然。
修改完后。
(3)在application.yml配置你数据库的信息
下面是以我本地的数据库为例。你们操作时,把它改成你自己对应的数据库便可。

spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver

启动一下,没有报错。配置成功了。

注:默认端口号为8080,因为8080在我电脑上被其他项目占用了,所以我在application.yml改了端口号为8000。你们没改的应该是8080。

3、测试是否成功
虽然不报错连上了,但是不是那么的一目了然让我们看到是否连接成功。下面我们来写个接口来测试一下,看下能否读到数据库中的表数据。我数据库中有一个student的表,那我就写一个读取它数据的接口:

(1)建立一个controller包,在它下面创建一个TestController类

在这个类中的代码如下:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class TestController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/getUsers")
public List<Map<String, Object>> getDbType(){
String sql = "select * from student";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
Set<Map.Entry<String, Object>> entries = map.entrySet( );
if(entries != null) {
Iterator<Map.Entry<String, Object>> iterator = entries.iterator( );
while(iterator.hasNext( )) {
Map.Entry<String, Object> entry =(Map.Entry<String, Object>) iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
System.out.println(key+":"+value);
}
}
}
return list;
}
}
你们操作的话里面那句sql改成相应查找你数据库的查询语句来测,表要是你数据库中存在的表,否则是查不到的。
然后我们在浏览器端输入:
http://localhost:端口号+路径
http://localhost:8000/getUsers

然后把sql放在数据库执行一下,

查到的和接口中查到的相符。
到这一步了,我们的目的也就达到了。

结果:

 

接下来,我们可以用这个建好的项目做一个简单的登录注册功能。

发表评论

0/200
27 点赞
0 评论
收藏