SpringBoot 初次配置数据库 并 进行第一次业务查询

2024-01-08 18:33:48

第1步:创建表 并 添加数据

创建表

CREATE TABLE `students` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT '',
  `password` varchar(255) DEFAULT NULL,
  `age` tinyint unsigned DEFAULT '0',
  `height` decimal(5,2) DEFAULT NULL,
  `gender` enum('男','女','中性','保密') DEFAULT '保密',
  `class_id` int unsigned DEFAULT '0',
  `is_delete` bit(1) DEFAULT b'0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1051 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

添加数据

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
[Ref] mysql创建数据库,创建数据表,并且插入数据

第2步:构造Model

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String username;
    private String password;
    private int age;
    private int height;
    private String gender;
    private int classId;
    private boolean logicDelete;
}

第3步:数据访问层 (Dao层)(Repository层)

com.practisesvr.mapper.UserMapper

@Repository
public interface UserMapper {
    Student getInfo(@Param("name") String name, @Param("password") String password);
}

com.practisesvr.mapper.UserMapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.practisesvr.mapper.UserMapper">

    <select id="getInfo" parameterType="String" resultType="com.practisesvr.model.Student">
        SELECT *
        FROM students
        WHERE username = #{name} and password = #{password}
    </select>

</mapper>

src/main/resources/application.yml

扫描 xml

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*.xml  #配置映射文件

com.zhangziwa.practisesvr.PractisesvrApplication

扫描接口

@SpringBootApplication
@MapperScan("com.practisesvr.mapper")
public class PractisesvrApplication {
    public static void main(String[] args) {
        SpringApplication.run(PractisesvrApplication.class, args);
    }
}

第4步:业务层(service层)

@Service
public interface UserService {
    Student loginIn(String name, String password);
}

@Component
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Student loginIn(String name, String password) {
        log.info("input name {}, password {}", name, password);
        if (ObjectUtils.anyNull(name, password)) {
            log.warn("input name {}, password {}", name, password);
            return null;
        }
        return userMapper.getInfo(name, password);
    }
}

第5层:控制层(Controller层)

@Controller
@Slf4j
public class LoginController {

    @Autowired
    UserService userService;

    // 为了返回登录页面
    @RequestMapping("/login")
    public String show() {
        return "login";
    }

    @RequestMapping(value = "/loginIn", method = RequestMethod.POST)
    public String login(String name, String password) {
        Student student = userService.loginIn(name, password);
        log.info("student {}", student);
        if (student != null) {
            return "success";
        } else {
            return "error";
        }
    }
}

第6步:前端登录页面

登录页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form role="form" action="/loginIn" method="post">
    账号:<input type="text" id="name" name="name"> <br>
    密码:<input type="password" id="password" name="password"> <br>
    <input type="submit" id="login" value="login">
</form>

</body>
</html>

成功页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>

失败页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>error</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>

springmvc 配置

spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  web:
    resources:
      static-locations: classpath:/templates/,classpath:/static/

第7步:数据库配置

src/main/resources/application.yml

spring:
  datasource:
    name: practisedb  #数据库名
    url: jdbc:mysql://localhost:3306/practisedb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
    #url
    username: root  #用户名
    password: 123456  #密码
    driver-class-name: com.mysql.cj.jdbc.Driver  #数据库链接驱动

第8步:maven依赖一览

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>3.0.0</version>
 </dependency>
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>8.0.18</version>
     <scope>runtime</scope>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
 </dependency>

第9步:启动服务

在这里插入图片描述

第10步:登录演示

登录页面

在这里插入图片描述

登录成功

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

登录失败

在这里插入图片描述

参考

使用IDEA搭建一个简单的SpringBoot项目——详细过程

文章来源:https://blog.csdn.net/weixin_37646636/article/details/135428566
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。