一、新建项目
二、代码编写
项目结构
先在resource文件夹中创建application.yml编写数据库连接配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://106.15.105.2:3306/oms?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=true
username: user1
password: 123456
在po包下编写user实体类
package edu.nynu.demo01.po;
public class User {
private Integer id;
private String name;
private String pwd;
public User() {}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
在dao包下编写UserDao.java接口
package edu.nynu.demo01.dao;
import edu.nynu.demo01.po.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserDao {
@Select("select * from user1")
List<User> findAll();
@Select("select * from user1 where name=#{name} and pwd=#{pwd}")
User findByNameAndPwd(@Param("name") String name, @Param("pwd") String pwd);
@Insert("insert into user1 values(#{id}, #{name}, #{pwd})")
Integer addByUser(User user);
@Delete("delete from user1 where id=#{id}")
Integer deleteUserById(Integer id);
@Update("update user1 set name=#{name}, pwd=#{pwd} where id=#{id}")
Integer updateNameAndPwdById(@Param("id") Integer id, @Param("name") String name, @Param("pwd") String pwd);
}
在service包下编写UserService.java接口
package edu.nynu.demo01.service;
import edu.nynu.demo01.po.User;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
List<User> findAllUser();
User findUser(String name, String pwd);
Integer addUser(User user);
Integer deleteUser(Integer id);
Integer updateUserInfo(Integer id, String name, String pwd);
}
在service包下做UserService接口的实现类UserServiceImpl.java
package edu.nynu.demo01.service.impl;
import edu.nynu.demo01.dao.UserDao;
import edu.nynu.demo01.po.User;
import edu.nynu.demo01.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAllUser() {
return userDao.findAll();
}
@Override
public User findUser(String name, String pwd) {
return userDao.findByNameAndPwd(name, pwd);
}
@Override
public Integer addUser(User user) {
return userDao.addByUser(user);
}
@Override
public Integer deleteUser(Integer id) {
return userDao.deleteUserById(id);
}
@Override
public Integer updateUserInfo(Integer id, String name, String pwd) {
return userDao.updateNameAndPwdById(id, name, pwd);
}
}
在controller包下编写UserController.java
package edu.nynu.demo01.controller;
import edu.nynu.demo01.po.User;
import edu.nynu.demo01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/f1")
public String findAllUser(){
return userService.findAllUser().toString();
}
@RequestMapping("/f2")
public User findUser(String name, String pwd){
name = "张三";
pwd = "123456";
return userService.findUser(name, pwd);
}
@RequestMapping("/add")
public Integer addUser(User user){
user.setName("张三");
user.setPwd("123456");
return userService.addUser(user);
}
@RequestMapping("/delete")
public Integer deleteUser(Integer id){
id = 1;
return userService.deleteUser(id);
}
@RequestMapping("/update")
public Integer update(Integer id, String name, String pwd){
id = 1;
name = "李四";
pwd = "654321";
return userService.updateUserInfo(id, name, pwd);
}
}
三、结语
本片文章使用SpringBoot实现了对数据库的增删改查,还是很简单的
标签:SpringBoot,改查,public,String,pwd,增删,Integer,id,name From: https://www.cnblogs.com/puz2le/p/17616083.html