SpringBoot+Mybatis+MySQL实现增删改查
新建项目
DemoApplication
添加:
@MapperScan("com.example.demo.mapper")
一定要在com.example.demo后面指明是mapper文件夹
application.properties:
注意:等号后面不要加引号
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8
#不要加引号!
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
controller/UserController:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> findAll(){
return userService.findAll();
}
}
entity/User:
导入lombok,用@Data注解后,类就不用添加get、set这些麻烦操作了
package com.example.demo.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
}
mapper/UserMapper:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
}
service/UserService:
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
service/UserServiceImpl:
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
mysql数据库文件:
启动项目,访问:http://localhost:8081/user
这样就实现了查询功能。
接下来实现其他功能
controller/UserController:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getUsers(){
return userService.findAll();
}
@PostMapping
public String addUser(@RequestBody User user){
userService.addUser(user);
return "success";
}
@PutMapping
public String updateUser(@RequestBody User user){
userService.updateUserById( user);
return "success";
}
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Integer id){
userService.deleteUserById(id);
return "success";
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Integer id){
return userService.getUserById(id);
}
@GetMapping("/find")
public User getUserByUsername(@RequestParam String username){
return userService.getUserByUsername(username);
}
@GetMapping("/hello")
public String hello(@RequestParam String name){
return "hello:"+name;
}
}
mapper/UserMapper:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
@Update("INSERT INTO `user`(`username`, `password`) VALUES (#{username}, #{password})")
@Transactional
void addUser(User user);
@Update("update user set username=#{username},password=#{password} where id=#{id}")
@Transactional
void updateUserById(User user);
@Delete("delete from user where id=#{id}")
@Transactional
void deleteUserById(Integer id);
@Select("select * from user where id=#{id}")
User getUserById(Integer id);
@Select("select * from user where username=#{username}")
User getUserByUsername(String username);
}
service/UserService:
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
void addUser(User user);
void updateUserById(User user);
void deleteUserById(Integer id);
User getUserById(Integer id);
User getUserByUsername(String username);
}
service/UserServiceImpl:
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void updateUserById(User user) {
userMapper.updateUserById(user);
}
@Override
public void deleteUserById(Integer id) {
userMapper.deleteUserById(id);
}
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public User getUserByUsername(String username) {
return userMapper.getUserByUsername(username);
}
}
还没有评论,来说两句吧...