1.引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql的驱动依赖 3.-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis和springboot整合的jar.-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置文件:
spring.application.name=qy174-springboot-mybatis
#数据源的信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/qy174-springboot?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#配置mybatis映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml
3.实体类
@Data
public class User {
private Integer userid;
private String username;
private String sex;
private Integer age;
}
4.对应的dao层代码
public interface UserDao {
int insert(User user);
int delete(Integer id);
int update(User user);
User select(Integer id);
List<User> selectAll();
}
5.映射文件
<?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.ykq.dao.UserDao">
<insert id="insert">
insert into user(username,age,sex) values(#{username},#{age},#{sex})
</insert>
<update id="update">
update user set username=#{username},age=#{age},sex=#{sex} where userid=#{userid}
</update>
<delete id="delete">
delete from user where userid=#{id}
</delete>
<select id="selectById" resultType="com.ykq.entity.User">
select * from user where userid=#{id}
</select>
<select id="selectAll" resultType="com.ykq.entity.User">
select * from user
</select>
</mapper>
6. 对应的service层代码
package com.ls.service.Impl;
import com.ls.dao.UserDao;
import com.ls.entity.User;
import com.ls.service.UserService;
import com.ls.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: springboot
* @description:
* @author: 1suo
* @create: 2024-07-12 09:12
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public R insert(User user) {
int insert = userDao.insert(user);
if (insert>0){
return new R(200,"success",null);
}
return new R(500,"error",null);
}
@Override
public R delete(Integer id) {
int delete = userDao.delete(id);
if (delete>0){
return new R(200,"success",null);
}
return new R(500,"error",null);
}
@Override
public R update(User user) {
int update = userDao.update(user);
if (update>0){
return new R(200,"success",null);
}
return new R(500,"error",null);
}
@Override
public R selectById(Integer id) {
User user = userDao.selectById(id);
if (user!=null){
return new R(200,"success",user);
}
return new R(500,"error",null);
}
@Override
public R selectAll() {
List<User> list = userDao.selectAll();
return new R(200,"success",list);
}
}
7.对应的controller层代码
package com.ls.controller;
import com.ls.entity.User;
import com.ls.service.UserService;
import com.ls.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @program: springboot
* @description:
* @author: 1suo
* @create: 2024-07-10 17:01
**/
@RestController
public class UserController {
/* @RequestMapping(value = "/hello")
public String getName(){
return "hello";
}*/
@Autowired
private UserService userService;
@PostMapping("/insert")
public R insert(@RequestBody User user){
R insert = userService.insert(user);
return insert;
}
@DeleteMapping("/delete")
public R delete(Integer id){
R delete = userService.delete(id);
return delete;
}
@PutMapping("/update")
public R update(@RequestBody User user){
R update = userService.update(user);
return update;
}
@GetMapping("/selectById")
public R selectById(Integer id){
R selectById = userService.selectById(id);
return selectById;
}
@GetMapping("/selectAll")
public R selectAll(){
R selectAll = userService.selectAll();
return selectAll;
}
}