Browse Source

20230523:提交版本

李云瑞 11 months ago
commit
c5758fe32b

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### 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/

+ 161 - 0
pom.xml

@@ -0,0 +1,161 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.7.10</version>
+        <relativePath/> <!-- lookup parent from repository -->
+    </parent>
+    <groupId>com.example</groupId>
+    <artifactId>SpringBoot</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>SpringBoot</name>
+    <description>Demo project for Spring Boot</description>
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>2.2.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <!--热部署-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-devtools</artifactId>
+            <scope>runtime</scope>
+            <optional>true</optional>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+
+        <!--MyBatis-plus-->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.5.3</version>
+        </dependency>
+        <!--代码生成器-->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-generator</artifactId>
+            <version>3.5.3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity</artifactId>
+            <version>1.7</version>
+        </dependency>
+
+        <!--swagger-->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </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>
+    <repositories>
+        <repository>
+            <id>nexus-aliyun</id>
+            <name>nexus-aliyun</name>
+            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+        <repository>
+            <id>spring-milestones</id>
+            <name>Spring Milestones</name>
+            <url>https://repo.spring.io/milestone</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+        <repository>
+            <id>spring-snapshots</id>
+            <name>Spring Snapshots</name>
+            <url>https://repo.spring.io/snapshot</url>
+            <releases>
+                <enabled>false</enabled>
+            </releases>
+        </repository>
+    </repositories>
+    <pluginRepositories>
+        <pluginRepository>
+            <id>public</id>
+            <name>aliyun nexus</name>
+            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </pluginRepository>
+        <pluginRepository>
+            <id>spring-milestones</id>
+            <name>Spring Milestones</name>
+            <url>https://repo.spring.io/milestone</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </pluginRepository>
+        <pluginRepository>
+            <id>spring-snapshots</id>
+            <name>Spring Snapshots</name>
+            <url>https://repo.spring.io/snapshot</url>
+            <releases>
+                <enabled>false</enabled>
+            </releases>
+        </pluginRepository>
+    </pluginRepositories>
+
+</project>

+ 15 - 0
src/main/java/com/example/springboot/Application.java

@@ -0,0 +1,15 @@
+package com.example.springboot;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@SpringBootApplication
+public class Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+     }
+
+}

+ 26 - 0
src/main/java/com/example/springboot/config/CorsConfig.java

@@ -0,0 +1,26 @@
+package com.example.springboot.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+@Configuration
+public class CorsConfig {
+
+    private static final long MAX_AGE=24*60*60;
+
+    @Bean
+    public CorsFilter corsFilter(){
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        CorsConfiguration corsConfiguration =new CorsConfiguration();
+        corsConfiguration.addAllowedOrigin("*"); //设置访问原地址(* 为所有)
+        corsConfiguration.addAllowedHeader("*"); //设置访问源请求头
+        corsConfiguration.addAllowedMethod("*"); //设置访问源请求方法
+        corsConfiguration.setMaxAge(MAX_AGE);
+        source.registerCorsConfiguration("/**",corsConfiguration); //对接口配置跨域设置
+        return new CorsFilter(source);
+    }
+
+}

+ 20 - 0
src/main/java/com/example/springboot/config/MybatisPlusConfig.java

@@ -0,0 +1,20 @@
+package com.example.springboot.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@MapperScan("com.example.springboot.mapper")
+public class MybatisPlusConfig {
+
+    @Bean
+    public MybatisPlusInterceptor mybatisPlusInterceptor() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
+        return interceptor;
+    }
+}

+ 69 - 0
src/main/java/com/example/springboot/controller/UserController.java

@@ -0,0 +1,69 @@
+package com.example.springboot.controller;
+
+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.example.springboot.entity.User;
+import com.example.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
+    UserService userService;
+
+    //查询所用用户
+    @GetMapping("/queryList")
+    public List<User> queryList() {
+//        return userMapper.queryUserList();
+        return userService.list();
+    }
+
+    /*//添加一个用户
+    @PostMapping("/addUser")
+    public int addUser(@RequestBody User user) {
+        System.out.println("1111111111");
+        return userMapper.insert(user);
+    }*/
+
+    //User中id为空则是创建,不为空则是修改
+    @PostMapping()
+    public boolean save(@RequestBody User user) {
+        return userService.saveUser(user);
+    }
+
+    //根据ID删除一条数据
+    @PostMapping("/{id}")
+    public boolean daleteById(@PathVariable Integer id) {
+        return userService.removeById(id);
+    }
+
+    //根据ID批量删除
+    @PostMapping("/batch{id}")
+    public boolean daleteBatch(@RequestBody List<Integer> ids) {
+        return userService.removeBatchByIds(ids);
+    }
+
+    //mybatis-plus分页查询
+    @GetMapping("/page")
+    public IPage<User> findPage(@RequestParam Integer pageNum,
+                                @RequestParam Integer pageSize,
+                                @RequestParam(defaultValue = "") String name,
+                                @RequestParam(defaultValue = "") String phone) {
+        IPage<User> page = new Page<>(pageNum,pageSize);
+        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
+        if(!"".equals(name)){
+            queryWrapper.like("name",name);
+        }
+        if (!"".equals(phone)){
+            queryWrapper.like("phone",phone);
+        }
+        return userService.page(page,queryWrapper);
+    }
+}

+ 16 - 0
src/main/java/com/example/springboot/entity/User.java

@@ -0,0 +1,16 @@
+package com.example.springboot.entity;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.Data;
+
+@Data
+public class User {
+    private Integer id;
+    private String name;
+    @JsonIgnore
+    private String pwd;
+    private String sex;
+    private Integer stature;
+    private String mail;
+    private String phone;
+}

+ 34 - 0
src/main/java/com/example/springboot/mapper/UserMapper.java

@@ -0,0 +1,34 @@
+package com.example.springboot.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.springboot.entity.User;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+import org.springframework.context.annotation.Bean;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+//@Mapper
+//@Repository
+public interface UserMapper extends BaseMapper<User> {
+
+    /*@Select("select * from user")
+    List<User> queryUserList();
+
+    @Insert("insert into user(name,pwd,sex,stature,mail,phone) values (#{name},#{pwd},#{sex},#{stature},#{mail},#{phone})")
+    int insert(User user);
+
+    int update(User user);
+
+    @Delete("delete from user where id = #{id}")
+    int deleteById(Integer id);
+
+    @Select("select * from user where name like concat('%',#{name},'%') and phone like concat('%',#{phone},'%') limit #{pageNum},#{pageSize}")
+    List<User> selectPage(Integer pageNum,Integer pageSize,String name,String phone);
+
+    @Select("select count(*) from user where name like concat('%',#{name},'%') and phone like concat('%',#{phone},'%')")
+    Integer selectTotal(String name,String phone);*/
+}

+ 16 - 0
src/main/java/com/example/springboot/service/IUserService.java

@@ -0,0 +1,16 @@
+package com.example.springboot.service;
+
+import com.example.springboot.entity.User;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author 小李测试
+ * @since 2023-05-23
+ */
+public interface IUserService extends IService<User> {
+
+}

+ 32 - 0
src/main/java/com/example/springboot/service/UserService.java

@@ -0,0 +1,32 @@
+package com.example.springboot.service;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.example.springboot.entity.User;
+import com.example.springboot.mapper.UserMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserService extends ServiceImpl<UserMapper,User> {
+    public boolean saveUser(User user) {
+        /*if (user.getId() == null){
+            return save(user);
+        }else {
+            return updateById(user);
+        }*/
+        return saveOrUpdate(user);
+
+    }
+
+    /*@Autowired
+    UserMapper userMapper;
+
+    public int save(User user){
+        if (user.getId() == null){
+            return userMapper.insert(user);
+        } else {
+            return userMapper.update(user);
+        }
+    }*/
+
+}

