使用Spring Boot實現操作數據庫的接口的過程
一、Spring Boot原理
用戶從頁面前端,也就是我們所說的 view 層進行查詢訪問,進入到 controller 層找到對應的接口,接 著 controller 進行對 service 層進行業務功能的調用,service 要進入 dao 層查詢數據,dao 層調用 mapper.xml 文件生成 sql 語句到數據庫中進行查詢
二、實現過程
2.1、準備數據庫user表插入四條數據
2.2、model下創建一個User類
與數據庫的字段一一對應
@Getter @Setter public class User { private int id; private String username; private String password; private int age; }
2.3、dao下創建一個UserDao接口
@Repository註解修飾哪個類,則表明這個類具有對對象進行CRUD(增刪改查)的功能
@Repository public interface UserDao { public User getUserById(@Param("id") int id); public List<User> getUserByAge(@Param("age") int age); public List<User> getUserByName(@Param("username") String username); public int insertUser(@RequestBody User user); }
通過UserMapping.xml配置文件實現UserDao接口
<?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.demo.dao.UserDao"> <select id="getUserById" resultType="User"> select * from `user` where id=#{id} </select> <select id="getUserByAge" resultType="User"> select * from `user` where age=#{age} </select> <select id="getUserByName" resultType="User"> select * from `user` where username like concat('%',#{username},'%') </select> <insert id="insertUser" parameterType="User"> <selectKey keyProperty="id" order="AFTER" resultType="int"> SELECT LAST_INSERT_ID() </selectKey> insert into user (username, password, age) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) </insert> </mapper>
2.4、Service下創建一個UserService接口
public interface UserService { public User queryUserById(int id); public List<User> queryUserByAge(int age); public List<User> queryUserByName(String username); public int insertUser(User user); }
再創建一個UserServiceImpl實現UserService
@Service public class UserSeviceImpl implements UserService { @Autowired private UserDao userDao; @Override public User queryUserById(int id) { return userDao.getUserById(id); } @Override public List<User> queryUserByAge(int age) { return userDao.getUserByAge(age); } @Override public List<User> queryUserByName(String username) { return userDao.getUserByName(username); } @Override public int insertUser(User user) { return userDao.insertUser(user); } }
2.5、controller下創建一個UserController
@GetMapping是代表該查詢接口用的是get方式
@RequestMapping(value=”/insert”,method = RequestMethod.POST)代表該插入接口用post方式
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("/userid") public User getUserById(@RequestParam("id") int id){ return userService.queryUserById(id); } @GetMapping("/username") public List<User> getUserByUsername(@RequestParam("username") String username){ return userService.queryUserByName(username); } @RequestMapping(value="/insert",method = RequestMethod.POST) public User insertUser(@RequestBody User user){ int x = userService.insertUser(user); return user; } }
2.6、application.yml文件連接數據庫
server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testingdev9?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 mybatis: mapper-locations: - classpath:mapper/*.xml - classpath*:com/**/mapper/*.xml type-aliases-package: com.example.demo.model
2.7、啟動DemoApplication文件
出現Started DemoApplication代表啟動成功瞭
2.8、使用postman調用接口
如下圖根據id查詢
如下圖插入數據
可以看到數據庫多瞭一條數據
到此這篇關於使用Spring Boot實現操作數據庫的接口的過程的文章就介紹到這瞭,更多相關Spring Boot操作數據庫接口內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 關於SSM框架下各層的解釋說明(Controller等)
- Spring Dao層@Repository與@Mapper的使用
- spring boot寫java web和接口
- SpringBoot整合Mybatis簡單實現增刪改查
- SpringBoot實現分頁功能