2.2. 测试MyBatisPlusGenerator生成的代码
先在straw父项目的pom.xml中添加Druid相关依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency>
在straw-portal项目的application.properties配置:
server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/straw?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.initial-size=2 spring.datasource.druid.max-active=7 mybatis.mapper-locations=classpath:mapper/*.xml logging.level.cn.tedu.straw.portal=trace
在项目的启动类上添加注解,配置接口文件的位置:
@MapperScan("cn.tedu.straw.portal.mapper")
全部完成后,在straw-portal子模块项目的src/test/java下创建cn.tedu.straw.portal.mapper.UserMapperTests测试类,进行简单的功能测试:
package cn.tedu.straw.portal.mapper; import cn.tedu.straw.portal.model.User; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.jws.soap.SOAPBinding; import javax.sql.DataSource; import java.io.Serializable; import java.sql.Connection; import java.sql.SQLException; import java.util.List; @SpringBootTest @Slf4j public class UserMapperTests { @Autowired DataSource dataSource; @Autowired UserMapper userMapper; @Test void contextLoads() { log.debug("UserMapperTests.context"); } @Test void getConnection() throws SQLException { Connection connection = dataSource.getConnection(); log.debug("connection > {}", connection); } @Test void insert() { User user = new User(); user.setUsername("plus"); user.setPassword("1234"); int rows = userMapper.insert(user); log.debug("rows={}", rows); } @Test void selectById() { Integer id = 1; User user = userMapper.selectById(id); log.debug("user > {}", user); } @Test void selectList() { List<User> users = userMapper.selectList(null); log.debug("count={}", users.size()); for (User user : users) { log.debug("user > {}", user); } } }
3. 项目前期准备–Spring Security
Spring Security是Spring提供的安全组件,主要用于在项目中对用户的身份进行识别和认证。
使用Spring Security之前,需要添加依赖,可以在创建SpringBoot项目时直接勾选,也可以在已经创建好的项目中添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
首先,Spring Security可以实现对密码的加密处理!在straw-portal子模块项目中创建cn.tedu.straw.portal.security.SecurityConfig配置类,需要添加@Configuration注解,在类中自定义方法,返回PasswordEncoder的对象交给Spring容器,其对象的类型可以使用BCryptPasswordEncoder类型的对象:
package cn.tedu.straw.portal.security; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
然后,在src/test/java下创建cn.tedu.straw.portal.SecurityTests测试类,测试加密:
package cn.tedu.straw.portal; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.security.crypto.password.PasswordEncoder; @SpringBootTest @Slf4j public class SecurityTests { @Autowired PasswordEncoder passwordEncoder; @Test void encode() { String rawPassword = "123456"; String encodePassword = passwordEncoder.encode(rawPassword); log.debug("raw password={}, encode password={}", rawPassword, encodePassword); } }