一个简单的SpringBoot项目 demo
Posted Ch4536251
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的SpringBoot项目 demo相关的知识,希望对你有一定的参考价值。
架构
相关代码
templates目录下存放前端静态页面
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
SpringBoot的简单启动页面!
</body>
</html>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>
login.html
<!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>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
拦截器contronller
HelloController.java
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController
@RequestMapping("/index")
public String sayHello()
return "index";
LoginController.java
添加@controller注解,注入Service服务
package com.example.controller;
import com.example.bean.UserBean;
import com.example.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)
UserBean userBean = userService.loginIn(name,password);
if(userBean!=null)
return "success";
else
return "error";
SpringBoot项目大概分为四层:
(1)DAO层:包括XxxMapper.java(数据库访问接口类),XxxMapper.xml(数据库链接实现);(这个命名,有人喜欢用Dao命名,有人喜欢用Mapper,看个人习惯了吧)
(2)Bean层:也叫model层,模型层,entity层,实体层,就是数据库表的映射实体类,存放POJO对象;
(3)Service层:也叫服务层,业务层,包括XxxService.java(业务接口类),XxxServiceImpl.java(业务实现类);(可以在service文件夹下新建impl文件放业务实现类,也可以把业务实现类单独放一个文件夹下,更清晰)
(4)Web层:就是Controller层,实现与web前端的交互。
bean层:
UserBean.java
package com.example.bean;
import lombok.Data;
@Data
public class UserBean
private int id;
private String name;
private String password;
service目录
UserService.java
package com.example.service;
import com.example.bean.UserBean;
public interface UserService
UserBean loginIn(String name, String password);
serviceImpl目录:
UserServiceImpl.java
package com.example.serviceImpl;
import com.example.bean.UserBean;
import com.example.mapper.UserMapper;
import com.example.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 UserBean loginIn(String name, String password)
return userMapper.getInfo(name,password);
mapper目录:
UserMapper.java
package com.example.mapper;
import com.example.bean.UserBean;
//DAO层访问数据库接口文件:
public interface UserMapper
UserBean getInfo(String name, String password);
resources–>mapper
UserMapper.xml
<?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.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.example.bean.UserBean">
SELECT * FROM user WHERE name = #name AND password = #password
</select>
</mapper>
<!--
DAO层访问数据库实现文件(需在resource包下创建mapper文件夹,然后再创建一个UserMapper.xml
在application配置文件中mybatis:mapper-locations:对应的就是该文件地址),
注意<mapper>标签的namespace属性要填写 访问数据库接口类文件路径。-->
YanjingMallApplication.java
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.mapper")
@SpringBootApplication
public class YanjingMallApplication
public static void main(String[] args)
SpringApplication.run(YanjingMallApplication.class, args);
application.yml
spring:
datasource:
name: yanjing #数据库名
url: jdbc:mysql://localhost:3306/yanjing #url
username: root #用户名
password: root #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
# MyBatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: com.example.test.bean #配置实体类
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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Yanjing-mall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Yanjing-mall</name>
<description>Yanjing-mall</description>
<properties>
<java.version>1.8</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.2</version>
</dependency>
<!--MySQL数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--单元测试配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok配置 不需要加get set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.6.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.18</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
小结:
前端输入:http://localhost:8080/login
输入信息登录点击login -> 提交loginIn表单,浏览器 访问http://localhost:8080/loginIn
后端代码调用userService对象的loginIn方法,然后调用dao层的usermapper的getInfo方法查询输入的用户信息数据库中是否存在,存在跳转至success界面,反之跳转到error !
JAVA目录
src下main:存放的是代码源文件,java、xml、proeprties等
src下test:通常是我们做单元测试的时候使用
controller:此目录主要是存放Controllerde ,比如:UserController.java,也有的项目是把action放在controller目录下,有的是把UserController.java放在action目录下。
service:这里分接口和实现类,接口在service目录下,接口实现类在service/impl目录下。
dao:持久层,目前比较流行的Mybatis或者jpa之类的。
mapper:DAO层:包括XxxMapper.java(数据库访问接口类),XxxMapper.xml(数据库链接实现);(这个命名:有人喜欢用Dao命名,有人喜欢用Mapper)
entity:就是数据库表的实体对象。
Bean:也叫model层,模型层,entity层,实体层,就是数据库表的映射实体类,存放POJO对象;
interrupt:项目统一拦截处理,比如:登录信息,同意异常处理
config:配置读取相关,比如RedisConfig.java
resources目录
static:存放的是一些js/css/图片
templates:存放模板引擎文件,比如:user.fl
application.properties文件是存放一些配置文件,有的是application.yml或者application.yaml实质是一样的,就是展示不同而已。
mapper:DAO层访问数据库实现文件(需在resource包下创建mapper文件夹,然后再创建一个UserMapper.xml.在application配置文件中mybatis:mapper-locations:对应的就是该文件地址),注意标签的namespace属性要填写 访问数据库接口类文件路径。
参考:
https://blog.csdn.net/baidu_39298625/article/details/98102453
https://blog.csdn.net/sinat_27933301/article/details/88563560
以上是关于一个简单的SpringBoot项目 demo的主要内容,如果未能解决你的问题,请参考以下文章
vue.js+iview+springboot搭建一个前后端分离登陆demo