SpringBoot+MyBatis
切换数据库连接词
引入数据库连接词的依赖,配置数据库连接池的类型;
编写测试类:
package org.example.threelayerdecouplingdomeapplication2;import org.example.threelayerdecouplingdomeapplication2.mapper.UserMapper;
import org.example.threelayerdecouplingdomeapplication2.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest//而用程序在运行时,会自动的为该接口创建一个实现类对象(代理对象),并且会自动将该实现类对象存入IOC容器- bean no usages
class ThreeLayerDecouplingDomeApplication2ApplicationTests {@Autowiredprivate UserMapper userMapper;@Testpublic void testFindAll(){List<User> users = userMapper.findAll();System.out.println(users);}}
使用Mapper来操纵数据库
在 MyBatis 框架里,@Mapper
注释的作用是把接口标记成 MyBatis 的映射器接口。这意味着 MyBatis 会自动为这个接口创建代理实现类,进而让开发者能够直接通过调用接口方法来执行 SQL 操作,无需手动编写实现代码。
package org.example.threelayerdecouplingdomeapplication2.mapper;import org.apache.ibatis.annotations.*;
import org.example.threelayerdecouplingdomeapplication2.pojo.User;import java.util.List;@Mapper
public interface UserMapper {@Select("SELECT * FROM people")List<User> findAll();@Select("SELECT * FROM people WHERE id = #{id}")User findById(Long id);@Insert("INSERT INTO people(name, age, status, gender, tp) VALUES(#{name}, #{age},#{status},#{gender},#{tp})")@Options(useGeneratedKeys = true, keyProperty = "id")int insert(User user);@Update("UPDATE people SET name=#{name}, age=#{age} WHERE ID=#{id}")int update(User user);@Delete("DELETE FROM people WHERE ID = #{id}")int delete(Integer id);
}
删除用户信息需要使用到占位符#{};
查询用户
当传入的形参有多个时候,因为在编译后形成的字节码文件只有类型,所以要用@Param注解给每一个形参起别名,让它能后准确对应sql语句的占位符。
两种占位符的区别
用注入来直接使用接口中的sql
package org.example.threelayerdecouplingdomeapplication2.service.impl;import org.example.threelayerdecouplingdomeapplication2.mapper.UserMapper;
import org.example.threelayerdecouplingdomeapplication2.pojo.User;
import org.example.threelayerdecouplingdomeapplication2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowired// 自动注入private UserMapper userMapper;public List<User> findAll() {return userMapper.findAll();}public User findById(Long id) {return userMapper.findById(id);}public int insert(User user) {return userMapper.insert(user);}public int update(User user) {return userMapper.update(user);}public int delete(Integer id) {return userMapper.delete(id);}
}
同样的使用注入来的控制器
package org.example.threelayerdecouplingdomeapplication2.controller;import org.example.threelayerdecouplingdomeapplication2.pojo.User;
import org.example.threelayerdecouplingdomeapplication2.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserServiceImpl userService;@GetMapping("/findAll")public List<User> findAll() {return userService.findAll();}@GetMapping("/{id}")public User findById(@PathVariable Long id) {return userService.findById(id);}@PostMappingpublic int insert(@RequestBody User user) {return userService.insert(user);}@PutMappingpublic int update(@RequestBody User user) {return userService.update(user);}@DeleteMapping("/{id}")public int delete(@PathVariable Integer id) {return userService.delete(id);}
}
1. @RestController
- 作用:将该类标记为 RESTful 控制器,相当于
@Controller
+@ResponseBody
的组合。 - 效果:
- 类中的方法返回值会自动序列化为 JSON/XML 等格式(取决于客户端的
Accept
头)。 - 无需在每个方法上单独添加
@ResponseBody
。
- 类中的方法返回值会自动序列化为 JSON/XML 等格式(取决于客户端的
2. @RequestMapping("/users")
- 作用:为整个控制器指定基础 URL 路径。
- 效果:所有该类中的请求处理方法的 URL 都会以
/users
开头(例如/users/findAll
)。
3. @Autowired
- 作用:自动注入依赖的 Bean(这里是
UserServiceImpl
)。 - 效果:
- Spring 会在容器中查找
UserServiceImpl
类型的 Bean,并将其注入到userService
字段。 - 无需手动创建
UserServiceImpl
的实例(依赖反转)。
- Spring 会在容器中查找
4. HTTP 请求方法注解
@GetMapping
- 作用:处理 HTTP GET 请求。
- 示例:
@GetMapping("/findAll")
:处理GET /users/findAll
请求。@GetMapping("/{id}")
:处理GET /users/{id}
请求(例如/users/1
),其中{id}
是路径变量。
@PostMapping
- 作用:处理 HTTP POST 请求(通常用于创建资源)。
- 示例:
@PostMapping
:处理POST /users
请求。
@PutMapping
- 作用:处理 HTTP PUT 请求(通常用于更新资源)。
- 示例:
@PutMapping
:处理PUT /users
请求。
@DeleteMapping
- 作用:处理 HTTP DELETE 请求(通常用于删除资源)。
- 示例:
@DeleteMapping("/{id}")
:处理DELETE /users/{id}
请求。
5. 参数绑定注解
@PathVariable
- 作用:从 URL 路径中提取变量值。
- 示例:
@PathVariable Long id
:从 URL 中提取id
参数(例如/users/1
中的1
),并转换为Long
类型。
@RequestBody
- 作用:将 HTTP 请求的 JSON/XML 体反序列化为 Java 对象。
- 示例:
@RequestBody User user
:将请求体中的 JSON 数据映射到User
对象(需确保 JSON 字段与User
类属性名匹配)。
SpringBoot中的配置文件
SpringBoot项目提供了多种属性配置方式(properties、yaml、yml)。
properties
例如:
yaml、yml
格式
·数值前边必须有空格,作为分隔符
·使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格( idea中会自动将Tab转换为空格)
·缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
·#表示注释,从这个字符一直到行尾,都会被解析器忽略
定义对象/Map集合
:后面要有空格
定义数组/List/Set集合
每一个元素前面都有一个空格
注意:在yml配置文件中,如果配置项的值是以开头的,值要使用"引起来,因为以0开头的在yml中表示8进制数据