使用Spring Boot实现操作数据库的接口的过程
作者:ouyangsong_pass_fail
一、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操作数据库接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!