赞
踩
Spring-Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring
Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
利用Spring-Boot可以减少我们的依赖配置环节所浪费的时间。
1.快速搭建一个demo项目
点击FIle>New>project>Spring Initializr
修改Group SDK 3. 点击Next,并在此页选择Web下的Spring Web
这样 一个Web项目就创建成功了
2.编写DemoController输出Hello World
3.运行并输入地址
到这里 我们新建项目就完成了
web项目缺不了与数据库的交互 下面我就以Mysql为例,创建用户管理模块
配置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 https://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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.skpang</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot-demo</name>
<description>springboot-demo</description>
<!-- 版 本 管 理 -->
<properties>
<!-- JDK 版本 -->
<java.version>1.8</java.version>
<!-- 数 据 源 版 本 -->
<druid.version>1.1.22</druid.version>
<!-- Mysql 版 本 -->
<mysql.version>8.0.19</mysql.version>
<!-- MyBatis 版 本 -->
<mybatis.plus.version>3.4.3.4</mybatis.plus.version>
<!-- 分页依赖-->
<pagehelper.version>5.3.0</pagehelper.version>
<!-- Maven 配 置 编 译 JDK 版 本-->
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
分别配置公共properties 和dev.properties
我们项目将xml文件放在了main文件夹下,但是生成target时默认不生成main文件夹下的文件所以在pom文件中build下添加一下配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!-- 加载 .Java 下的 XML 文件 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!-- 加载 .resources 下的 资源文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
<?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.skpang.demo.mapper.UserMapper">
<resultMap type="com.skpang.demo.domain.User" id="UserResult">
<result property="userId" column="user_id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="salt" column="salt"/>
<result property="status" column="status"/>
<result property="realName" column="real_name"/>
<result property="email" column="email"/>
<result property="avatar" column="avatar"/>
<result property="sex" column="sex"/>
<result property="phone" column="phone"/>
<result property="createTime" column="create_time"/>
<result property="createBy" column="create_by"/>
<result property="updateTime" column="update_time"/>
<result property="updateBy" column="update_by"/>
<result property="remark" column="remark"/>
<result property="enable" column="enable"/>
<result property="login" column="login"/>
</resultMap>
<sql id="selectUserVo">
select`user_id`,`username`,`password`,`salt`,`status`,`real_name`,`email`,`avatar`,`sex`,`phone`,`create_time`,`create_by`,`update_time`,`update_by`,`remark`,`enable`,`login` from `user`
</sql>
<select id="selectUserList" parameterType="com.skpang.demo.domain.User" resultMap="UserResult">
<include refid="selectUserVo"/>
<where>
<if test="username != null and username != ''">and
`username` like concat('%', #{username}, '%')
</if>
<if test="password != null and password != ''">and
`password` = #{password}
</if>
<if test="salt != null and salt != ''">and
`salt` = #{salt}
</if>
<if test="status != null and status != ''">and
`status` = #{status}
</if>
<if test="realName != null and realName != ''">and
`real_name` like concat('%', #{realName}, '%')
</if>
<if test="email != null and email != ''">and
`email` = #{email}
</if>
<if test="avatar != null and avatar != ''">and
`avatar` = #{avatar}
</if>
<if test="sex != null and sex != ''">and
`sex` = #{sex}
</if>
<if test="phone != null and phone != ''">and
`phone` = #{phone}
</if>
<if test="enable != null and enable != ''">and
`enable` = #{enable}
</if>
<if test="login != null and login != ''">and
`login` = #{login}
</if>
</where>
</select>
<select id="selectUserById" parameterType="String"
resultMap="UserResult">
<include refid="selectUserVo"/>
where `user_id` = #{userId}
</select>
<insert id="insertUser" parameterType="com.skpang.demo.domain.User">
insert into `user`
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">`user_id`,
</if>
<if test="username != null">`username`,
</if>
<if test="password != null">`password`,
</if>
<if test="salt != null">`salt`,
</if>
<if test="status != null">`status`,
</if>
<if test="realName != null">`real_name`,
</if>
<if test="email != null">`email`,
</if>
<if test="avatar != null">`avatar`,
</if>
<if test="sex != null">`sex`,
</if>
<if test="phone != null">`phone`,
</if>
<if test="createTime != null">`create_time`,
</if>
<if test="createBy != null">`create_by`,
</if>
<if test="updateTime != null">`update_time`,
</if>
<if test="updateBy != null">`update_by`,
</if>
<if test="remark != null">`remark`,
</if>
<if test="enable != null">`enable`,
</if>
<if test="login != null">`login`,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userId != null">#{userId},
</if>
<if test="username != null">#{username},
</if>
<if test="password != null">#{password},
</if>
<if test="salt != null">#{salt},
</if>
<if test="status != null">#{status},
</if>
<if test="realName != null">#{realName},
</if>
<if test="email != null">#{email},
</if>
<if test="avatar != null">#{avatar},
</if>
<if test="sex != null">#{sex},
</if>
<if test="phone != null">#{phone},
</if>
<if test="createTime != null">#{createTime},
</if>
<if test="createBy != null">#{createBy},
</if>
<if test="updateTime != null">#{updateTime},
</if>
<if test="updateBy != null">#{updateBy},
</if>
<if test="remark != null">#{remark},
</if>
<if test="enable != null">#{enable},
</if>
<if test="login != null">#{login},
</if>
</trim>
</insert>
<update id="updateUser" parameterType="com.skpang.demo.domain.User">
update `user`
<trim prefix="SET" suffixOverrides=",">
<if test="username != null">`username` =
#{username},
</if>
<if test="password != null">`password` =
#{password},
</if>
<if test="salt != null">`salt` =
#{salt},
</if>
<if test="status != null">`status` =
#{status},
</if>
<if test="realName != null">`real_name` =
#{realName},
</if>
<if test="email != null">`email` =
#{email},
</if>
<if test="avatar != null">`avatar` =
#{avatar},
</if>
<if test="sex != null">`sex` =
#{sex},
</if>
<if test="phone != null">`phone` =
#{phone},
</if>
<if test="createTime != null">`create_time` =
#{createTime},
</if>
<if test="createBy != null">`create_by` =
#{createBy},
</if>
<if test="updateTime != null">`update_time` =
#{updateTime},
</if>
<if test="updateBy != null">`update_by` =
#{updateBy},
</if>
<if test="remark != null">`remark` =
#{remark},
</if>
<if test="enable != null">`enable` =
#{enable},
</if>
<if test="login != null">`login` =
#{login},
</if>
<if test="lastTime != null">`last_time` =
#{lastTime},
</if>
</trim>
where `user_id` = #{userId}
</update>
<delete id="deleteUserById" parameterType="String">
delete
from `user`
where `user_id` = #{userId}
</delete>
<delete id="deleteUserByIds" parameterType="String">
delete from `user` where `user_id` in
<foreach item="userId" collection="array" open="(" separator="," close=")">
#{userId}
</foreach>
</delete>
</mapper>
package com.skpang.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import com.skpang.demo.domain.User;
/**
* 用户Mapper接口
*
* @author skpang
* @date 2022-03-12
*/
@Mapper
public interface UserMapper {
/**
* 查询用户
*
* @param userId 用户管理ID
* @return 用户
*/
User selectUserById(String userId);
/**
* 查询用户列表
*
* @param user 用户
* @return 用户集合
*/
List<User> selectUserList(User user);
/**
* 新增用户
*
* @param user 用户
* @return 结果
*/
int insertUser(User user);
/**
* 修改用户
*
* @param user 用户
* @return 结果
*/
int updateUser(User user);
/**
* 删除用户
*
* @param userId 用户ID
* @return 结果
*/
int deleteUserById(String userId);
/**
* 批量删除用户
*
* @param userIds 需要删除的数据ID
* @return 结果
*/
int deleteUserByIds(String[] userIds);
}
package com.skpang.demo.service;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.skpang.demo.domain.PageDomain;
import com.skpang.demo.domain.User;
/**
* 用户Service接口
*
* @author skpang
* @date 2022-03-12
*/
public interface IUserService {
/**
* 查询用户
*
* @param userId 用户ID
* @return 用户
*/
User selectUserById(String userId);
/**
* 查询用户管理
* @param ${classsName} 用户
* @param pageDomain
* @return 用户分页集合
* */
PageInfo<User> selectUserPage(User user, PageDomain pageDomain);
/**
* 查询用户管理列表
*
* @param user 用户
* @return 用户集合
*/
List<User> selectUserList(User user);
/**
* 新增用户
*
* @param user 用户
* @return 结果
*/
int insertUser(User user);
/**
* 修改用户
*
* @param user 用户
* @return 结果
*/
int updateUser(User user);
/**
* 批量删除用户
*
* @param ids 需要删除的数据ID
* @return 结果
*/
int deleteUserByIds(String[] ids);
/**
* 删除用户信息
*
* @param userId 用户管理ID
* @return 结果
*/
int deleteUserById(String userId);
}
package com.skpang.demo.service.impl;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skpang.demo.domain.PageDomain;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.skpang.demo.mapper.UserMapper;
import com.skpang.demo.domain.User;
import com.skpang.demo.service.IUserService;
/**
* 用户Service业务层处理
*
* @author jmys
* @date 2022-03-12
*/
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
/**
* 查询用户
*
* @param userId 用户ID
* @return 用户
*/
@Override
public User selectUserById(String userId) {
return userMapper.selectUserById(userId);
}
/**
* 查询用户列表
*
* @param user 用户
* @return 用户
*/
@Override
public List<User> selectUserList(User user) {
return userMapper.selectUserList(user);
}
/**
* 查询用户
* @param user 用户
* @param pageDomain
* @return 用户 分页集合
* */
@Override
public PageInfo<User> selectUserPage(User user, PageDomain pageDomain) {
PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
List<User> data = userMapper.selectUserList(user);
return new PageInfo<>(data);
}
/**
* 新增用户
*
* @param user 用户
* @return 结果
*/
@Override
public int insertUser(User user) {
user.setCreateTime(LocalDateTime.now());
return userMapper.insertUser(user);
}
/**
* 修改用户
*
* @param user 用户
* @return 结果
*/
@Override
public int updateUser(User user) {
user.setUpdateTime(LocalDateTime.now());
return userMapper.updateUser(user);
}
/**
* 删除用户对象
*
* @param ids 需要删除的数据ID
* @return 结果
*/
@Override
public int deleteUserByIds(String[] ids) {
return userMapper.deleteUserByIds(ids);
}
/**
* 删除用户信息
*
* @param userId 用户ID
* @return 结果
*/
@Override
public int deleteUserById(String userId) {
return userMapper.deleteUserById(userId);
}
}
package com.skpang.demo.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
@Data
public class User extends BaseDomain{
private static final long serialVersionUID = 1L;
/**
* 编号
*/
@TableField(value = "user_id", fill = FieldFill.INSERT_UPDATE)
private String userId;
/**
* 账户
*/
@TableField(value = "username", fill = FieldFill.INSERT_UPDATE)
private String username;
/**
* 密码
*/
@TableField(value = "password", fill = FieldFill.INSERT_UPDATE)
private String password;
/**
* 盐
*/
@TableField(value = "salt", fill = FieldFill.INSERT_UPDATE)
private String salt;
/**
* 状态
*/
@TableField(value = "status", fill = FieldFill.INSERT_UPDATE)
private String status;
/**
* 姓名
*/
@TableField(value = "real_name", fill = FieldFill.INSERT_UPDATE)
private String realName;
/**
* 邮箱
*/
@TableField(value = "email", fill = FieldFill.INSERT_UPDATE)
private String email;
/**
* 头像
*/
@TableField(value = "avatar", fill = FieldFill.INSERT_UPDATE)
private String avatar;
/**
* 性别
*/
@TableField(value = "sex", fill = FieldFill.INSERT_UPDATE)
private String sex;
/**
* 电话
*/
@TableField(value = "phone", fill = FieldFill.INSERT_UPDATE)
private String phone;
/**
* 是否启用
*/
@TableField(value = "enable", fill = FieldFill.INSERT_UPDATE)
private String enable;
/**
* 是否登录
*/
@TableField(value = "login", fill = FieldFill.INSERT_UPDATE)
private String login;
}
package com.skpang.demo.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;
@Data
public class BaseDomain implements Serializable {
/**
* 创建时间
*/
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 创建人
*/
@TableField(value = "create_by", fill = FieldFill.INSERT)
private String createBy;
/**
* 创建人名称
*/
private String createName;
/**
* 修改时间
*/
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 修改人
*/
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 修改人名称
*/
private String updateName;
/**
* 备注
*/
private String remark;
/**
* 请求参数
*/
private Map<String, Object> params;
}
package com.skpang.demo.domain;
@Data
public class PageDomain {
/**
* 当前页
*/
private Integer page;
/**
* 每页数量
*/
private Integer limit;
/**
* 获取开始的数据行
*/
public Integer start() {
return (this.page - 1) * this.limit;
}
/**
* 获取结束的数据行
*/
public Integer end() {
return this.page * this.limit;
}
}
package com.skpang.demo.domain;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
/**
* 是 否 成 功
*/
private boolean success;
/**
* 错 误 状 态
*/
private int code;
/**
* 错 误 消 息
*/
private String msg;
/**
* 返 回 数 据
*/
private T data;
/**
* 成 功 操 作
*/
public static <T> Result<T> success() {
return success(null);
}
/**
* 成 功 操 作 , 携 带 数 据
*/
public static <T> Result<T> success(T data) {
return success(ResultCode.SUCCESS.getMessage(), data);
}
/**
* 成 功 操 作, 携 带 消 息
*/
public static <T> Result<T> success(String message) {
return success(message, null);
}
/**
* 成 功 操 作, 携 带 消 息 和 携 带 数 据
*/
public static <T> Result<T> success(String message, T data) {
return success(ResultCode.SUCCESS.getCode(), message, data);
}
/**
* 成 功 操 作, 携 带 自 定 义 状 态 码 和 消 息
*/
public static <T> Result<T> success(int code, String message) {
return success(code, message, null);
}
/**
* 成 功 操 作, 携 带 自 定义 状 态 码, 消 息 和 数 据
*/
public static <T> Result<T> success(int code, String message, T data) {
Result<T> result = new Result<T>();
result.setCode(code);
result.setMsg(message);
result.setSuccess(true);
result.setData(data);
return result;
}
/**
* 失 败 操 作, 默 认 数 据
*/
public static <T> Result<T> failure() {
return failure(ResultCode.SUCCESS.getMessage());
}
/**
* 失 败 操 作, 携 带 自 定 义 消 息
*/
public static <T> Result<T> failure(String message) {
return failure(message, null);
}
/**
* 失 败 操 作, 携 带 自 定 义 消 息 和 数 据
*/
public static <T> Result<T> failure(String message, T data) {
return failure(ResultCode.FAILURE.getCode(), message, data);
}
/**
* 失 败 操 作, 携 带 自 定 义 状 态 码 和 自 定 义 消 息
*/
public static <T> Result<T> failure(int code, String message) {
return failure(ResultCode.FAILURE.getCode(), message, null);
}
/**
* 失 败 操 作, 携 带 自 定 义 状 态 码 , 消 息 和 数 据
*/
public static <T> Result<T> failure(int code, String message, T data) {
Result<T> result = new Result<T>();
result.setCode(code);
result.setMsg(message);
result.setSuccess(false);
result.setData(data);
return result;
}
/**
* Boolean 返 回 操 作, 携 带 默 认 返 回 值
*/
public static <T> Result<T> decide(boolean b) {
return decide(b, ResultCode.SUCCESS.getMessage(), ResultCode.FAILURE.getMessage());
}
/**
* Boolean 返 回 操 作, 携 带 自 定 义 消 息
*/
public static <T> Result<T> decide(boolean b, String success, String failure) {
if (b) {
return success(success);
} else {
return failure(failure);
}
}
}
package com.skpang.demo.domain;
import lombok.Getter;
public enum ResultCode {
/**
* 成功
*/
SUCCESS(200, "操作成功"),
/**
* 失败
*/
FAILURE(500, "操作失败");
/**
* 状 态 码
*/
@Getter
private final int code;
/**
* 携 带 消 息
*/
@Getter
private final String message;
/**
* 构 造 方 法
*/
ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
}
package com.skpang.demo.domain;
import lombok.Data;
import java.io.Serializable;
@Data
public class ResultTable implements Serializable {
/**
* 状态码
*/
private Integer code;
/**
* 提示消息
*/
private String msg;
/**
* 消息总量
*/
private Long count;
/**
* 数据对象
*/
private Object data;
/**
* 构 建
*/
public static ResultTable pageTable(long count, Object data) {
ResultTable resultTable = new ResultTable();
resultTable.setData(data);
resultTable.setCode(0);
resultTable.setCount(count);
return resultTable;
}
public static ResultTable dataTable(Object data) {
ResultTable resultTable = new ResultTable();
resultTable.setData(data);
resultTable.setCode(0);
return resultTable;
}
}
package com.skpang.demo.controller;
import com.github.pagehelper.PageInfo;
import com.skpang.demo.domain.PageDomain;
import com.skpang.demo.domain.Result;
import com.skpang.demo.domain.ResultTable;
import com.skpang.demo.domain.User;
import com.skpang.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
/**
* 查询用户列表
*/
@ResponseBody
@GetMapping("/data")
public ResultTable list(@ModelAttribute User user, PageDomain pageDomain) {
PageInfo<User> pageInfo = userService.selectUserPage(user, pageDomain);
return pageTable(pageInfo.getList(), pageInfo.getTotal());
}
/**
* 新增保存用户
*/
@ResponseBody
@PostMapping("/save")
public Result save(@RequestBody User user) {
return decide(userService.insertUser(user));
}
/**
* 修改保存用户
*/
@ResponseBody
@PutMapping("/update")
public Result update(@RequestBody User user) {
return decide(userService.updateUser(user));
}
/**
* 删除用户
*/
@ResponseBody
@DeleteMapping("/batchRemove")
public Result batchRemove(String ids) {
return decide(userService.deleteUserByIds(ids.split(",")));
}
/**
* 删除
*/
@ResponseBody
@DeleteMapping("/remove/{userId}")
public Result remove(@PathVariable("userId") String userId) {
return decide(userService.deleteUserById(userId));
}
/**
* Describe: 返回数据表格数据 分页
* Param data
* Return 表格分页数据
*/
protected static ResultTable pageTable(Object data, long count) {
return ResultTable.pageTable(count, data);
}
/**
* 选择返回
*/
public Result decide(Boolean b) {
return Result.decide(b);
}
/**
* 选择返回
*/
public Result decide(Boolean b, String success, String failure) {
return Result.decide(b, success, failure);
}
/**
* 选择返回
*/
public Result decide(int result) {
if (result > 0) {
return Result.decide(true);
} else {
return Result.decide(false);
}
}
/**
* 选择返回
*/
public Result decide(int result, String success, String failure) {
if (result > 0) {
return Result.decide(true, success, failure);
} else {
return Result.decide(false, success, failure);
}
}
}
新增功能的运行结果
查询功能的运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。