成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

瀏覽:6日期:2023-06-30 14:20:57
一、Spring Boot原理

用戶從頁(yè)面前端,也就是我們所說的 view 層進(jìn)行查詢?cè)L問,進(jìn)入到 controller 層找到對(duì)應(yīng)的接口,接 著 controller 進(jìn)行對(duì) service 層進(jìn)行業(yè)務(wù)功能的調(diào)用,service 要進(jìn)入 dao 層查詢數(shù)據(jù),dao 層調(diào)用 mapper.xml 文件生成 sql 語句到數(shù)據(jù)庫(kù)中進(jìn)行查詢

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

二、實(shí)現(xiàn)過程

2.1、準(zhǔn)備數(shù)據(jù)庫(kù)user表插入四條數(shù)據(jù)

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

2.2、model下創(chuàng)建一個(gè)User類

與數(shù)據(jù)庫(kù)的字段一一對(duì)應(yīng)

@Getter @Setterpublic class User { private int id; private String username; private String password; private int age;}

2.3、dao下創(chuàng)建一個(gè)UserDao接口

@Repository注解修飾哪個(gè)類,則表明這個(gè)類具有對(duì)對(duì)象進(jìn)行CRUD(增刪改查)的功能

@Repositorypublic 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配置文件實(shí)現(xiàn)UserDao接口

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapperPUBLIC '-//mybatis.org//DTD Mapper 3.0//EN''http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='com.example.demo.dao.UserDao'> <select resultType='User'>select * from `user` where id=#{id} </select> <select resultType='User'>select * from `user` where age=#{age} </select> <select resultType='User'>select * from `user` where username like concat(’%’,#{username},’%’) </select> <insert 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下創(chuàng)建一個(gè)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);}

再創(chuàng)建一個(gè)UserServiceImpl實(shí)現(xiàn)UserService

@Servicepublic 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下創(chuàng)建一個(gè)UserController

@GetMapping是代表該查詢接口用的是get方式@RequestMapping(value='/insert',method = RequestMethod.POST)代表該插入接口用post方式

@RestControllerpublic 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文件連接數(shù)據(jù)庫(kù)

server: port: 8080spring: 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: 123456mybatis: mapper-locations: - classpath:mapper/*.xml - classpath*:com/**/mapper/*.xml type-aliases-package: com.example.demo.model

2.7、啟動(dòng)DemoApplication文件

出現(xiàn)Started DemoApplication代表啟動(dòng)成功了

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

2.8、使用postman調(diào)用接口

如下圖根據(jù)id查詢

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

如下圖插入數(shù)據(jù)

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

可以看到數(shù)據(jù)庫(kù)多了一條數(shù)據(jù)

使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程

到此這篇關(guān)于使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過程的文章就介紹到這了,更多相關(guān)Spring Boot操作數(shù)據(jù)庫(kù)接口內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章: