赞
踩
学习过程主要参考了https://blog.csdn.net/baidu_39298625/article/details/98102453
新建一个SpringBoot项目
点击next,修改项目名:
点击next,选择需要的依赖:
确认项目名称和路径后点击finish。
配置maven
(1)选择本地Maven路径(2)勾选配置文件后的选项,修改为本地Maven的配置文件,它会根据配置文件直接找到本地仓库位置
配置完成后点击下载按钮下载源文件和文档
编写文件
在templates文件夹下新建index.html文件作为初始页面
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>云会议</title>
- </head>
- <body>
- hello!
- </body>
- </html>
在com.example.study下新建controller文件夹,在controller文件夹下新建一个helloController类(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)
- package com.example.study.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class helloController {
-
- @RequestMapping("/index")
- public String sayHello(){
- return "index";
- }
- }
在resources文件夹下的application文件中配置DataSource基本信息,application文件有两种文件格式,一种以.properties为后缀,一种以.yml为后缀,两种配置方式略有差别,详情参考: https://blog.csdn.net/huang_wei_cai/article/details/100977469?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-5.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-5.control
这里选择以.properties为后缀:
- spring.datasource.name=study
- spring.datasource.url=jdbc:mysql://localhost:3306/study
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
在浏览器中输入localhost:8080,回车显示初始的index界面。到这里项目的初步搭建已经完成,下面实现一些简单的业务逻辑。
首先连接一下数据库(记得先打开MySQL服务)
SpringBoot项目大概分为四层:
(1)DAO层:包括XxxMapper.java(数据库访问接口类),XxxMapper.xml(数据库链接实现);
(2)Bean层:也叫model层,模型层,entity层,实体层,就是数据库表的映射实体类,存放POJO对象;
(3)Service层:也叫服务层,业务层,包括XxxService.java(业务接口类),XxxServiceImpl.java(业务实现类);(可以在service文件夹下新建impl文件放业务实现类,也可以把业务实现类单独放一个文件夹下)
(4)Web层:就是Controller层,实现与web前端的交互。
创建目录结构:
各文件代码展示
在application配置文件中添加MyBatis配置
- spring.datasource.name=study
- spring.datasource.url=jdbc:mysql://localhost:3306/study
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-
- mybatis.mapper-locations=classpath:mapper/*.xml
- mybatis.type-aliases-package=com.example.study.pojo
pom.xml文件配置信息
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.5.2</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>study</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>study</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>11</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <!--thymeleaf模板引擎配置-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <!--Web依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--MyBatis配置-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.2.0</version>
- </dependency>
-
- <!--MySQL数据库配置-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.25</version>
- <scope>runtime</scope>
- </dependency>
- <!--单元测试配置-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>RELEASE</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>

Pojo类,依据数据库表生成set和get方法
- package com.example.study.pojo;
-
- public class UserPojo {
- private int id;
- private String name;
- private String password;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
- }

DAO层访问数据库接口文件(<mapper>标签的namespace属性要填写 访问数据库接口类文件路径)
- <?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.example.study.mapper.UserMapper">
-
- <select id="getInfo" parameterType="String" resultType="com.example.study.pojo.UserPojo">
- SELECT * FROM user WHERE name = #{name} AND password = #{password}
- </select>
-
- </mapper>
Service层业务接口类
- package com.example.study.service;
-
- import com.example.study.pojo.UserPojo;
-
- public interface UserService {
-
- UserPojo loginIn(String name, String password);
-
- }
Service层业务实现类编写,注解@Service,注入DAO
- package com.example.study.serviceImpl;
-
- import com.example.study.pojo.UserPojo;
- import com.example.study.mapper.UserMapper;
- import com.example.study.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserServiceImpl implements UserService {
-
- //将DAO注入Service层
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public UserPojo loginIn(String name, String password) {
- return userMapper.getInfo(name,password);
- }
- }

项目启动类,添加注解@MapperScan项目启动时扫描mapper接口
- package com.example.study;
-
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @MapperScan("com.example.study.mapper")
- public class StudyApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(StudyApplication.class, args);
- }
-
- }
编写测试类
- package com.example.study;
-
- import org.junit.Test;
- import com.example.study.pojo.UserPojo;
- import com.example.study.service.UserService;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class StudyApplicationTests {
-
- @Autowired
- UserService userService;
-
- @Test
- public void contextLoads() {
- UserPojo userPojo = userService.loginIn("二","222");
- System.out.print("该用户ID为:");
- System.out.println(userPojo.getId());
- }
-
- }

Controller层,添加@controller注解,注入Service服务
- package com.example.study.controller;
-
- import com.example.study.pojo.UserPojo;
- import com.example.study.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- @Controller
- public class LoginController {
-
- //将Service注入Web层
- @Autowired
- UserService userService;
-
- @RequestMapping("/login")
- public String show(){
- return "login";
- }
-
- @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
- public String login(String name,String password){
- UserPojo userPojo = userService.loginIn(name,password);
- if (userPojo != null){
- return "success";
- }else {
- return "error";
- }
- }
-
- }

html文件:
(1)login.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>登录</title>
- </head>
- <body>
- <form role="form" action="/loginIn" method="post">
- 账号:<input type="text" id="name" name="name"> <br>
- 密码:<input type="password" id="passwword" name="password"> <br>
- <input type="submit" id="login" value="login">
- </form>
- </body>
- </html>
(2)success.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>登录成功</title>
- </head>
- <body>
- <h1>登录成功!</h1>
- </body>
- </html>
(3)error.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>登录失败</title>
- </head>
- <body>
- <h1>登录失败!</h1>
- </body>
- </html>
运行测试类
运行结果
但有一条WARN:
2021-06-30 08:49:12.767 WARN 1840 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
将 import org.junit.jupiter.api.Test; 修改为 import org.junit.Test; 即可
运行TestApplication.java文件,启动项目,在浏览器输入localhost:8080/login
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。