SpringBoot实现数据服务接口
1、创建项目
2、添加依赖(可选的)
3、初建目录结构
浏览器
↑↓
Application(地址、端口localhost:8070
)
↑↓
controller(URL路径、GET和POST方法)
↑↓
service(连接数据库)
↑↓
mapper(发送SQL)
↑↓
数据库
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.5version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>demo3artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo3name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server.port=5000
spring.datasource.url=jdbc:mysql://localhost:3306/库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=密码
server.port
配置端口spring.datasource
配置数据库连接package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class Demo3Application {
public static void main(String[] args) {
SpringApplication.run(Demo3Application.class, args);
}
}
@MapperScan
注解,指明mapper
包路径package com.example.demo.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
Integer uid;
String name;
}
@Data
含Getter、Setter、ToString等方法@AllArgsConstructor
含所有参数的构造器package com.example.demo.controller;
import com.example.demo.bean.User;
import com.example.demo.service.UserServiceImpl;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Controller
@RestController
@RequestMapping("/school")
public class UserController {
// @Autowired自动装载服务类
@Autowired
private UserServiceImpl userServiceImpl;
@RequestMapping("/user")
public String getUser(@RequestParam("uid") Integer i,
@RequestParam("name") String s) {
User user = userServiceImpl.getUser(i, s);
return "uid:" + user.getUid() + "\nname:" + user.getName();
}
@RequestMapping("/users")
public String getUsers() {
// 可变的字符序列
StringBuilder uid_s = new StringBuilder("[");
StringBuilder name_s = new StringBuilder("[");
// 遍历
List<User> users = userServiceImpl.getUsers();
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
Integer uid = user.getUid();
String name = user.getName();
// 追加字符串
uid_s.append("\"").append(uid).append("\"");
name_s.append("\"").append(name).append("\"");
if (i < users.size() - 1) {
uid_s.append(",");
name_s.append(",");
} else {
uid_s.append("]");
name_s.append("]");
}
}
return "{\n" +
" \"uid\":" + uid_s + ",\n" +
" \"name\":" + name_s + "\n" +
"}";
}
}
@ResponseBody
@RestController
@Controller
+@ResponseBody
,可用来返回JSON数据@RequestMapping
value
指定请求URL,method
指定请求方式,默认GET@RequestMapping(value="/a",method=RequestMethod.GET)
可简写为@RequestMapping("/a")
/school/user
@RequestParma("uid")
package com.example.demo.service;
import com.example.demo.bean.User;
import com.example.demo.mapper.MysqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl{
// 自动装载 Mapper 接口实现类
@Autowired
MysqlMapper mysqlMapper;
// 传参测试,获取单个用户
public User getUser(Integer i, String s) {
return mysqlMapper.selectUser(i, s);
}
// 获取多个用户
public List<User> getUsers() {
return mysqlMapper.selectUsers();
}
}
@Service
注解@Autowired
注解@Autowired
注解的接口,去内存的常驻组件中寻找适配组件(实现类)package com.example.demo.mapper;
import com.example.demo.bean.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface MysqlMapper {
// 返回单个用户(#{}传参,字符串不需要引号)
@Select("SELECT #{u} AS uid,#{n} AS name")
User selectUser(@Param("u")Integer i, @Param("n")String s);
// 查多个用户
@Select("SELECT 101 AS uid,'Zoe' AS name UNION ALL SELECT 102 AS uid,'Mary' AS name")
List<User> selectUsers();
}
@Select
注解,位于方法定义语句上方@MapperScan
,指明mapper
包路径@Autowired
自动装载该实现类@Param
注解:传参给SQL输入
controller
定义的URL路径
英 | 🔉 | 中 |
---|---|---|
starter | ˈstɑːrtər | n. (机器或引擎的)启动装置;adj. 起步时使用的 |
Autowire | n. 自动装配 | |
stereotype | ˈsteriətaɪp | n. 模式化的思想,老一套;公式化人物;铅版; |
stereo type | 定型 | |
wired | ˈwaɪərd | adj. 有线的,联网的;v. 接通(电源);(用电线)连接(wire 的过去式和过去分词) |
wire | ˈwaɪər | n. 金属线;导线;<美>电报;v. 接通(电源);(用电线)接通;用金属线固定 |
boot | buːt | n. 靴子;猛踢;(汽车的)后备箱;v. 猛踢;启动(电脑);在车轮上装制动装置 |