springboot-curd基于mybatis项目搭建
短信预约 -IT技能 免费直播动态提醒
项目结构:
pom.xml文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.liuyang</groupId>
<artifactId>springbootcurd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootcurd</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<!-- 测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- springboot 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- c3p0 数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--当前这个项目被继承之后,这个不向下传递-->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
yml
## 数据源配置
spring:
## 热部署配置
devtools:
restart:
enabled: true
# 设置重启的目录,添加目录的文件需要restart
additional-paths: class="lazy" data-src/main/java
# 解决项目自动重新编译后接口报404的问题
poll-interval: 3000
quiet-period: 1000
datasource:
type: com.mchange.v2.c3p0.ComboPooledDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
##视图的配置
freemarker:
template-loader-path: classpath*:/views/
charset: UTF-8
content-type: text/html
cache: false
suffix: .ftl
## mybatis 配置
mybatis:
#映射文件的存放路径
mapper-locations: classpath*:/mapper
@Override
public void saveUser(User user) {
//验证用户名
AssertUtil.isTrue( StringUtils.isBlank(user.getUserName()),"用户名不能为空");
//用户密码
AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"用户密码不能为空");
//用户是否存在
User temp = usermapper.selectUserByName(user.getUserName());
AssertUtil.isTrue(temp!=null,"用户已经存在");
//用户添加是否成功
AssertUtil.isTrue(usermapper.insertUser(user)<1,"添加失败了");
}
// 修改操作
@Override
public void changeUser(User user) {
AssertUtil.isTrue( StringUtils.isBlank( user.getUserName() ),"用户名不为空" );
AssertUtil.isTrue( StringUtils.isBlank( user.getUserPwd() ),"密码不为空" );
User temp = usermapper.selectuserbyid(user.getUserId());
AssertUtil.isTrue( temp == null,"用户不存在");
AssertUtil.isTrue( usermapper.updateUser(user)<1,"修改失败了");
}
@Override
public void removeUserById(Integer userId) {
AssertUtil.isTrue( userId==null|| null==usermapper.selectuserbyid( userId ),"待删除数据不存在" );
AssertUtil.isTrue( usermapper.deleteUserById( userId )<1,"删除失败" );
}
@Override
public PageInfo<User> queryUserByPage(UserQuery userQuery) {
PageHelper.startPage( userQuery.getPageNum(),userQuery.getPageSize());
List<User> ulist = usermapper.selectUserByItem( userQuery );
PageInfo<User> plist =new PageInfo<User>(ulist);
return plist;
}
}
新增类 用来校验数据数据是否有误
AssertUtil
public class AssertUtil {
public static void isTrue(Boolean flag,String msg){
if(flag){
throw new ParamException(msg);
}
}
}
ParamException
自定义异常类
public class ParamException extends RuntimeException{
private Integer code=300;
private String msg="参数异常";
public ParamException() {
super("参数异常");
}
public ParamException(Integer code) {
super("参数异常");
this.code=code;
}
public ParamException(String msg) {
super(msg);
this.msg=msg;
}
public ParamException(Integer code,String msg) {
super(msg);
this.msg=msg;
this.code=code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
把数据状态码 状态数据存在在Resultinfo
所创建的对象中
ResultInfo
public class ResultInfo {
private Integer code=200;
private String msg="操作成功";
private Object object;
public ResultInfo() {
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
@Override
public String toString() {
return "ResultInfo{" +
"code=" + code +
", msg='" + msg + '\'' +
", object=" + object +
'}';
}
}
更新传递的事json
数据:
到此这篇关于springboot-curd基于mybatis项目搭建的文章就介绍到这了,更多相关springboot-curd基于mybatis项目 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341