pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--添加分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
applicati.yml
server:
port: 8888
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/user?serverTimezone=GMT
username: root
password: root123
mybatis:
type-aliases-package: com.itheima.pojo #别名
mapper-locations: classpath:mapper/*.xml
实体类pojo
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr;
}
mapper数据层
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> getAll();
public List<User> getById(@Param("id") Integer id);
public int deleteById(Integer id);
public int updateUser(User user);
public int insertUser(User user);
}
mapper.xml
<?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.itheima.mapper.UserMapper">
<insert id="insertUser">
insert into user (username,password,gender,addr)
VALUES (#{username},#{password},#{gender},#{addr})
</insert>
<update id="updateUser">
update user
set username=#{username},password=#{password},gender=#{gender},addr=#{addr}
where id =#{id}
</update>
<delete id="deleteById">
delete from user where id = #{id}
</delete>
<select id="getAll" resultType="com.itheima.pojo.User">
select * from user
</select>
<select id="getById" resultType="com.itheima.pojo.User">
select * from user
where id = #{id}
</select>
</mapper>
service业务层接口
import com.itheima.pojo.User;
import java.util.List;
public interface UserService {
public List<User> getAll();
public List<User> getById(Integer id);
public int deleteById(Integer id);
public int updateUser(User user);
public int insertUser(User user);
}
serviceIpml业务层实现类
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAll() {
return userMapper.getAll();
}
@Override
public List<User> getById(Integer id) {
return userMapper.getById(id);
}
@Override
public int deleteById(Integer id) {
return userMapper.deleteById(id);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}
controller控制层
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public PageInfo getAll(@RequestParam(value = "categoryId",required = false) Integer categoryId,
@RequestParam(value = "currentPageNo", defaultValue = "1", required = false) Integer currentPageNo,
@RequestParam(value = "pageSize", defaultValue = "3", required = false) Integer pageSize) {
PageHelper.startPage(currentPageNo, pageSize);
List<User> list = userService.getAll();
PageInfo pageInfo = new PageInfo(list);
return pageInfo;
}
@GetMapping("/{id}")
public List<User> getById(@PathVariable Integer id) {
return userService.getById(id);
}
@DeleteMapping("/{id}")
public int deleteById(@PathVariable Integer id) {
return userService.deleteById(id);
}
@PutMapping
public int updateUser(@RequestBody User user) {
return userService.updateUser(user);
}
@PostMapping
public int insertUser(@RequestBody User user) {
return userService.insertUser(user);
}
}