当前位置: 首页>后端>正文

最新版SpringBoot整合Mybatis,实现增删改查(CRUD)

鍓嶈█

涓轰粈涔堣瀛pringBoot鍜孧ybatis鍛紝鎴戣寰椾綔涓轰竴涓悎鏍肩殑鍚庣绋嬪簭鍛樺鍒犳敼鏌ワ紙CRUD锛夎偗瀹氭槸瑕佷細鐨勶紝鑰屼笖鏄渶鍩烘湰鐨勪竴椤规妧鑳斤紝涓嶇劧鎬庝箞瀹屾垚缁欎綘鍒嗛厤鐨勯渶姹傚憿锛涜€屼负浠€涔堥€夋嫨SpringBoot鍜孧ybatis鍛紝棣栧厛閫夋嫨SpringBoot鏄彲浠ョ悊瑙g殑锛屾瘯绔熶綔涓轰竴娆捐交閲忓瀷鐨勬鏋讹紝闆嗘垚浜嗗緢澶氱粍浠跺拰Tomcat鏈嶅姟鍣紝璁╂垜浠殑寮€鍙戞洿绠€渚匡紝褰撶劧鏈€涓昏鐨勮繕鏄洜涓哄崥涓诲彧浼歋pringBoot 馃お銆傚ソ浜嗗紑涓帺绗戯紝涓嬮潰璇翠负鍟ョ敤SpringBoot鍔燤ybatis锛屽洜涓洪櫎浜哅ybatis杩樻湁Mybatis-Plus鍜孒ibernate鍙互閫夋嫨锛岃€屼笖杩欎袱涓皝瑁呬簡CRUD鏂规硶锛岄兘涓嶇敤鑷繁鍐檚ql璇彞锛屽紑鍙戣捣鏉ユ洿鍔犲揩鎹凤紱鑰孧ybatis鍒欐槸瑕佽嚜宸辩紪鍐檚ql璇彞鐨勶紝瀵逛簬褰撴椂鐨勬垜鏉ヨ鎴戣寰楄繕鏄鐨勫熀纭€鐐癸紝姣曠珶鍚庨潰杩欎袱涓彲浠ョ敤鏉ヨ繘闃讹紝涔熷璧锋潵姣曠珶绠€鍗曪紝鎵€浠ヨ鎴戜滑寮€濮嬬敤SpringBoot鏁村悎Mybatis鍚э紒

绗竴锛氬垱寤篗ySQL鏁版嵁搴?/h2>

