欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot-curd基于mybatis項(xiàng)目搭建

 更新時(shí)間:2022年01月25日 17:35:58   作者:捕風(fēng)捉影??  
這篇文章主要介紹了springboot-curd基于mybatis項(xiàng)目搭建,圍繞相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,希望對(duì)正在學(xué)習(xí)的你有所幫助,需要的小伙伴也可以參考一下

項(xiàng)目結(jié)構(gòu):

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>
? ? ? ? <!-- ? ?測(cè)試依賴(lài)-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>junit</groupId>
? ? ? ? ? ? <artifactId>junit</artifactId>
? ? ? ? ? ? <version>4.13</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>
? ? ? ? <!-- springboot 分頁(yè)插件 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.github.pagehelper</groupId>
? ? ? ? ? ? <artifactId>pagehelper-spring-boot-starter</artifactId>
? ? ? ? ? ? <version>1.2.13</version>
? ? ? ? </dependency>
? ? ? ? <!-- mysql 驅(qū)動(dòng) -->
? ? ? ? <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>
? ? ? ? ? ? <!--當(dāng)前這個(gè)項(xiàng)目被繼承之后,這個(gè)不向下傳遞-->
? ? ? ? ? ? <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
? ? ? # 設(shè)置重啟的目錄,添加目錄的文件需要restart
? ? ? additional-paths: src/main/java
? ? ? # 解決項(xiàng)目自動(dòng)重新編譯后接口報(bào)404的問(wèn)題
? ? ? 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:
? ? ## 下劃線(xiàn)轉(zhuǎn)駝峰配置
? ? map-underscore-to-camel-case: true

## pageHelper
pagehelper:
? helper-dialect: mysql

## 顯示dao 執(zhí)行sql語(yǔ)句
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);

//?? ?注意返回的類(lèi)型
?? ?public int insertUser(User user);

//?? ?根據(jù)id進(jìn)行刪除數(shù)據(jù)
?? ?public int deleteUserById(int userId);

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

}

query

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

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

新增類(lèi) service 主要進(jìn)行的業(yè)務(wù)的處理

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);

?? ?//查詢(xún)用戶(hù)信息
?? ?public PageInfo<User> queryUserByPage(UserQuery userQuery);

}

UserserviceImpl

@Service
public class UserserviceImpl implements Userservice {

?? ?@Resource
?? ?private Usermapper usermapper;
//?? ?查詢(xún)都是用的User返回類(lèi)型
?? ?@Override
?? ?public User queryUserById(Integer userId) {
?? ??? ?return usermapper.selectuserbyid( userId );
?? ?}

?? ?@Override
?? ?public User queryUserByName(String userName) {
?? ??? ?return usermapper.selectUserByName( userName );
?? ?}
//增加操作
?? ?/***
?? ? * ?? ?需要用戶(hù)名 AssertUtil
?? ? * ?? ?用戶(hù)密碼
?? ? * ?? ?用戶(hù)是否存在
?? ? * ?? ?添加用戶(hù)是否成功
?? ? * @param user
?? ? */
?? ?@Override
?? ?public void saveUser(User user) {
?? ??? ?//驗(yàn)證用戶(hù)名
?? ??? ?AssertUtil.isTrue( StringUtils.isBlank(user.getUserName()),"用戶(hù)名不能為空");
?? ??? ?//用戶(hù)密碼
?? ??? ?AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"用戶(hù)密碼不能為空");
?? ??? ?//用戶(hù)是否存在
?? ??? ?User temp = usermapper.selectUserByName(user.getUserName());
?? ??? ?AssertUtil.isTrue(temp!=null,"用戶(hù)已經(jīng)存在");
?? ??? ?//用戶(hù)添加是否成功
?? ??? ?AssertUtil.isTrue(usermapper.insertUser(user)<1,"添加失敗了");
?? ?}

//?? ?修改操作

?? ?/****
?? ? * 用戶(hù)名不為空
?? ? * 用戶(hù)密碼不為空
?? ? * 用戶(hù)不存在
?? ? * 用戶(hù)修改操作失敗
?? ? *
?? ? * @param user
?? ? */

?? ?@Override
?? ?public void changeUser(User user) {
?? ??? ?AssertUtil.isTrue( StringUtils.isBlank( user.getUserName() ),"用戶(hù)名不為空" );
?? ??? ?AssertUtil.isTrue( StringUtils.isBlank( user.getUserPwd() ),"密碼不為空" );
?? ??? ?User temp = usermapper.selectuserbyid(user.getUserId());
?? ??? ?AssertUtil.isTrue( temp == null,"用戶(hù)不存在");
?? ??? ?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,"刪除失敗" );
?? ?}

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

?? ?}
}

新增類(lèi) 用來(lái)校驗(yàn)數(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 自定義異常類(lèi)

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)建的對(duì)象中

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ù):

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

相關(guān)文章

  • Spring單元測(cè)試類(lèi)ApplicationTests錯(cuò)誤的解決

    Spring單元測(cè)試類(lèi)ApplicationTests錯(cuò)誤的解決

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

    java實(shí)現(xiàn)簡(jiǎn)單的彈球游戲

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

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

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

    Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息詳析

    這篇文章主要給大家介紹了關(guān)于Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java8使用Function讀取文件

    Java8使用Function讀取文件

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

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

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

    關(guān)于Java中properties文件編碼問(wèn)題

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

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

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

    Java多線(xiàn)程yield心得分享

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

    Java漢字轉(zhuǎn)拼音工具類(lèi)完整代碼實(shí)例

    這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類(lèi)完整代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論