9 changed files with 354 additions and 0 deletions
@ -0,0 +1,56 @@ |
|||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
<groupId>com.unicom</groupId> |
||||
|
<artifactId>jtjsc-backend</artifactId> |
||||
|
<version>0.0.1-SNAPSHOT</version> |
||||
|
<packaging>jar</packaging> |
||||
|
<name>jtjsc-backend</name> |
||||
|
<description>交通驾驶舱后端</description> |
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-parent</artifactId> |
||||
|
<version>2.5.4</version> |
||||
|
<relativePath/> <!-- lookup parent from repository --> |
||||
|
</parent> |
||||
|
<dependencies> |
||||
|
<!-- Spring Web --> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
<!-- MySQL Driver --> |
||||
|
<dependency> |
||||
|
<groupId>mysql</groupId> |
||||
|
<artifactId>mysql-connector-java</artifactId> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
<!-- MyBatis-Plus Starter --> |
||||
|
<dependency> |
||||
|
<groupId>com.baomidou</groupId> |
||||
|
<artifactId>mybatis-plus-boot-starter</artifactId> |
||||
|
<version>3.4.3.4</version> |
||||
|
</dependency> |
||||
|
<!-- Spring Boot Test --> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<!-- Lombok --> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<version>1.18.22</version> |
||||
|
<scope>provided</scope> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
</project> |
@ -0,0 +1,12 @@ |
|||||
|
package com.unicom; |
||||
|
|
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
|
||||
|
@SpringBootApplication |
||||
|
public class JtjscBackend { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(JtjscBackend.class, args); |
||||
|
} |
||||
|
} |
@ -0,0 +1,78 @@ |
|||||
|
package com.unicom.test.controller; |
||||
|
|
||||
|
import com.unicom.test.entity.User; |
||||
|
import com.unicom.test.service.UserService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("/users") |
||||
|
@Slf4j |
||||
|
public class UserController { |
||||
|
|
||||
|
@Autowired |
||||
|
private UserService userService; |
||||
|
|
||||
|
@GetMapping("/") |
||||
|
public List<User> list() { |
||||
|
return userService.list(); |
||||
|
} |
||||
|
|
||||
|
// 插入用户
|
||||
|
@PostMapping("/") |
||||
|
public String insertUser(@RequestBody User user) { |
||||
|
if (userService.insertUser(user)) { |
||||
|
return "User inserted successfully"; |
||||
|
} else { |
||||
|
return "Failed to insert user"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 根据 ID 查询用户
|
||||
|
@GetMapping("/{id}") |
||||
|
public User getUserById(@PathVariable Long id) { |
||||
|
log.info("getUserById"); |
||||
|
return userService.getUserById(id); |
||||
|
} |
||||
|
|
||||
|
// // 查询所有用户
|
||||
|
// @GetMapping("/")
|
||||
|
// public List<User> getAllUsers() {
|
||||
|
// return userService.getAllUsers();
|
||||
|
// }
|
||||
|
|
||||
|
// 根据条件查询用户
|
||||
|
@GetMapping("/search") |
||||
|
public List<User> getUsersByCondition(@RequestParam(required = false) String name) { |
||||
|
return userService.getUsersByCondition(name); |
||||
|
} |
||||
|
|
||||
|
// 分页查询用户
|
||||
|
@GetMapping("/page") |
||||
|
public Object getUsersByPage(@RequestParam int pageNum, @RequestParam int pageSize) { |
||||
|
return userService.getUsersByPage(pageNum, pageSize); |
||||
|
} |
||||
|
|
||||
|
// 更新用户信息
|
||||
|
@PutMapping("/") |
||||
|
public String updateUser(@RequestBody User user) { |
||||
|
if (userService.updateUser(user)) { |
||||
|
return "User updated successfully"; |
||||
|
} else { |
||||
|
return "Failed to update user"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 删除用户
|
||||
|
@DeleteMapping("/{id}") |
||||
|
public String deleteUser(@PathVariable Long id) { |
||||
|
if (userService.deleteUser(id)) { |
||||
|
return "User deleted successfully"; |
||||
|
} else { |
||||
|
return "Failed to delete user"; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package com.unicom.test.dao; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import com.unicom.test.entity.User; |
||||
|
import org.apache.ibatis.annotations.Mapper; |
||||
|
import org.apache.ibatis.annotations.Param; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Mapper |
||||
|
public interface UserMapper extends BaseMapper<User> { |
||||
|
|
||||
|
// 自定义插入用户的方法
|
||||
|
int insertUser(User user); |
||||
|
|
||||
|
// 自定义根据 ID 查询用户的方法
|
||||
|
User selectUserById(Long id); |
||||
|
|
||||
|
// 自定义查询所有用户的方法
|
||||
|
List<User> selectAllUsers(); |
||||
|
|
||||
|
// 自定义更新用户的方法
|
||||
|
int updateUser(User user); |
||||
|
|
||||
|
// 自定义删除用户的方法
|
||||
|
int deleteUserById(@Param("id") Long id); |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.unicom.test.entity; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
@TableName("user") |
||||
|
public class User { |
||||
|
// @TableId
|
||||
|
private Long id; |
||||
|
private String name; |
||||
|
private Integer age; |
||||
|
private String email; |
||||
|
} |
@ -0,0 +1,104 @@ |
|||||
|
package com.unicom.test.service; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import com.unicom.test.dao.UserMapper; |
||||
|
import com.unicom.test.entity.User; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Service |
||||
|
public class UserService extends ServiceImpl<UserMapper, User> { |
||||
|
|
||||
|
@Autowired |
||||
|
private UserMapper userMapper; |
||||
|
|
||||
|
// 插入用户
|
||||
|
public boolean insertUser(User user) { |
||||
|
try { |
||||
|
int rows = userMapper.insert(user); |
||||
|
return rows > 0; |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 根据 ID 查询用户
|
||||
|
public User getUserById(Long id) { |
||||
|
try { |
||||
|
return userMapper.selectById(id); |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 查询所有用户
|
||||
|
public List<User> getAllUsers() { |
||||
|
try { |
||||
|
return userMapper.selectList(null); |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 根据条件查询用户
|
||||
|
public List<User> getUsersByCondition(String name) { |
||||
|
try { |
||||
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
||||
|
if (name!= null) { |
||||
|
queryWrapper.like("name", name); |
||||
|
} |
||||
|
return userMapper.selectList(queryWrapper); |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 分页查询用户
|
||||
|
public IPage<User> getUsersByPage(int pageNum, int pageSize) { |
||||
|
try { |
||||
|
Page<User> page = new Page<>(pageNum, pageSize); |
||||
|
return userMapper.selectPage(page, null); |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 更新用户信息
|
||||
|
public boolean updateUser(User user) { |
||||
|
try { |
||||
|
int rows = userMapper.updateById(user); |
||||
|
return rows > 0; |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 删除用户
|
||||
|
public boolean deleteUser(Long id) { |
||||
|
try { |
||||
|
int rows = userMapper.deleteById(id); |
||||
|
return rows > 0; |
||||
|
} catch (Exception e) { |
||||
|
// 可以添加日志记录异常信息
|
||||
|
e.printStackTrace(); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
spring: |
||||
|
datasource: |
||||
|
url: jdbc:mysql://59.110.10.99:53306/jttjsc?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true |
||||
|
username: root |
||||
|
password: Unicom@2024 |
||||
|
driver-class-name: com.mysql.cj.jdbc.Driver |
||||
|
|
||||
|
mybatis-plus: |
||||
|
mapper-locations: classpath*:/mapper/**.xml |
||||
|
configuration: |
||||
|
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl |
@ -0,0 +1,13 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<configuration> |
||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
||||
|
<encoder> |
||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> |
||||
|
</encoder> |
||||
|
</appender> |
||||
|
<logger name="com.baomidou.mybatisplus" level="DEBUG" /> |
||||
|
<logger name="com.unicom" level="DEBUG" /> |
||||
|
<root level="INFO"> |
||||
|
<appender-ref ref="STDOUT" /> |
||||
|
</root> |
||||
|
</configuration> |
@ -0,0 +1,38 @@ |
|||||
|
<!DOCTYPE mapper |
||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
|
||||
|
<mapper namespace="com.unicom.test.dao.UserMapper"> |
||||
|
|
||||
|
<!-- 插入用户 --> |
||||
|
<insert id="insertUser" parameterType="com.unicom.test.entity.User"> |
||||
|
INSERT INTO user (name, age, email) |
||||
|
VALUES (#{name}, #{age}, #{email}) |
||||
|
</insert> |
||||
|
|
||||
|
<!-- 根据 ID 查询用户 --> |
||||
|
<select id="selectUserById" resultType="com.unicom.test.entity.User"> |
||||
|
SELECT id, name, age, email |
||||
|
FROM user |
||||
|
WHERE id = #{id} |
||||
|
</select> |
||||
|
|
||||
|
<!-- 查询所有用户 --> |
||||
|
<select id="selectAllUsers" resultType="com.unicom.test.entity.User"> |
||||
|
SELECT id, name, age, email |
||||
|
FROM user |
||||
|
</select> |
||||
|
|
||||
|
<!-- 更新用户信息 --> |
||||
|
<update id="updateUser" parameterType="com.unicom.test.entity.User"> |
||||
|
UPDATE user |
||||
|
SET name = #{name}, age = #{age}, email = #{email} |
||||
|
WHERE id = #{id} |
||||
|
</update> |
||||
|
|
||||
|
<!-- 删除用户 --> |
||||
|
<delete id="deleteUserById" parameterType="java.lang.Long"> |
||||
|
DELETE FROM user |
||||
|
WHERE id = #{id} |
||||
|
</delete> |
||||
|
</mapper> |
Loading…
Reference in new issue