棣栧厛鎴戜滑搴旇鍏堝垱寤轰竴涓暟鎹簱锛岀敤浜庢搷浣滄垜浠珻RUD鐨勬暟鎹紙鍙互浣跨敤鍙鍖栧伐鍏疯繘琛屽垱寤猴紝涔熷彲浠ヤ娇鐢⊿QL鍛戒护杩涜鍒涘缓锛夛紝鏁版嵁搴撶殑鍚嶅瓧鍙互鑷繁瀹氫箟锛屽垱寤哄畬鏁版嵁搴撲箣鍚庯紝鎴戜滑灏辫鍒涘缓琛ㄨ繘琛屾暟鎹殑瀛樺偍浜嗭紝琛ㄧ殑鍚嶅瓧鍙€?user 鈥樿〃锛屽瓧娈靛彲浠ヨ嚜宸辫缃紝鍙鍜屽悗杈硅嚜宸卞垱寤虹殑瀹炰綋瀵瑰簲灏卞ソ浜嗐€?/p>

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第1张
鎴睆2023-03-24 14.01.31.png
CREATE TABLE `user` (
  `userId` bigint NOT NULL AUTO_INCREMENT,
  `userName` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `userAddress` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

绗簩锛氬垱寤篠pringBoot椤圭洰锛屽紩鍏ラ渶瑕佺殑渚濊禆鍖?/h2>

鐢↖DEA鍒涘缓SpringBoot椤圭洰锛?br> 1銆侀€夋嫨Spring Initializr鍒涘缓锛岀劧鍚嶫DK閫夋嫨1.8锛孞ava8鐗堟湰銆?br> 2銆佸垵濮嬪寲缁勪欢閫夋嫨Spring Web銆丣DBC API銆丮ybatis Framework銆丮ySQL Driver锛孡ombok涔熷彲浠ラ€夋嫨涓€涓嬶紝鍙兘浼氱敤鍒般€?br> 3銆侀€夋嫨濂戒簡涔嬪悗鐐瑰嚮Finish鍒涘缓灏卞ソ浜嗐€?br> 4銆佸垱寤哄ソ浜哠pringBoot涔嬪悗鐪嬬湅鑷繁鐨凪aven鏈夋病鐢ㄩ厤缃ソ锛屽鏋滄病鏈夐厤缃ソ鍘绘煡璇㈠浣曢厤缃甅aven锛屼笉鐒朵綘鐨刾om渚濊禆鏄姞杞戒笉浜嗙殑锛屽氨涓嬭浇涓€涓婱aven锛屽浣曟崲涓樋閲屾簮闀滃儚鍦板潃鍜屾湰鍦颁粨搴擄紝鐒跺悗IDEA閫夋嫨涓€涓婱aven鐨剎ml鏂囦欢鍜屾湰鍦颁粨搴撳湴鍧€灏卞ソ浜嗭紝涓嶈繃鎴戣寰楄兘鎼滅储杩欑瘒鏂囩珷鐨勬湅鍙嬭繖浜涘熀纭€鐨勪笢瑗垮簲璇ユ槸閰嶇疆濂戒簡鐨勩€?/p>

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第2张
鎴睆2023-03-24 14.05.09.png
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第3张
鎴睆2023-03-24 14.05.24.png
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第4张
鎴睆2023-03-24 14.05.39.png
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第5张
鎴睆2023-03-24 14.05.55.png

浠ヤ笂鏄墍闇€瑕佺殑渚濊禆鍖咃紝濡傛灉蹇樿寮曞叆鐨勶紝鍙互鐩存帴娣诲姞鍗冲彲锛屼笉鐢ㄥ啀閲嶆柊鍒涘缓椤圭洰浜?/p>

!--web鐩稿叧-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--mybatis鐩稿叧-->
    <dependency><!--boot鏁村悎mybatis鎵€闇€瑕佺殑鍖?->
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!--mysql鐩稿叧-->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.31</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency><!--lombok娉ㄨВ-->
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

绗笁锛氬垱寤虹▼搴忕洰褰曞拰閰嶇疆鏍稿績application.xml鏂囦欢

棣栧厛闇€瑕佸垱寤哄洓涓枃浠跺す锛屽垎鍒槸锛欵ntity锛堢敤鎴峰疄浣擄級銆丆ontroller銆丼ervice銆丏ao锛岀劧鍚庤繕闇€瑕佸湪 resource 鐩綍涓嬫柊寤?Mapper 鐩綍锛岃繖涓?Mapper 鐩綍鏄敤鏉ュ瓨鏀?SQL 璇彞鐨勫湴鏂广€傚埌杩欓噷鎴戣寰楅渶瑕佺悊瑙d竴涓婱VC妯″紡锛岀敱浜庢垜浠殑璁块棶涓€灞傝皟鐢ㄤ竴灞傦紝鐒跺悗鏌ヨ鍒版暟鎹箣鍚庡啀杩斿洖杩囧幓鐨勶紝鎵€浠ユ垜浠殑灞傜骇搴旇鏄細Controller锛堟帶鍒跺眰锛屼篃鏄帴鏀跺墠绔紶閫掑弬鏁扮殑灞傦級 -> Service锛堜笟鍔″眰锛屾垜浠墍鏈夌殑涓氬姟锛屽鍒ゆ柇鍜屼竴浜涘惊鐜鍙ュ氨闇€瑕佸啓鍒拌繖涓眰褰撲腑锛?-> Dao锛堟寔涔呭眰锛岃繖涓眰涓昏鏄拰鏁版嵁搴撴墦浜ら亾鐨勶紝涓昏璐熻矗鏁版嵁鐨勬搷浣滐紝涔熷氨鏄疌RUD鎿嶄綔锛?-> 鏁版嵁搴擄紝濡備笅鍥炬墍绀猴細


最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第6张
鎴睆2023-03-24 14.07.47.png

娌℃湁鍟ュソ鐨勭敾鍥惧伐鍏凤紝灏遍殢渚跨敾浜嗕竴涓嬶紝鍙嶆鏁版嵁鐨勬祦鍚戝氨鏄繖鏍风殑锛屽ぇ瀹跺彲浠ュ噾鍚堢湅涓€涓嬨€?/p>

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第7张
鎴睆2023-03-24 14.08.52.png

鍦╮esources鏂囦欢澶逛笅鍒涘缓yml鏂囦欢锛屽苟杈撳叆濡備笅鍐呭锛?
server:
  port: 8080    //鏈満鐨勭鍙e彿

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/鑷繁鏁版嵁搴撶殑鍚嶇О?useUnicode=true & characterEncoding=utf-8 &
      useSSL=true & serverTimezone=Asia/Shanghai
    username: root      //鏁版嵁搴撶殑鐢ㄦ埛鍚? 
    password: xxxxxx    //鏁版嵁搴撶殑瀵嗙爜

mybatis:
  mapper-locations: classpath:/Mapper/*.xml   
  type-aliases-package: com.example.test.Entity    //杩欎釜鏄壂鎻忓埌Entity瀹炰綋鍖呯殑璺緞锛屽彲鏍规嵁鑷繁鐨勯厤缃?
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第8张
鎴睆2023-03-24 14.10.39.png

閰嶇疆濂統ml鏂囦欢涔嬪悗锛屾垜浠幓鐪嬪彸渚х殑Databases鍘荤湅鐪嬭兘涓嶈兘杩炴帴鑷繁鏈湴鐨凪ySQL鏁版嵁搴擄細


最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第9张
鎴睆2023-03-24 14.11.38.png

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第10张
鎴睆2023-03-24 14.12.24.png

渚濇鍒嗗埆濉紝鏁版嵁琛ㄥ悕瀛楋紙杩欓噷鎴戜滑鐨勮〃鏄痷ser琛級锛岀敤鎴峰悕鍜屽瘑鐮併€佽繕鏈夋暟鎹簱鍚?/p>

绗洓锛氫緷娆$紪鍐橢ntity銆丏ao銆丼ervice銆丆ontroller灞傦紝杩樻湁鍒涘缓mapper.xml鏂囦欢

Entity鍖咃細鍒涘缓涓€涓猆serEntity绫伙紝鍐呭濡備笅锛?/p>

public class UserEntity {
    private Integer userId;
    private String userName;
    private String userAddress;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
}

Dao鍖咃細鍒涘缓涓€涓猆serDao鎺ュ彛锛屽唴瀹瑰涓嬶細

import com.example.test.Entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserDao {
     List<UserEntity> queryLimit(Integer currentPage, Integer pageSize);

     Integer addUser(UserEntity user);

     Integer updateUser(UserEntity user);

     Integer deleteUser(UserEntity user);
}

Service鍖咃細鍒涘缓涓€涓猆serService绫伙紝鍐呭濡備笅锛?/p>

import com.example.test.Dao.UserDao;
import com.example.test.Entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserService")
public class UserService {
    @Autowired
    private UserDao userDao;

    public List<UserEntity> queryLimit(Integer currentPage,Integer pageSzie){
        return userDao.queryLimit(currentPage,pageSzie);
    }

    public Integer addUser(UserEntity user){
        return userDao.addUser(user);
    }

    public Integer updateUser(UserEntity user){
        return userDao.updateUser(user);
    }

    public Integer deleteUser(UserEntity user){
        return userDao.deleteUser(user);
    }
}

Controller鍖咃細鍒涘缓涓€涓猆serController绫伙紝鍐呭涓猴細

import com.example.test.Entity.UserEntity;
import com.example.test.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    String message = "";
    @RequestMapping("/queryLimit")
    public List<UserEntity> queryLimit(@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        return userService.queryLimit(currentPage,pageSize);
    }

    @PostMapping("/addUser")
    public String addUer(@RequestBody UserEntity user){
        //鐢∕ybatis鎵цinsert璇彞鐨勬椂鍊欙紝鎻掑叆鎴愬姛浼氳繑鍥?锛屼笉鎴愬姛鍒欎細鎶涘嚭寮傚父锛屾崟鑾蜂竴涓嬪紓甯稿氨濂?
        try {
            userService.addUser(user);
            message = "澧炲姞鐢ㄦ埛鎴愬姛";
        }catch (Exception exception){
            message = "澧炲姞鐢ㄦ埛寮傚父";
        }
        return message;
    }

    @PutMapping("/updateUser")
    public String updateUser(@RequestBody UserEntity user){
        //Mybatis鐨勬洿鏂版搷浣滄垚鍔熻繑鍥?锛岀敤鎴蜂笉瀛樺湪杩斿洖0锛屽け璐ュ垯鎶涘紓甯?
        try {
            message = userService.updateUser(user) == 1?"鏇存柊鐢ㄦ埛鎴愬姛":"鐢ㄦ埛涓嶅瓨鍦紝鏇存柊澶辫触";
        }catch (Exception exception){
            message = "鏇存柊寮傚父";
        }
        return message;
    }

    @DeleteMapping("/deleteUser")
    public String deleteUser(@RequestBody UserEntity user){
        //Mybatis鐨勫垹闄ゆ搷浣滃拰鏇存柊杩斿洖鍊间竴鏍凤紝鎴愬姛杩斿洖1锛岀敤鎴蜂笉瀛樺湪杩斿洖0锛屽け璐ュ垯鎶涘紓甯?
        try {
            message = userService.deleteUser(user) == 1?"鍒犻櫎鐢ㄦ埛鎴愬姛":"鐢ㄦ埛涓嶅瓨鍦紝鍒犻櫎澶辫触";
        }catch (Exception exception){
            message = "鍒犻櫎寮傚父";
        }
        return message;
    }
}

缂栧啓瀹屼笂闈㈢殑鍐呭涔嬪悗鎴戜滑闇€瑕佸湪Mapper鏂囦欢澶逛笅鍒涘缓mapper.xml鏂囦欢锛屽涓嬪浘锛?/p>

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第11张
鎴睆2023-03-24 14.15.16.png

mapper.xml鏂囦欢鍐呭濡備笅锛氶渶瑕佹敞鎰忕殑鍦版柟鏄痭amespace锛氳繖涓矾寰勬槸浣犵殑UserDao鎺ュ彛鐨勮矾寰勶紝鍥犱负浣犱紶杩囨潵鐨勬暟鎹渶瑕佸拰xml杩涜涓€涓粦瀹氾紝杩欐牱浣犵紪鍐欑殑SQL璇彞鎵嶈兘鎺ユ敹鍒颁綘鍓嶇浼犺繃鏉ョ殑鏁版嵁锛堝ぇ浣撴剰鎬濆彲浠ヨ繖涔堢悊瑙o級锛岃€宨d鍒欐槸鍜屼綘UserDao涓殑鎺ュ彛鏂规硶鐩稿搴旓紝姣斿鎴戠殑UserDao鎺ュ彛涓殑鏌ユ壘鏂规硶鍛藉悕鏄痲ueryLimit锛岄偅涔堟垜鍦▁ml鏂囦欢涓殑鏌ヨ璇彞鐨刬d灏卞簲璇ュ拰queryLimit鎯冲搴旓紝鍗筹細id=鈥渜ueryLimit鈥濄€?br> resultType鏄繑鍥炴暟鎹殑绫诲瀷
parameterType鍒欐槸浼犲叆鐨勬暟鎹被鍨?br> 鍏蜂綋杩欎簺缁嗚妭灏变笉璧樿堪浜嗭紝鍙啓瀹炵幇鐨勫姛鑳斤紝鎯充簡瑙g殑灏忎紮浼村彲浠ュ幓鐪婱ybatis鐨勫畼鏂规枃妗?/p>

<?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.example.test.Dao.UserDao">
    <select id="queryLimit" resultType="UserEntity">
        select * from user limit #{currentPage},#{pageSize};
    </select>

    <insert id="addUser" parameterType="UserEntity">
        insert into user(userName,userAddress) values(#{userName},#{userAddress});
    </insert>

    <update id="updateUser" parameterType="UserEntity">
        update user set userName=#{userName},userAddress=#{userAddress} where userId=#{userId};
    </update>

    <delete id="deleteUser" parameterType="UserEntity">
        delete from user where userId=#{userId};
    </delete>
</mapper>

鏈€鍚庢垜浠渶瑕佸湪鍚姩绫诲姞涓€鐐逛笢瑗匡紙MapperScan鎵弿鐨勬槸鎴戜滑Dao鍖呯殑鍦板潃锛屽~鍐欒嚜宸辩殑灏卞ソ锛?/p>

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第12张
鎴睆2023-03-24 14.16.42.png

绗簲锛氭祴璇曠粨鏋滐紙杩欓噷鎴戠敤鐨刾ostman鏉ヨ繘琛屾帴鍙f祴璇曪級

鏌ヨ鐢ㄦ埛鏁版嵁->鍦板潃涓猴細http://localhost:8080/user/queryLimit?currentPage=0&pageSize=5

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第13张
鎴睆2023-03-24 14.17.32.png

娣诲姞鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/addUser
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第14张
鎴睆2023-03-24 14.17.57.png
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第15张
鎴睆2023-03-24 14.18.14.png

鏇存柊鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/updateUser

最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第16张
鎴睆2023-03-24 14.19.22.png

鏇存柊涔嬪悗鏁版嵁搴撴暟鎹负锛?br>
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第17张
鎴睆2023-03-24 14.19.52.png

鍒犻櫎鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/deleteUser
最新版SpringBoot整合Mybatis,实现增删改查(CRUD),第18张
鎴睆2023-03-24 14.20.31.png

https://www.xamrdz.com/backend/3cf1942771.html

相关文章: