From 6f9ebbda372a66b3d20ecc7566680b5d44b95438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=80=A1?= Date: Wed, 1 Jan 2025 20:49:52 +0800 Subject: [PATCH] init demo --- pom.xml | 56 ++++++++++ src/main/java/com/unicom/JtjscBackend.java | 12 ++ .../test/controller/UserController.java | 78 +++++++++++++ .../java/com/unicom/test/dao/UserMapper.java | 27 +++++ .../java/com/unicom/test/entity/User.java | 15 +++ .../com/unicom/test/service/UserService.java | 104 ++++++++++++++++++ src/main/resources/application.yml | 11 ++ src/main/resources/logback-spring.xml | 13 +++ src/main/resources/mapper/test/UserMapper.xml | 38 +++++++ 9 files changed, 354 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/unicom/JtjscBackend.java create mode 100644 src/main/java/com/unicom/test/controller/UserController.java create mode 100644 src/main/java/com/unicom/test/dao/UserMapper.java create mode 100644 src/main/java/com/unicom/test/entity/User.java create mode 100644 src/main/java/com/unicom/test/service/UserService.java create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/logback-spring.xml create mode 100644 src/main/resources/mapper/test/UserMapper.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..98ee321 --- /dev/null +++ b/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + com.unicom + jtjsc-backend + 0.0.1-SNAPSHOT + jar + jtjsc-backend + 交通驾驶舱后端 + + org.springframework.boot + spring-boot-parent + 2.5.4 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + mysql + mysql-connector-java + runtime + + + + com.baomidou + mybatis-plus-boot-starter + 3.4.3.4 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + 1.18.22 + provided + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/src/main/java/com/unicom/JtjscBackend.java b/src/main/java/com/unicom/JtjscBackend.java new file mode 100644 index 0000000..df0e174 --- /dev/null +++ b/src/main/java/com/unicom/JtjscBackend.java @@ -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); + } +} diff --git a/src/main/java/com/unicom/test/controller/UserController.java b/src/main/java/com/unicom/test/controller/UserController.java new file mode 100644 index 0000000..61cb81d --- /dev/null +++ b/src/main/java/com/unicom/test/controller/UserController.java @@ -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 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 getAllUsers() { +// return userService.getAllUsers(); +// } + + // 根据条件查询用户 + @GetMapping("/search") + public List 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"; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/unicom/test/dao/UserMapper.java b/src/main/java/com/unicom/test/dao/UserMapper.java new file mode 100644 index 0000000..fbb51bf --- /dev/null +++ b/src/main/java/com/unicom/test/dao/UserMapper.java @@ -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 { + + // 自定义插入用户的方法 + int insertUser(User user); + + // 自定义根据 ID 查询用户的方法 + User selectUserById(Long id); + + // 自定义查询所有用户的方法 + List selectAllUsers(); + + // 自定义更新用户的方法 + int updateUser(User user); + + // 自定义删除用户的方法 + int deleteUserById(@Param("id") Long id); +} diff --git a/src/main/java/com/unicom/test/entity/User.java b/src/main/java/com/unicom/test/entity/User.java new file mode 100644 index 0000000..ed035fd --- /dev/null +++ b/src/main/java/com/unicom/test/entity/User.java @@ -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; +} diff --git a/src/main/java/com/unicom/test/service/UserService.java b/src/main/java/com/unicom/test/service/UserService.java new file mode 100644 index 0000000..9e5eafd --- /dev/null +++ b/src/main/java/com/unicom/test/service/UserService.java @@ -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 { + + @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 getAllUsers() { + try { + return userMapper.selectList(null); + } catch (Exception e) { + // 可以添加日志记录异常信息 + e.printStackTrace(); + return null; + } + } + + // 根据条件查询用户 + public List getUsersByCondition(String name) { + try { + QueryWrapper queryWrapper = new QueryWrapper<>(); + if (name!= null) { + queryWrapper.like("name", name); + } + return userMapper.selectList(queryWrapper); + } catch (Exception e) { + // 可以添加日志记录异常信息 + e.printStackTrace(); + return null; + } + } + + // 分页查询用户 + public IPage getUsersByPage(int pageNum, int pageSize) { + try { + Page 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; + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..e215829 --- /dev/null +++ b/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..794f7f1 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/test/UserMapper.xml b/src/main/resources/mapper/test/UserMapper.xml new file mode 100644 index 0000000..06981f7 --- /dev/null +++ b/src/main/resources/mapper/test/UserMapper.xml @@ -0,0 +1,38 @@ + + + + + + + INSERT INTO user (name, age, email) + VALUES (#{name}, #{age}, #{email}) + + + + + + + + + + + UPDATE user + SET name = #{name}, age = #{age}, email = #{email} + WHERE id = #{id} + + + + + DELETE FROM user + WHERE id = #{id} + + \ No newline at end of file