Spring整合其他框架
1.SpringBoot整合Junit
測試類需要寫在引導類所在的包及其子包。否則,需要加
@SpringBootTest(classes = SpringbootConfigApplication.class)
實現步驟:
1.搭建SpringBoot工程
2.在pom檔案引入starter-test起步依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.編寫測試類
@Service
public class UserService {
public void add(int i) {
if(i == 0) {
System.out.println("0....");
}else{
System.out.println("1....");
}
System.out.println("add...");
}
}
4.編寫測試方法
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=啟動類.class)
/**
* 如果測試類在引導類所在包及其子包,可不寫 classes = SpringbootTestApplication.class
*/
@SpringBootTest
class SpringbootConfigApplicationTests {
@Autowired
private UserService userService;
@Test
void contextLoads() {
userService.add(0);
userService.add(1);
}
}
2.SpringBoot整合Mybatis
1.搭建SpringBoot工程
2.mybatis起步依賴,mysql驅動
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.編寫DataSource和MyBatis相關配置
spring.datasource.url=jdbc:mysql:///springboot?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
4.定義表和實體類
t_user
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springboot` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `springboot`;
/*Table structure for table `t_user` */
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*Data for the table `t_user` */
insert into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
User.java
public class User {
private int id;
private String username;
private String password;
... //get set toString
}
5.編寫dao和mapper檔案/純註解開發
UserMapper.java
/**
* 純註解開發方式
*
* @author liqp
* @version 1.0
* @date 2020/12/19
*/
//@Mapper
public interface UserMapper {
@Select("select * from t_user")
List<User> findAll();
}
UserMapperXml.java
/**
* @author liqp
* @version 1.0
* @date 2020/12/19
*/
//@Mapper
public interface UserXmlMapper {
List<User> findAll();
}
UserMapper.xml
<?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.itheima.springbootmybatis.mapper.UserXmlMapper">
<select id="findAll" resultType="com.itheima.springbootmybatis.domain.User">
select * from t_user
</select>
</mapper>
6.引導類新增mapper掃描
@SpringBootApplication
@MapperScan("com.itheima.springbootmybatis.mapper")//加上該註解後,可以不用在mapper介面上加@Mapper註解
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
7.測試
SpringbootMybatisApplicationTests.java
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserXmlMapper userXmlMapper;
@Test
void contextLoads() {
List<User> all = userMapper.findAll();
System.out.println(all);
}
@Test
void testFindAllXml() {
List<User> all = userXmlMapper.findAll();
System.out.println(all);
}
}
3.SpringBoot整合Redis
1.搭建SpringBoot工程
2.引入redis起步依賴
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置redis相關屬性 (如果是本地可忽略)
spring.redis.host=localhost
spring.redis.port=6379
4.注入RedisTemplate模板
5.編寫測試方法,測試
package com.itheima.springbootredis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSet() {
redisTemplate.boundValueOps("name").set("wangwu");
}
@Test
public void testGet() {
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
相關文章
- Spring框架|整合JdbcTemplateSpring框架JDBC
- Mybatis和其他主流框架的整合使用MyBatis框架
- 【SSH框架】系列之 Spring 整合 Hibernate 框架框架Spring
- Spring框架(五)實戰Spring整合MybatisSpring框架MyBatis
- Spring+SpringMVC+MyBatis框架整合SpringMVCMyBatis框架
- Spring Boot整合熱部署框架Spring Boot熱部署框架
- Spring Boot:整合Shiro許可權框架Spring Boot框架
- 【Spring Boot】整合Netty Socket.IO通訊框架Spring BootNetty框架
- SSM(Spring-MyBatis-SpringMVC)框架整合【完整版】SSMMyBatisSpringMVC框架
- 整合測試——Spring TestContext框架支援詳細解說SpringContext框架
- Spring GraphQL與Netflix領域圖服務框架整合Spring框架
- Spring Cloud 快速入門(八)訊息系統整合框架 Spring Cloud StreamSpringCloud框架
- jQuery 與其他框架相容jQuery框架
- [Android]後端之路--整合SSM(Spring+SpringMVC+MyBatis)框架(2)Android後端SSMSpringMVCMyBatis框架
- SSM三大框架整合詳細教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- Spring框架 - Spring和Spring框架組成Spring框架
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring AopSpring Boot
- SSM框架整合SSM框架
- 整合SSM框架SSM框架
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- mybatis整合springMyBatisSpring
- Spring 整合 HibernateSpring
- ActiveMq整合SpringMQSpring
- Spring Cloud 整合SpringCloud
- Spring 整合 MyBatisSpringMyBatis
- Spring整合WebSocketSpringWeb
- Spring整合JUnitSpring
- Spring整合MyBatisSpringMyBatis
- spring:spring與mybatis的整合SpringMyBatis
- Spring Boot:整合Spring Data JPASpring Boot
- 微服務框架-dubbo整合nacos框架微服務框架
- spring+redis的整合,使用spring-data-redis來整合SpringRedis
- CI 框架整合 PHPExcel框架PHPExcel
- SSM框架的整合SSM框架
- spring-boot 整合mybatis-plus 組成後臺開發基本框架SpringbootMyBatis框架
- spring-boot 整合 spring-securitySpringboot