+ 20 - 0
src/main/java/com/example/springboot/service/impl/UserServiceImpl.java

@@ -0,0 +1,20 @@
+package com.example.springboot.service.impl;
+
+import com.example.springboot.entity.User;
+import com.example.springboot.mapper.UserMapper;
+import com.example.springboot.service.IUserService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author 小李测试
+ * @since 2023-05-23
+ */
+@Service
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
+
+}

+ 47 - 0
src/main/java/com/example/springboot/utils/CodeGenerator.java

@@ -0,0 +1,47 @@
+package com.example.springboot.utils;
+
+import com.baomidou.mybatisplus.generator.FastAutoGenerator;
+import com.baomidou.mybatisplus.generator.config.OutputFile;
+import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
+
+import java.sql.Types;
+import java.util.Collections;
+
+/*
+* 代码生成器
+* */
+public class CodeGenerator {
+    public static void main(String[] args) {
+        generate();
+    }
+    private static void generate(){
+        FastAutoGenerator.create("jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8", "root", "root1120")
+                .globalConfig(builder -> {
+                    builder.author("小李测试") // 设置作者
+                            .enableSwagger() // 开启 swagger 模式
+                            .fileOverride() // 覆盖已生成文件
+                            .outputDir("D:\\project\\2023\\SpringVUE\\SpringBoot\\src\\main\\java\\"); // 指定输出目录
+                })
+                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
+                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
+                    if (typeCode == Types.SMALLINT) {
+                        // 自定义类型转换
+                        return DbColumnType.INTEGER;
+                    }
+                    return typeRegistry.getColumnType(metaInfo);
+
+                }))
+                .packageConfig(builder -> {
+                    builder.parent("com.example.springboot") // 设置父包名
+                            .moduleName("") // 设置父包模块名
+                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\project\\2023\\SpringVUE\\SpringBoot\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
+                })
+                .strategyConfig(builder -> {
+                    builder.addInclude("user") // 设置需要生成的表名
+                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
+                })
+//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
+                .execute();
+    }
+}

+ 4 - 0
src/main/resources/application.properties

@@ -0,0 +1,4 @@
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
+spring.datasource.url=jdbc:mysql://localhost:3306/boot?serverTimezone=GMT%2b8
+spring.datasource.username=root
+spring.datasource.password=root1120

+ 23 - 0
src/main/resources/application.yml

@@ -0,0 +1,23 @@
+spring:
+  datasource:
+    username: root
+    hikari:
+      password: root1120
+    url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8
+    driver-class-name: com.mysql.cj.jdbc.Driver
+  devtools:
+    restart:
+      enabled: true #开启热部署
+      additional-paths: src/main/java #重启目录
+      exclude: WEB-INF/**
+  freemarker:
+    cache: false #页面不加载缓存,修改即时生效
+server:
+  port: 9090
+mybatis:
+  mapper-locations: classpath:mapper/*
+#  configuration:
+mybatis-plus:
+  configuration:
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

+ 30 - 0
src/main/resources/mapper/UserMapper.xml

@@ -0,0 +1,30 @@
+<?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.springboot.mapper.UserMapper">
+    <update id="update">
+        update user
+        <set>
+            <if test="name != null">
+                name = #{name},
+            </if>
+            <if test="sex != null">
+                sex = #{sex},
+            </if>
+            <if test="stature != null">
+                stature = #{stature},
+            </if>
+            <if test="mail != null">
+                mail = #{mail},
+            </if>
+            <if test="pwd != null">
+                pwd = #{pwd}
+            </if>
+            <if test="phone != null">
+                phone = #{phone}
+            </if>
+        </set>
+        <where>
+            id = #{id}
+        </where>
+    </update>
+</mapper>

+ 13 - 0
src/test/java/com/example/springboot/ApplicationTests.java

@@ -0,0 +1,13 @@
+package com.example.springboot;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class ApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}