From a1f173dfdbd1c9a934e779b5626795884114d8a0 Mon Sep 17 00:00:00 2001 From: chenyanghu Date: Thu, 11 Mar 2021 18:06:12 -0500 Subject: [PATCH] added annotation SQL statement --- pom.xml | 58 +++++++++++++++++- .../easynotes/EasyNotesApplication.java | 5 ++ .../easynotes/adapters/SecurityConfig.java | 34 +++++++++++ .../easynotes/controller/UserController.java | 35 +++++++++++ .../example/easynotes/mappers/UserMapper.java | 16 +++++ .../com/example/easynotes/model/User.java | 61 +++++++++++++++++++ .../easynotes/services/UserService.java | 25 ++++++++ .../services/impl/UserServiceImpl.java | 43 +++++++++++++ .../example/easynotes/testForGitIngore.java | 4 ++ src/main/resources/Mappers/MyMapper.xml | 13 ++++ src/main/resources/Mappers/UserMapper.xml | 17 ++++++ src/main/resources/application.properties | 13 ---- src/main/resources/application.yml | 14 +++++ src/main/resources/mybatis-config.xml | 40 ++++++++++++ 14 files changed, 363 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/example/easynotes/adapters/SecurityConfig.java create mode 100644 src/main/java/com/example/easynotes/controller/UserController.java create mode 100644 src/main/java/com/example/easynotes/mappers/UserMapper.java create mode 100644 src/main/java/com/example/easynotes/model/User.java create mode 100644 src/main/java/com/example/easynotes/services/UserService.java create mode 100644 src/main/java/com/example/easynotes/services/impl/UserServiceImpl.java create mode 100644 src/main/java/com/example/easynotes/testForGitIngore.java create mode 100644 src/main/resources/Mappers/MyMapper.xml create mode 100644 src/main/resources/Mappers/UserMapper.xml delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/mybatis-config.xml diff --git a/pom.xml b/pom.xml index 1145d6d..ca98066 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,9 @@ UTF-8 UTF-8 1.8 + 2.9.2 + 1.6.0 + 1.6.0 @@ -49,6 +52,59 @@ spring-boot-starter-test test + + + org.mybatis + mybatis + 3.5.6 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + 1.2.0 + + + + + io.springfox + springfox-swagger2 + ${swagger2.version} + + + io.springfox + springfox-swagger-ui + ${swagger2.version} + + + + io.swagger + swagger-models + ${swagger-models.version} + + + io.swagger + swagger-annotations + ${swagger-annotations.version} + + + + org.springframework.boot + spring-boot-starter-security + 2.0.1.RELEASE + + @@ -59,6 +115,4 @@ - - diff --git a/src/main/java/com/example/easynotes/EasyNotesApplication.java b/src/main/java/com/example/easynotes/EasyNotesApplication.java index af3b8dd..592a877 100644 --- a/src/main/java/com/example/easynotes/EasyNotesApplication.java +++ b/src/main/java/com/example/easynotes/EasyNotesApplication.java @@ -1,11 +1,16 @@ package com.example.easynotes; +import com.example.easynotes.model.User; +import org.apache.ibatis.type.MappedTypes; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication @EnableJpaAuditing +@MappedTypes(User.class) +@MapperScan("com.example.easynotes.mappers") public class EasyNotesApplication { public static void main(String[] args) { diff --git a/src/main/java/com/example/easynotes/adapters/SecurityConfig.java b/src/main/java/com/example/easynotes/adapters/SecurityConfig.java new file mode 100644 index 0000000..e9d7281 --- /dev/null +++ b/src/main/java/com/example/easynotes/adapters/SecurityConfig.java @@ -0,0 +1,34 @@ +package com.example.easynotes.adapters; + +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * @EnableWebSecurity:此注解会启用Spring Security + */ +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + /** + * 1)HttpSecurity支持cors。 + * 2)默认会启用CRSF,此处因为没有使用thymeleaf模板(会自动注入_csrf参数), + * 要先禁用csrf,否则登录时需要_csrf参数,而导致登录失败。 + * 3)antMatchers:匹配 "/" 路径,不需要权限即可访问,匹配 "/user" 及其以下所有路径, + * 都需要 "USER" 权限 + * 4)配置登录地址和退出地址 + */ + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .cors().and() + .csrf().disable() + .authorizeRequests() + .antMatchers("/").permitAll() + .antMatchers("/user/**").hasRole("USER") + .and() + .formLogin().loginPage("/login").defaultSuccessUrl("/hello") + .and() + .logout().logoutUrl("/logout").logoutSuccessUrl("/login"); + } +} diff --git a/src/main/java/com/example/easynotes/controller/UserController.java b/src/main/java/com/example/easynotes/controller/UserController.java new file mode 100644 index 0000000..891d4e4 --- /dev/null +++ b/src/main/java/com/example/easynotes/controller/UserController.java @@ -0,0 +1,35 @@ +package com.example.easynotes.controller; + +import com.example.easynotes.exception.ResourceNotFoundException; +import com.example.easynotes.model.Note; +import com.example.easynotes.model.User; +import com.example.easynotes.services.UserService; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/users") +public class UserController { + @Autowired + private UserService userService; + + @ApiOperation("批量删除角色") + @RequestMapping(value = "/delete", method = RequestMethod.DELETE) + @ResponseBody + public String delete(@RequestParam("ids") Long id) { + int count = userService.delete(id); + if (count > 0) { + return "Success"; + } + return "Failed"; + } + @GetMapping("/all") + public List findAll(){ + return userService.findAll(); + } +} diff --git a/src/main/java/com/example/easynotes/mappers/UserMapper.java b/src/main/java/com/example/easynotes/mappers/UserMapper.java new file mode 100644 index 0000000..100ebd3 --- /dev/null +++ b/src/main/java/com/example/easynotes/mappers/UserMapper.java @@ -0,0 +1,16 @@ +package com.example.easynotes.mappers; + +import com.example.easynotes.model.User; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +@Mapper +public interface UserMapper { + @Select("select * from users") + public List getUserList(); + @Delete("delete from users where id = #{id, jdbcType=BIGINT}") + public int deleteByPrimaryKey(Long id); +} diff --git a/src/main/java/com/example/easynotes/model/User.java b/src/main/java/com/example/easynotes/model/User.java new file mode 100644 index 0000000..2d09f9f --- /dev/null +++ b/src/main/java/com/example/easynotes/model/User.java @@ -0,0 +1,61 @@ +package com.example.easynotes.model; + +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; + +public class User implements Serializable { + Long id; + + @ApiModelProperty(value = "Email") + String email; + @ApiModelProperty(value = "User Name") + String userName; + + String password; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", email=").append(email); + sb.append(", username=").append(userName); + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/main/java/com/example/easynotes/services/UserService.java b/src/main/java/com/example/easynotes/services/UserService.java new file mode 100644 index 0000000..524093d --- /dev/null +++ b/src/main/java/com/example/easynotes/services/UserService.java @@ -0,0 +1,25 @@ +package com.example.easynotes.services; + +import com.example.easynotes.model.User; + +import java.util.List; + +public interface UserService { + /** + * create users + */ + int create(User user); + /** + * update user + */ + int update(Long id, User user); + /** + * delete users + */ + int delete(Long id); + + /** + * find all users + */ + List findAll(); +} diff --git a/src/main/java/com/example/easynotes/services/impl/UserServiceImpl.java b/src/main/java/com/example/easynotes/services/impl/UserServiceImpl.java new file mode 100644 index 0000000..6ef5ba6 --- /dev/null +++ b/src/main/java/com/example/easynotes/services/impl/UserServiceImpl.java @@ -0,0 +1,43 @@ +package com.example.easynotes.services.impl; + +import com.example.easynotes.mappers.UserMapper; +import com.example.easynotes.model.User; +import com.example.easynotes.services.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + @Autowired + private UserMapper userMapper; + + @Override + public List findAll() { + return userMapper.getUserList(); + } + + @Override + public int create(User user) { + return 0; + } + + @Override + public int update(Long id, User user) { + return 0; + } + + @Override + public int delete(Long id) { + int count = userMapper.deleteByPrimaryKey(id); + return count; + } + + private void encryptPassword(User userInfo){ + String password = userInfo.getPassword(); + password = new BCryptPasswordEncoder().encode(password); + userInfo.setPassword(password); + } +} diff --git a/src/main/java/com/example/easynotes/testForGitIngore.java b/src/main/java/com/example/easynotes/testForGitIngore.java new file mode 100644 index 0000000..844bb4d --- /dev/null +++ b/src/main/java/com/example/easynotes/testForGitIngore.java @@ -0,0 +1,4 @@ +package com.example.easynotes; + +public class testForGitIngore { +} diff --git a/src/main/resources/Mappers/MyMapper.xml b/src/main/resources/Mappers/MyMapper.xml new file mode 100644 index 0000000..5f31726 --- /dev/null +++ b/src/main/resources/Mappers/MyMapper.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/src/main/resources/Mappers/UserMapper.xml b/src/main/resources/Mappers/UserMapper.xml new file mode 100644 index 0000000..83c002b --- /dev/null +++ b/src/main/resources/Mappers/UserMapper.xml @@ -0,0 +1,17 @@ + + + + + + + + delete from users + where id = #{id, jdbcType=BIGINT} + + \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index d357d46..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,13 +0,0 @@ -## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false -spring.datasource.username = root -spring.datasource.password = callicoder - - -## Hibernate Properties - -# The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect - -# Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..11a997f --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,14 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3306/notes_app?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false + username: root + password: password + druid: + initial-size: 5 #连接池初始化大小 + min-idle: 10 #最小空闲连接数 + max-active: 20 #最大连接数 + web-stat-filter: + exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #不统计这些请求数据 + stat-view-servlet: #访问监控网页的登录用户名和密码 + login-username: druid + login-password: druid \ No newline at end of file diff --git a/src/main/resources/mybatis-config.xml b/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..0fe0748 --- /dev/null +++ b/src/main/resources/mybatis-config.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file