IDEA下载
IDEA好贵,想要学生认证白嫖,结果哈工大的学生邮箱非得加个stu的后缀导致无法识别,还得通过人工认证…
springboot initializr
打开idea创建一个新项目,记得先配置java环境(你发任你发,我用java8)
运行环境配置
点击右侧maven打开工具箱, 双击lifecycle中的install install完后,项目即可运行,run一下,打开网页localhost:8080,出现如下页面。
建立controller
其为MVC三层架构中的一层,用于将用户请求转发给相应的 Model 进行处理,并根据 Model 的计算结果向用户提供相应响应。它是springboot里最基本的组件,这里我们用其来接收用户提交请求,进行URL匹配,进行处理后,再返回给用户。 这里我们首先在主程序所在文件夹建立一个controller包 然后在该包下编写一个hello.java程序,其中注解@RestController表示该类处理http请求,@RequestMapping 配置url映射关系
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class hello {
@RequestMapping("/hello")
public String helloController(){
return "Hello World";
}
}
再run一下,并打开localhost:8080/hello
mysql文件准备
我这里准备了一个建好User表的数据库university,其中User表里有uname和password两个属性。 通过下列代码进行导入
mysql -u root -p
create database university;
use university;
source university.sql;
springboot 后端配置
pom.xml配置
pom是project object model的简称,是maven项目中的文件,用于管理源代码,配置文件,开发者的信息和角色,项目url,依赖关系等等,这里引入mysql连接驱动,mybatis依赖,以及io工具类
<?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>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>demo1</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot 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>
<version>8.0.28</version>
</dependency>
<!--io工具类-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dao文件编写
dao层是MVC架构中的数据访问层,也是mapper层,具体到对某个表的增删改查,某个dao一定和数据库中每个表对应,建议其只做原子操作。
package com.example.demo.dao;
import com.example.demo.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface UserDao {
@Insert("insert into user values (#{uname}, #{password})")
void addUser(User user);
@Update("update user set password = #{password} where uname = #{uname}")
void updateUser(User user);
@Select("select * from user")
List<User> selectAll();
}
service文件编写
service层是服务层,也就是对多个dao进行再次封装,封装成一个事务处理。
package com.example.demo.service;
import com.example.demo.bean.User;
import java.util.List;
public interface userService {
boolean addUser(User user);
List<User> selectAll();
}
package com.example.demo.service;
import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class userServiceImpl implements userService{
@Autowired
private UserDao userDao;
@Override
public boolean addUser(User user) {
boolean flag = false;
try{
userDao.addUser(user);
flag = true;
}
catch (Exception e){
e.printStackTrace();
}
return flag;
}
@Override
public List<User> selectAll() {
return userDao.selectAll();
}
}
controller再编写
@RequestBody用于接收前端传递给后端的json字符串,使用注解@Autowired来引用服务
package com.example.demo.controller;
import com.example.demo.bean.User;
import com.example.demo.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class hello {
@Autowired
private userService userservice;
@RequestMapping("/hello")
public String helloController(){
return "Hello World";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public boolean addUser(@RequestBody User user){
System.out.println("新增数据");
return userservice.addUser(user);
}
@RequestMapping(value = "/userAll", method = RequestMethod.GET)
public List<User> selectAll(){
System.out.println("查询所有数据");
return userservice.selectAll();
}
}
application.properties
配置对应数据库
spring.datasource.url=jdbc:mysql://localhost:3306/university?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.username={your username}
spring.datasource.password={your password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#注意mysql8.0版本以下用com.mysql.jdbc.Driver,以上用com.mysql.cj.jdbc.Driver
## Mybatis
# 配置实体类位置
mybatis.typeAliasesPackage=com.example.demo.bean
# 配置mapper位置
mybatis.mapperLocations=classpath\:mapper
最后访问页面成功查询到数据
|