-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c7207cb
Showing
50 changed files
with
15,790 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
**/temp | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Vue ### | ||
**/node_modules | ||
**/.idea | ||
dist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
# 第七节 | ||
|
||
## postman下载 | ||
|
||
https://app.getpostman.com/app/download/win64 | ||
|
||
## postman测试接口 | ||
|
||
 | ||
|
||
 | ||
|
||
## UserController.java | ||
|
||
``` | ||
package com.qingge.springboot.controller; | ||
import com.qingge.springboot.entity.User; | ||
import com.qingge.springboot.mapper.UserMapper; | ||
import com.qingge.springboot.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 UserMapper userMapper; | ||
@Autowired | ||
private UserService userService; | ||
// 新增和修改 | ||
@PostMapping | ||
public Integer save(@RequestBody User user) { | ||
// 新增或者更新 | ||
return userService.save(user); | ||
} | ||
// 查询所有数据 | ||
@GetMapping | ||
public List<User> index() { | ||
List<User> all = userMapper.findAll(); | ||
return all; | ||
} | ||
@DeleteMapping("/{id}") | ||
public Integer delete(@PathVariable Integer id) { | ||
return userMapper.deleteById(id); | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
## UserMapper.java | ||
|
||
``` | ||
package com.qingge.springboot.mapper; | ||
import com.qingge.springboot.entity.User; | ||
import org.apache.ibatis.annotations.*; | ||
import java.util.List; | ||
@Mapper | ||
public interface UserMapper { | ||
@Select("SELECT * from sys_user") | ||
List<User> findAll(); | ||
@Insert("INSERT into sys_user(username, password,nickname,email,phone,address) VALUES (#{username}, #{password}," + | ||
" #{nickname}, #{email},#{phone}, #{address})") | ||
int insert(User user); | ||
int update(User user); | ||
@Delete("delete from sys_user where id = #{id}") | ||
Integer deleteById(@Param("id") Integer id); | ||
} | ||
``` | ||
|
||
|
||
|
||
## UserService.java | ||
|
||
``` | ||
package com.qingge.springboot.service; | ||
import com.qingge.springboot.entity.User; | ||
import com.qingge.springboot.mapper.UserMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
@Service | ||
public class UserService { | ||
@Autowired | ||
private UserMapper userMapper; | ||
public int save(User user) { | ||
if (user.getId() == null) { // user没有id,则表示是新增 | ||
return userMapper.insert(user); | ||
} else { // 否则为更新 | ||
return userMapper.update(user); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
## User.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.qingge.springboot.mapper.UserMapper"> | ||
<update id="update"> | ||
update sys_user | ||
<set> | ||
<if test="username != null"> | ||
username = #{username}, | ||
</if> | ||
<!-- <if test="password != null">--> | ||
<!-- password = #{password}--> | ||
<!-- </if>--> | ||
<if test="nickname != null"> | ||
nickname = #{nickname}, | ||
</if> | ||
<if test="email != null"> | ||
email = #{email}, | ||
</if> | ||
<if test="phone != null"> | ||
phone = #{phone}, | ||
</if> | ||
<if test="address != null"> | ||
address = #{address} | ||
</if> | ||
</set> | ||
<where> | ||
id = #{id} | ||
</where> | ||
</update> | ||
</mapper> | ||
``` | ||
|
||
|
||
|
||
## application.yml | ||
|
||
``` | ||
server: | ||
port: 9090 | ||
spring: | ||
datasource: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/qing?serverTimezone=GMT%2b8 | ||
username: root | ||
password: 123456 | ||
mybatis: | ||
mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的xml文件 | ||
configuration: | ||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl | ||
``` | ||
|
Oops, something went wrong.