鍓嶈█
涓轰粈涔堣瀛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>
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>
浠ヤ笂鏄墍闇€瑕佺殑渚濊禆鍖咃紝濡傛灉蹇樿寮曞叆鐨勶紝鍙互鐩存帴娣诲姞鍗冲彲锛屼笉鐢ㄥ啀閲嶆柊鍒涘缓椤圭洰浜?/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鎿嶄綔锛?-> 鏁版嵁搴擄紝濡備笅鍥炬墍绀猴細
娌℃湁鍟ュソ鐨勭敾鍥惧伐鍏凤紝灏遍殢渚跨敾浜嗕竴涓嬶紝鍙嶆鏁版嵁鐨勬祦鍚戝氨鏄繖鏍风殑锛屽ぇ瀹跺彲浠ュ噾鍚堢湅涓€涓嬨€?/p>
鍦╮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瀹炰綋鍖呯殑璺緞锛屽彲鏍规嵁鑷繁鐨勯厤缃?
閰嶇疆濂統ml鏂囦欢涔嬪悗锛屾垜浠幓鐪嬪彸渚х殑Databases鍘荤湅鐪嬭兘涓嶈兘杩炴帴鑷繁鏈湴鐨凪ySQL鏁版嵁搴擄細
渚濇鍒嗗埆濉紝鏁版嵁琛ㄥ悕瀛楋紙杩欓噷鎴戜滑鐨勮〃鏄痷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>
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>
绗簲锛氭祴璇曠粨鏋滐紙杩欓噷鎴戠敤鐨刾ostman鏉ヨ繘琛屾帴鍙f祴璇曪級
鏌ヨ鐢ㄦ埛鏁版嵁->鍦板潃涓猴細http://localhost:8080/user/queryLimit?currentPage=0&pageSize=5
娣诲姞鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/addUser
鏇存柊鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/updateUser
鏇存柊涔嬪悗鏁版嵁搴撴暟鎹负锛?br>
鍒犻櫎鐢ㄦ埛鏁版嵁-> 鍦板潃涓猴細http://localhost:8080/user/deleteUser