diff --git a/pom.xml b/pom.xml index cdceb06ef619e32bfdcd7c38fdabfbc260a70097..9f7f519b4a302b6bb93eca0aa16e46ba4933a5c5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.5.6 + 2.6.6 com.kwan @@ -17,47 +17,16 @@ 1.8 - - org.springframework.boot - spring-boot-starter-data-redis - org.springframework.boot spring-boot-starter-web - - - - com.alibaba druid 1.2.3 - - - - - - - io.springfox - springfox-swagger2 - 2.9.2 - - - - - io.springfox - springfox-swagger-ui - 2.9.2 - - - - org.apache.velocity - velocity-engine-core - 2.0 - org.apache.commons @@ -68,19 +37,18 @@ com.baomidou mybatis-plus-boot-starter - 3.4.3.1 + 3.4.3 mysql mysql-connector-java - runtime + 8.0.11 org.projectlombok lombok - true @@ -88,15 +56,6 @@ spring-boot-starter-test test - - junit - junit - 4.13.1 - test - - - - diff --git a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java index d1f9ac896cc3194d06f5a6a954bfc904a8f60be4..d2a159195f0db6977ccd1f1096c7df8deea5707e 100644 --- a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java +++ b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java @@ -1,7 +1,9 @@ package com.kwan.springbootkwan; import org.springframework.boot.SpringApplication; + import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; + @EnableScheduling @SpringBootApplication public class SpringBootKwanApplication { diff --git a/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java b/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..70227d8b8327567f49e25d167ea7493d8a96beee --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java @@ -0,0 +1,26 @@ +package com.kwan.springbootkwan.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; +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; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@MapperScan("com.kwan.springbootkwan.mapper")//扫描我们的mapper文件夹 这里添加了@MapperScan注解就不需要Springboot启动类上加入@MapperScan注解了 +@EnableTransactionManagement //事务控制 +@Configuration //配置类 +public class MyBatisPlusConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor(){ + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + //注册乐观锁插件 + interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); + //分页插件 + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); + return interceptor; + } +} diff --git a/src/main/java/com/kwan/springbootkwan/config/Swagger2.java b/src/main/java/com/kwan/springbootkwan/config/Swagger2.java deleted file mode 100644 index 0628e7007b0cd633c70c3e1845082ae6d83dd7cf..0000000000000000000000000000000000000000 --- a/src/main/java/com/kwan/springbootkwan/config/Swagger2.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.kwan.springbootkwan.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.Contact; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - - -/** - * Swagger2配置 - * - * @author : qinyingjie - * @version : 2.2.0 - * @date : 2022/12/9 11:33 - */ -@Configuration -@EnableSwagger2 -@ConfigurationProperties(prefix = "swagger") -public class Swagger2 { - - private static final String BASE_PACKAGE = "com.kwan.springbootkwan"; - @Value("${swagger.enable}") - private boolean enableSwagger; - - @Bean - public Docket helloDocket() { - return new Docket(DocumentationType.SWAGGER_2) - //用于分组功能,也可以不配置 - .groupName("admin") - //注册整体api信息 - .apiInfo(apiInfo()) - //swagger功能是否启用,可以通过配置设置,也可以先写死 - .enable(enableSwagger) - .select() - //指定扫描的包 - .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE)) - //设置此组只匹配admin/**的请求 - .paths(PathSelectors.ant("/admin/**")) - .build(); - } - - private ApiInfo apiInfo() { - return new ApiInfoBuilder().title("后台管理项目") - .description("通用的CRUD") - .contact(new Contact("Van", "", "")) - .version("1.0.0") - .build(); - } -} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/UserController.java b/src/main/java/com/kwan/springbootkwan/controller/UserController.java new file mode 100644 index 0000000000000000000000000000000000000000..7c3ab9e9ab9a78884c6f9d4ee5964aecb0ba1f0a --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/UserController.java @@ -0,0 +1,27 @@ +package com.kwan.springbootkwan.controller; + +import com.kwan.springbootkwan.entity.User; +import com.kwan.springbootkwan.service.IUserService; +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 IUserService userService; + + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List addAdvertise() { + return userService.getUsers(); + } + + @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET) + public User getUserById(@PathVariable Integer id) { + return userService.getUserById(id); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/entity/User.java b/src/main/java/com/kwan/springbootkwan/entity/User.java index 1bf24c6e3642b8060e9871186be99b3e8ec87386..27738fae599f455a97574dbaca34974ffee8cc86 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/User.java +++ b/src/main/java/com/kwan/springbootkwan/entity/User.java @@ -5,15 +5,11 @@ import com.baomidou.mybatisplus.annotation.TableName; import lombok.Builder; import lombok.Data; -import java.io.Serializable; - @Data @Builder -@TableName(value = "User")//指定表名 -public class User implements Serializable { - private static final long serialVersionUID = -5644799954031156649L; - //value与数据库主键列名一致,若实体类属性名与表主键列名一致可省略value - @TableId(value = "id", type = IdType.AUTO)//指定自增策略 +@TableName(value = "user") +public class User { + @TableId(value = "id", type = IdType.AUTO) private Integer id; private String name; private String sex; diff --git a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..27e180782142a1ebde068d10b63fc0fcb049c843 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java @@ -0,0 +1,12 @@ +package com.kwan.springbootkwan.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.kwan.springbootkwan.entity.User; +import org.springframework.stereotype.Repository; + +//@Mapper +@Repository // 代表持久层 +public interface UserMapper extends BaseMapper { + +} + diff --git a/src/main/java/com/kwan/springbootkwan/service/IUserService.java b/src/main/java/com/kwan/springbootkwan/service/IUserService.java new file mode 100644 index 0000000000000000000000000000000000000000..cd6b2403888b23cb579c5879eb8498c435ce4844 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/IUserService.java @@ -0,0 +1,24 @@ +package com.kwan.springbootkwan.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.kwan.springbootkwan.entity.User; + +import java.util.List; + +public interface IUserService extends IService { + + /** + * 获取所有用户 + * + * @return + */ + List getUsers(); + + /** + * 获取一个用户 + * + * @return + */ + User getUserById(Integer id); + +} diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..fc0b8038629a15565240f12590f25bfc3e598554 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java @@ -0,0 +1,27 @@ +package com.kwan.springbootkwan.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.kwan.springbootkwan.entity.User; +import com.kwan.springbootkwan.mapper.UserMapper; +import com.kwan.springbootkwan.service.IUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl extends ServiceImpl implements IUserService { + + @Autowired + private UserMapper userMapper; + + @Override + public List getUsers() { + return userMapper.selectList(null); + } + + @Override + public User getUserById(Integer id) { + return userMapper.selectById(id); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 8d7b4fb09ff40a04e0ba4e64b25082e2d9fb94ea..705a7f12941e5af95ed9cec1688e32db3c67b2d9 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -9,16 +9,27 @@ spring: mvc: pathmatch: matching-strategy: ant_path_matcher - profiles: - active: dev # mysql datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://127.0.0.1:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai + url: jdbc:mysql://127.0.0.1:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 716288qwe + +#mybatis-plus配置 mybatis-plus: - mapper-locations: mapper/*.xml configuration: + map-underscore-to-camel-case: true + call-setters-on-nulls: true + auto-mapping-behavior: full log-impl: org.apache.ibatis.logging.stdout.StdOutImpl - map-underscore-to-camel-case: true \ No newline at end of file + mapper-locations: classpath*:mapper/**/*Mapper.xml + global-config: + banner: false + # 逻辑删除配置 + db-config: + id-type: AUTO + logic-delete-field: delFlag + logic-delete-value: 1 + logic-not-delete-value: 0 + table-underline: true \ No newline at end of file diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index 494132e94d37fd664e68fb2920b4e5799965493b..146a35d58c9b6ce5bb52820d81e429fc4d31d042 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -1,4 +1,5 @@ - - - - + + + + + diff --git a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java index 557c6106498e9b32509c70fe24d1ea0f5f676a7e..d33147c279b6620a51da12cc44d4aede22af6c11 100644 --- a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java +++ b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java @@ -1,74 +1,22 @@ -//package com.kwan.springbootkwan; -// -// -//import com.kwan.springbootkwan.entity.User; -//import com.kwan.springbootkwan.service.impl.UserServiceImpl; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.context.SpringBootTest; -// -//import java.util.ArrayList; -//import java.util.List; -// -// -//@SpringBootTest -//public class UserServiceImplTest { -// -// @Autowired -// UserServiceImpl userService; -// @Test -// public void queryAll() { -// userService.queryAll().forEach(System.out::println); -// } -// -// @Test -// public void add() { -// List users = new ArrayList<>(); -// for (int i = 0; i < 15; ++i) { -// User user = User.builder().id(i + 1).name("test" + i).sex(i % 2 == 0 ? "男" : "女").pwd("aaaa").email("123" + i + "@qq.com").build(); -// -// users.add(user); -// } -// users.forEach(System.out::println); -// userService.add(users); -// -// -// } -// -// @Test -// public void query() { -// userService.queryAll().forEach(System.out::println); -// -// userService.queryByName1("test1").forEach(System.out::println); -// userService.queryByName2("test1").forEach(System.out::println); -// User user = userService.queryById(User.builder().id(2).build()); -// System.out.println(user); -// userService.queryByNameMap("test1").forEach(System.out::println); -// System.out.println(userService.count()); -// } -// -// @Test -// public void delete() { -// User user = User.builder().id(2).build(); -// userService.deleteById(user); -// -// userService.deleteBy("name", "test15"); -// -// userService.deleteByIds(); -// -// } -// -// @Test -// public void change() { -// User user1 = User.builder().name("蔡徐坤").build(); -// userService.changeBy(user1, "sex", "男"); -// -// user1.setName("蔡徐坤2"); -// userService.changeUserById(user1); -// } -// -//// public void test1(){ -//// userService.register() -//// } -// -//} +package com.kwan.springbootkwan; + +import com.kwan.springbootkwan.entity.User; +import com.kwan.springbootkwan.mapper.UserMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + + + +@SpringBootTest +public class UserServiceImplTest { + + @Autowired + private UserMapper userService; + + @Test + public void queryAll() { + User user = userService.selectById(1); + System.out.println(user); + } +} \ No newline at end of file