使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫的接口的過程
用戶從頁面前端,也就是我們所說的 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ù)庫中進(jìn)行查詢
2.1、準(zhǔn)備數(shù)據(jù)庫user表插入四條數(shù)據(jù)
2.2、model下創(chuàng)建一個(gè)User類
與數(shù)據(jù)庫的字段一一對(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ù)庫
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)成功了
2.8、使用postman調(diào)用接口
如下圖根據(jù)id查詢
如下圖插入數(shù)據(jù)
可以看到數(shù)據(jù)庫多了一條數(shù)據(jù)
到此這篇關(guān)于使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫的接口的過程的文章就介紹到這了,更多相關(guān)Spring Boot操作數(shù)據(jù)庫接口內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 解決Android Studio Design界面不顯示layout控件的問題2. python mysql 字段與關(guān)鍵字沖突的解決方式3. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼4. Python加載數(shù)據(jù)的5種不同方式(收藏)5. python讀取中文路徑時(shí)出錯(cuò)(2種解決方案)6. Python用K-means聚類算法進(jìn)行客戶分群的實(shí)現(xiàn)7. python編寫五子棋游戲8. Java源碼解析之接口List9. Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作10. layui Ajax請(qǐng)求給下拉框賦值的實(shí)例
