springboot-curd基於mybatis項目搭建
項目結構:
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
數據:
分頁查詢:
到此這篇關於springboot-curd基於mybatis項目搭建的文章就介紹到這瞭,更多相關springboot-curd基於mybatis項目 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java MyBatis框架環境搭建詳解
- SpringBoot框架整合Mybatis簡單攻略
- Spring框架基於註解開發CRUD詳解
- SpringBoot MyBatis簡單快速入門例子
- Redis實現短信驗證碼登錄的示例代碼