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);
}
}