1.Springboot和Mybatis的整合
1.1.使用注解的方式整合MyBatis
- 引入相关的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<!--mysql驱动依赖坐标-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
<scope>runtime</scope>
</dependency>
<!--druid数据库连接池依赖坐标-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
- application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/a8?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
# 日志级别设置
logging:
level:
com:
test: debug
- 编写Account
public class Account {
private Integer id;
private String name;
private Integer blance;
public Account() {
}
public Account(Integer id, String name, Integer blance) {
this.id = id;
this.name = name;
this.blance = blance;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBlance() {
return blance;
}
public void setBlance(Integer blance) {
this.blance = blance;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", blance=" + blance +
'}';
}
}
- 写出mapper接口
@Mapper //作用和@Repository作用一致,可以解决包扫描mapper,controller注解mapper,idea提示报错(实际运行是没问题的)
public interface IAccountMapper {
@Select("select * from account")
public List<Account> findAllAccount();
}
- 让mapper接口起作用
@SpringBootApplication
@MapperScan("com.test.mapper") //扫描mapper接口
public class ASpringBoot03MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(ASpringBoot03MybatisApplication.class, args);
}
}
- 进行测试
public class AccountController {
@Autowired
private IAccountMapper accountMapper;
@RequestMapping("/findAll")
public List<Account> findAllAccount(){
List<Account> allAccount = accountMapper.findAllAccount();
System.out.println(allAccount);
return allAccount;
}
}
1.2.使用xml的方式整合mybatis
- 修改mapper接口
@Mapper //作用和@Repository作用一致,可以解决包扫描mapper,controller注解mapper,idea提示报错(实际运行是没问题的)
public interface IAccountMapper {
@Select("select * from account")
public List<Account> findAllAccount();
public Account findAccountById(Integer id);
}
- 在yml中进行配置
#数据库配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/a8?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
#配置数据库连接池
type: com.alibaba.druid.pool.DruidDataSource
#配置日志打印级别
logging:
level:
com:
test: trace
- 创建mapper映射文件
<?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.test.mapper.IAccountMapper">
<select id="findAccountById" parameterType="int" resultType="account">
select * from account where id=#{id}
</select>
</mapper>
- 修改Controller
@RestController
public class AccountController {
@Autowired
private IAccountMapper accountMapper;
@RequestMapping("/findAll")
public List<Account> findAllAccount(){
List<Account> allAccount = accountMapper.findAllAccount();
System.out.println(allAccount);
return allAccount;
}
@RequestMapping("/findAccount")
public Account findAccountById(Integer id){
Account account = accountMapper.findAccountById(id);
System.out.println(account);
return account;
}
}
2.Springboot整合Junit
1.搭建SpringBoot工程
2.引入起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.创建单元测试类
如果单元测试类的所在包和启动引导类所在包是一致的,则是以下写法:
@SpringBootTest
class SpringbootQuickstartWebApplicationTests {
@Test
void contextLoads() {
System.out.println("这是一个单元测试");
}
}
如果单元测试类的所在包和启动引导类所在包是不一致的,则是以下写法:
@SpringBootTest(classes = SpringbootJunitTestApplication.class)
public class text {
@Test
void contextLoads() {
System.out.println("自定义单元测试");
}
}
3.Springboot整合Redis
1.搭建SpringBoot工程
2.引入redis起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.在application.yml文件中进行redis配置
#redis端口和主机地址(单体redis)
spring:
redis:
port: 6379
host: 123.207.196.5
password: 1234
4.在单元测试类中操作Redis
@SpringBootTest
class SpringbootDay04RedisApplicationTests {
//如果redisTemplate不设置key、value的类型,存储到redis的数据,会出现乱码前缀
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
void set() {
redisTemplate.opsForValue().set("name","zhangsan");
}
@Test
void get() {
String name = (String) redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
在application.yml中不配置redis的主机地址和端口也是可以的,因为Springboot内置的有 默认配置,默认主机地址是localhost,端口是6379.
4.自定义starter起步依赖
在引入起步依赖的时候我们会发现有时候起步依赖的artifactId为spring-boot-starter-web,而有 的起步依赖为mybatis-spring-boot-starter,都是起步依赖,artifactId为什么会不一样呢?
- 以spring-boot-starter开头的都是springboot官方提供的
- 以其他开头的都是其他第三方框架官方为兼容springboot专门提供的(比如mybatisspring-boot-starter就是mybatis官方提供的)
当然我们也可以封装属于自己的起步依赖,然后到项目中使用
需求:自己封装加密起步依赖:根据使用者在yml文件中配置md5或者salt,对密码采用不同的 加密方式。
4.1.编写自动配置模块
创建springboot项目lanou-spring-boot-autoconfiguration
- 编写属性配置类,用于获取yml文件中的属性
@ConfigurationProperties(prefix = "aaa")
public class TypeProperties {
/**
* 加密方式 md5 salt
*/
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
- 编写密码加密工具类
public class TypeUtil {
private TypeProperties typeProperties;
public TypeProperties getTypeProperties() {
return typeProperties;
}
public void setTypeProperties(TypeProperties typeProperties) {
this.typeProperties = typeProperties;
}
//加密方法
/**
* 使用对应的加密方式进行密码加密,返回密文
* @param password 明文
* @return 密文
*/
private String encryption(String password){
//根据加密方式进行不同的加密操作
String newPassword = "密码加密方式不对";
if ("md5".equals(typeProperties.getType())){
newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
}else if("salt".equals(typeProperties.getType())){
password = password+"salt1232";
newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
}
return newPassword;
}
}
- 编写自定义的自动配置类
@Configuration
@EnableConfigurationProperties({TypeProperties.class})
public class LanouConfiguration {
@Bean
public TypeUtil typeUtil(TypeProperties typeProperties){
TypeUtil typeUtil = new TypeUtil();
typeUtil.setTypeProperties(typeProperties);
return typeUtil;
}
}
- 在resources中创建META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.config.LanouConfiguration
4.2.编写起步依赖模块
创建Maven项目lanou-spring-boot-starter为起步依赖模块,打包方式为pom,并引入自动配置 模块
- 在pom文件中添加自动配置模块
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.lanou</groupId>
<artifactId>lanou-spring-boot-autoconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
4.3.创建自己的项目使用自己封装的起步依赖
- 先install自动配置模块lanou-spring-boot-autoconfiguration再install起步依赖lanouspring-boot-starter
- 创建项目中引入我们自己封装的起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.lanou</groupId>
<artifactId>lanou-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- application.yml文件中配置加密方式
test:
type: md5
- 创建Controller
@RestController
public class TypeController {
@Autowired
private TypeUtil typeUtil;
@RequestMapping("/type")
public String type(String password){
return typeUtil.encryption(password);
}
}
- 启动项目(也可以选择使用mvn spring-boot:run运行项目)
- 测试查看效果