目录
1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<groupId>com.coffee</groupId>
<artifactId>webmvc-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<mybatis.verison>2.2.2</mybatis.verison>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.verison}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebMvcApplication {
public static void main(String[] args) {
SpringApplication.run(WebMvcApplication.class, args);
}
}
3、实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class People implements Serializable {
private static final long serialVersionUID = 620950479129453625L;
private Integer id;
private String username;
private String gender;
private String address;
}
4、dao层
import com.coffee.webmvc.samples.entity.People;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;
import java.util.List;
@Mapper
public interface PeopleDao {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
People queryById(Integer id);
/**
* 查询指定行数据
*
* @param people 查询条件
* @param pageable 分页对象
* @return 对象列表
*/
List<People> queryAllByLimit(People people, @Param("pageable") Pageable pageable);
/**
* 统计总行数
*
* @param people 查询条件
* @return 总行数
*/
long count(People people);
/**
* 新增数据
*
* @param people 实例对象
* @return 影响行数
*/
int insert(People people);
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<People> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<People> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<People> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<People> entities);
/**
* 修改数据
*
* @param people 实例对象
* @return 影响行数
*/
int update(People people);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Integer id);
}
5、service层
package com.coffee.webmvc.samples.service;
import com.coffee.webmvc.samples.entity.People;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
public interface PeopleService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
People queryById(Integer id);
/**
* 分页查询
*
* @param people 筛选条件
* @param pageRequest 分页对象
* @return 查询结果
*/
Page<People> queryByPage(People people, PageRequest pageRequest);
/**
* 新增数据
*
* @param people 实例对象
* @return 实例对象
*/
People insert(People people);
/**
* 修改数据
*
* @param people 实例对象
* @return 实例对象
*/
People update(People people);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Integer id);
}
6、service实现类
import com.coffee.webmvc.samples.entity.People;
import com.coffee.webmvc.samples.dao.PeopleDao;
import com.coffee.webmvc.samples.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
@Service("peopleService")
public class PeopleServiceImpl implements PeopleService {
@Autowired
private PeopleDao peopleDao;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public People queryById(Integer id) {
return this.peopleDao.queryById(id);
}
/**
* 分页查询
*
* @param people 筛选条件
* @param pageRequest 分页对象
* @return 查询结果
*/
@Override
public Page<People> queryByPage(People people, PageRequest pageRequest) {
long total = this.peopleDao.count(people);
return new PageImpl<>(this.peopleDao.queryAllByLimit(people, pageRequest), pageRequest, total);
}
/**
* 新增数据
*
* @param people 实例对象
* @return 实例对象
*/
@Override
public People insert(People people) {
this.peopleDao.insert(people);
return people;
}
/**
* 修改数据
*
* @param people 实例对象
* @return 实例对象
*/
@Override
public People update(People people) {
this.peopleDao.update(people);
return this.queryById(people.getId());
}
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Integer id) {
return this.peopleDao.deleteById(id) > 0;
}
}
7、controller层
import com.coffee.webmvc.samples.entity.People;
import com.coffee.webmvc.samples.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("people")
public class PeopleController {
/**
* 服务对象
*/
@Autowired
private PeopleService peopleService;
/**
* 分页查询
*
* @param people 筛选条件
* @param pageRequest 分页对象
* @return 查询结果
*/
@GetMapping("/queryByPage")
public ResponseEntity<Page<People>> queryByPage(People people, PageRequest pageRequest) {
return ResponseEntity.ok(this.peopleService.queryByPage(people, pageRequest));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
public ResponseEntity<People> queryById(@PathVariable("id") Integer id) {
return ResponseEntity.ok(this.peopleService.queryById(id));
}
/**
* 新增数据
*
* @param people 实体
* @return 新增结果
*/
@PostMapping
public ResponseEntity<People> add(People people) {
return ResponseEntity.ok(this.peopleService.insert(people));
}
/**
* 编辑数据
*
* @param people 实体
* @return 编辑结果
*/
@PutMapping({"{id}"})
public ResponseEntity<People> edit(@PathVariable("id") People people) {
return ResponseEntity.ok(this.peopleService.update(people));
}
/**
* 删除数据
*
* @param id 主键
* @return 删除是否成功
*/
@DeleteMapping({"{id}"})
public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
return ResponseEntity.ok(this.peopleService.deleteById(id));
}
}
8、mapper文件
<?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.coffee.webmvc.samples.dao.PeopleDao">
<resultMap type="com.coffee.webmvc.samples.entity.People" id="PeopleMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="gender" column="gender" jdbcType="VARCHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="PeopleMap">
select id,
username,
gender,
address
from people
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="PeopleMap">
select
id, username, gender, address
from people
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
<if test="address != null and address != ''">
and address = #{address}
</if>
</where>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from people
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
<if test="address != null and address != ''">
and address = #{address}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into people(username, gender, address)
values (#{username}, #{gender}, #{address})
</insert>
<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
insert into people(username, gender, address)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.gender}, #{entity.address})
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
insert into people(username, gender, address)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.gender}, #{entity.address})
</foreach>
on duplicate key update
username = values(username),
gender = values(gender),
address = values(address)
</insert>
<!--通过主键修改数据-->
<update id="update">
update people
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="gender != null and gender != ''">
gender = #{gender},
</if>
<if test="address != null and address != ''">
address = #{address},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete
from people
where id = #{id}
</delete>
</mapper>
9、application.properties
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
mybatis.mapper-locations=classpath:mapper/*.xml
至此,整合数据库实现增删改查的一个简单SpringMVC Web项目就搭建完成。详细的项目目录结构如下图:
备注:以上大多数代码可以利用EasyCode结合数据库自动生成。