亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot-curd基于mybatis項目搭建

 更新時間:2022年01月25日 17:35:58   作者:捕風捉影??  
這篇文章主要介紹了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 數(shù)據(jù)源 -->
? ? ? ? <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

## 數(shù)據(jù)源配置
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 執(zhí)行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);

//?? ?根據(jù)id進行刪除數(shù)據(jù)
?? ?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 + "<<<" );
?? ??? ?//根據(jù)ID查詢
?? ??? ?User user = usermapper.selectuserbyid(id);
?? ??? ?return user;
?? ?}
//?? ?沒有檢測重復
?? ?@GetMapping("userOne/{name}")
?? ?public User sayUserOne(@PathVariable String name) {
?? ??? ?System.out.println( name + "<<<" );
?? ??? ?//根據(jù)ID查詢
?? ??? ?User user = usermapper.selectUserByName( name );
?? ??? ?//user--json
?? ??? ?return user;
?? ?}
?? ?@PutMapping("add")
?? ?public int sayAdd(User user) {
?? ??? ?System.out.println( user + "<<<" );
?? ??? ?//根據(jù)ID查詢
?? ??? ?return usermapper.insertUser( user );
?? ?}
?? ?@DeleteMapping("delete/{userId}")
?? ?public int sayDel(@PathVariable Integer userId) {
?? ??? ?System.out.println( userId + "<<<" );
?? ??? ?//根據(jù)ID查詢
?? ??? ?return usermapper.deleteUserById( userId );
?? ?}
?? ?@GetMapping("query")
?? ?public java.util.List<User> sayDel(UserQuery userQuery) {
?? ??? ?System.out.println( userQuery + "<<<" );
?? ??? ?//分頁的集合數(shù)據(jù)
?? ??? ?return usermapper.selectUserByItem( userQuery );
?? ?}

}

query

public class UserQuery {
?? ?private ?Integer pageNum=1;
?? ?private ?Integer pageSize=1;
?? ?private ?String userName;

以上只是簡單的實現(xiàn)了增刪改查 如果發(fā)生了插入或者是刪除異常,我們就應該會自定義全局異常去捕獲出現(xiàn)的問題

新增類 service 主要進行的業(yè)務的處理

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,"用戶已經(jīng)存在");
?? ??? ?//用戶添加是否成功
?? ??? ?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 ),"待刪除數(shù)據(jù)不存在" );
?? ??? ?AssertUtil.isTrue( usermapper.deleteUserById( userId )<1,"刪除失敗" );
?? ?}

?? ?/**
?? ? * 分頁參數(shù)傳遞了兩個變量 一個是起始位置 一個是一頁有多行數(shù)據(jù)
?? ? *
?? ? * @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;

?? ?}
}

新增類 用來校驗數(shù)據(jù)數(shù)據(jù)是否有誤

AssertUtil

public class AssertUtil {
?? ?/**
?? ? *
?? ? * @param flag 參數(shù)
?? ? * @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="參數(shù)異常";

?? ?public ParamException() {
?? ??? ?super("參數(shù)異常");
?? ?}

?? ?public ParamException(Integer code) {
?? ??? ?super("參數(shù)異常");
?? ??? ?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;
?? ?}
}

把數(shù)據(jù)狀態(tài)碼 狀態(tài)數(shù)據(jù)存在在Resultinfo所創(chuàng)建的對象中

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數(shù)據(jù):

到此這篇關于springboot-curd基于mybatis項目搭建的文章就介紹到這了,更多相關springboot-curd基于mybatis項目 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Spring單元測試類ApplicationTests錯誤的解決

    Spring單元測試類ApplicationTests錯誤的解決

    這篇文章主要介紹了Spring單元測試類ApplicationTests錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • java實現(xiàn)簡單的彈球游戲

    java實現(xiàn)簡單的彈球游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單的彈球游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Springboot整合Netty實現(xiàn)RPC服務器的示例代碼

    Springboot整合Netty實現(xiàn)RPC服務器的示例代碼

    這篇文章主要介紹了Springboot整合Netty實現(xiàn)RPC服務器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Spring Boot詳細打印啟動時異常堆棧信息詳析

    Spring Boot詳細打印啟動時異常堆棧信息詳析

    這篇文章主要給大家介紹了關于Spring Boot詳細打印啟動時異常堆棧信息的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-10-10
  • Java8使用Function讀取文件

    Java8使用Function讀取文件

    這篇文章主要為大家詳細介紹了Java8如何使用Function讀取文件,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2024-12-12
  • Java創(chuàng)建可執(zhí)行的Jar文件的方法實踐

    Java創(chuàng)建可執(zhí)行的Jar文件的方法實踐

    創(chuàng)建的可執(zhí)行Jar文件實際就是在原始Jar的清單文件中添加了Main-Class的配置,本文主要介紹了Java創(chuàng)建可執(zhí)行的Jar文件的方法實踐,感興趣的可以了解一下
    2023-12-12
  • 關于Java中properties文件編碼問題

    關于Java中properties文件編碼問題

    這篇文章主要介紹了關于Java中properties文件編碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java實現(xiàn)字符串和輸入流的相互轉換

    Java實現(xiàn)字符串和輸入流的相互轉換

    這篇文章主要介紹了Java實現(xiàn)字符串和輸入流的相互轉換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java多線程yield心得分享

    Java多線程yield心得分享

    前幾天復習了一下多線程,發(fā)現(xiàn)有許多網(wǎng)上講的都很抽象,所以,自己把網(wǎng)上的一些案例總結了一下
    2013-12-12
  • Java漢字轉拼音工具類完整代碼實例

    Java漢字轉拼音工具類完整代碼實例

    這篇文章主要介紹了java漢字轉拼音工具類完整代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03

最新評論