springboot-curd基于mybatis项目
捕風捉影 人气:0项目结构:
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: 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/*.xml type-aliases-package: com.liuyang.bean,com.liuyang.vo,com.liuyang.query configuration: ## 下划线转驼峰配置 map-underscore-to-camel-case: true ## pageHelper pagehelper: helper-dialect: mysql ## 显示dao 执行sql语句 logging: level: com: xxxx: mapper: debug
bean
private Integer userId; private String userName; private String userPwd;
mapper
public interface Usermapper { public User selectuserbyid(Integer id); public User selectUserByName(String userName); // 注意返回的类型 public int insertUser(User user); // 根据id进行删除数据 public int deleteUserById(int userId); //条件查询 public List<User> selectUserByItem(UserQuery userQuery); }
mapping.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.liuyang.mapper.Usermapper"> <select id="selectuserbyid" parameterType="int" resultType="com.liuyang.bean.User"> select * from t_user where user_id=#{userId} </select> <select id="selectUserByName" parameterType="String" resultType="com.liuyang.bean.User"> select * from t_user where user_name=#{userName} select * from t_user where user_name= </select> <insert id="insertUser"> insert into t_user(user_name,user_pwd) values(#{userName},#{userPwd}); </insert> <!--删除一条--> <delete id="deleteUserById" parameterType="int"> delete from t_user where user_id=#{userId} </delete> <!--条件查询--> <select id="selectUserByItem" resultType="com.liuyang.bean.User"> select * from t_user <where> <if test="userName!=null"> user_name like "%${userName}%" </if> </where> </select> </mapper>
controller
@RestController public class Usercontroller { @Resource private Usermapper usermapper; @GetMapping("one/{id}") public User sayUser(@PathVariable Integer id) { System.out.println( id + "<<<" ); //根据ID查询 User user = usermapper.selectuserbyid(id); return user; } // 没有检测重复 @GetMapping("userOne/{name}") public User sayUserOne(@PathVariable String name) { System.out.println( name + "<<<" ); //根据ID查询 User user = usermapper.selectUserByName( name ); //user--json return user; } @PutMapping("add") public int sayAdd(User user) { System.out.println( user + "<<<" ); //根据ID查询 return usermapper.insertUser( user ); } @DeleteMapping("delete/{userId}") public int sayDel(@PathVariable Integer userId) { System.out.println( userId + "<<<" ); //根据ID查询 return usermapper.deleteUserById( userId ); } @GetMapping("query") public java.util.List<User> sayDel(UserQuery userQuery) { System.out.println( userQuery + "<<<" ); //分页的集合数据 return usermapper.selectUserByItem( userQuery ); } }
query
public class UserQuery { private Integer pageNum=1; private Integer pageSize=1; private String userName;
以上只是简单的实现了增删改查 如果发生了插入或者是删除异常,我们就应该会自定义全局异常去捕获出现的问题
新增类 service
主要进行的业务的处理
Userservice
public interface Userservice { public User queryUserById(Integer userId); public User queryUserByName(String userName); public void saveUser(User user); public void changeUser(User user); //删除一条 public void removeUserById(Integer userId); //查询用户信息 public PageInfo<User> queryUserByPage(UserQuery userQuery); }
UserserviceImpl
@Service public class UserserviceImpl implements Userservice { @Resource private Usermapper usermapper; // 查询都是用的User返回类型 @Override public User queryUserById(Integer userId) { return usermapper.selectuserbyid( userId ); } @Override public User queryUserByName(String userName) { return usermapper.selectUserByName( userName ); } //增加操作 /*** * 需要用户名 AssertUtil * 用户密码 * 用户是否存在 * 添加用户是否成功 * @param user */ @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,"添加失败了"); } // 修改操作 /**** * 用户名不为空 * 用户密码不为空 * 用户不存在 * 用户修改操作失败 * * @param user */ @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,"修改失败了"); } /**** * 删除是的id是否还存在 * 删除失败 * @param userId */ @Override public void removeUserById(Integer userId) { AssertUtil.isTrue( userId==null|| null==usermapper.selectuserbyid( userId ),"待删除数据不存在" ); AssertUtil.isTrue( usermapper.deleteUserById( userId )<1,"删除失败" ); } /** * 分页参数传递了两个变量 一个是起始位置 一个是一页有多行数据 * * @param userQuery * @return */ @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 { /** * * @param flag 参数 * @param msg 信息 * flag=true * 抛出异常,自定义 */ 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
数据:
加载全部内容