springboot测试
This commit is contained in:
commit
09a89ee48f
|
@ -0,0 +1,50 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<!-- 继承父项目-->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.0.5</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
|
<groupId>com.atguigu.java</groupId>
|
||||||
|
<artifactId>01_springboot_part</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<!-- web项 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>3.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.30</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.atguigu.java;
|
||||||
|
|
||||||
|
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||||
|
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootRunner{
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//启动tomcat即可
|
||||||
|
SpringApplication.run(SpringBootRunner.class,args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.atguigu.java.interceptors;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* projectName: com.atguigu.java.interceptors
|
||||||
|
*
|
||||||
|
* @author: 赵伟风
|
||||||
|
* description:
|
||||||
|
*/
|
||||||
|
public class AllInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
// if(!preHandler()){return;}
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
System.out.println("AllInterceptor.preHandle");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||||
|
System.out.println("AllInterceptor.postHandle");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
System.out.println("AllInterceptor.afterCompletion");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.atguigu.java.javaconfig;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfigration {
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
|
||||||
|
|
||||||
|
//创建一个对象
|
||||||
|
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
|
||||||
|
//与库连接上的关键数据
|
||||||
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
//String类型的序列化器
|
||||||
|
RedisSerializer<String> stringSerializer = RedisSerializer.string();
|
||||||
|
//json类型的序列化器
|
||||||
|
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer
|
||||||
|
= new GenericJackson2JsonRedisSerializer();
|
||||||
|
|
||||||
|
// 设置序列化器
|
||||||
|
|
||||||
|
redisTemplate.setKeySerializer(stringSerializer);
|
||||||
|
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
|
||||||
|
redisTemplate.setHashKeySerializer(stringSerializer);
|
||||||
|
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
|
||||||
|
|
||||||
|
//返回数据
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.atguigu.java.javaconfig;
|
||||||
|
|
||||||
|
import com.atguigu.java.interceptors.AllInterceptor;
|
||||||
|
import org.springframework.cglib.transform.impl.AddInitTransformer;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* projectName: com.atguigu.java
|
||||||
|
*
|
||||||
|
* @author: 赵伟风
|
||||||
|
* description: 目标;将拦截器加入到ioc容器的同时还要设置拦截和放行地址!
|
||||||
|
* 1. 配置类添加@Configuration注解
|
||||||
|
* 2. springmvc的配置类需要使用特殊的接口WebMvcConfigurer
|
||||||
|
* 3. WebMvcConfigurer接口中提供各种方法配置特殊(拦截器)的组件对象
|
||||||
|
* 4. 重写addInterceptor方法
|
||||||
|
* 5. 方法的形参对象 InterceptorRegistry 拦截器加入到ioc容器和配置拦截和放行的地址
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class SpringMvcConfiguration implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new AllInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/a").order(10);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.atguigu.java.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.java.pojo.Employee;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface EmpMapper {
|
||||||
|
|
||||||
|
List<Employee> getAllEmp();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.atguigu.java.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.java.pojo.OrderItems;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface OrderItemsMapper {
|
||||||
|
OrderItems selectOrderItems(Integer id);
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.atguigu.java.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.java.pojo.Orders;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface OrdersMapper {
|
||||||
|
List<Orders> getAllOrders(Integer userId);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.atguigu.java.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.java.pojo.Users;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UsersMapper {
|
||||||
|
|
||||||
|
List<Users> getAllUsersItem(Integer id);
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.java.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
private Integer empId;
|
||||||
|
private String empName;
|
||||||
|
private Double empSalary;
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.java.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class OrderItems {
|
||||||
|
private Integer id;
|
||||||
|
private Integer orderId;
|
||||||
|
private String productName;
|
||||||
|
private Integer quantity;
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.atguigu.java.pojo;
|
||||||
|
|
||||||
|
import com.atguigu.java.mapper.OrderItemsMapper;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Orders {
|
||||||
|
private Integer id;
|
||||||
|
private Integer userId;
|
||||||
|
private String orderNumber;
|
||||||
|
private OrderItems orderItems;
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.java.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Student {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private Integer age;
|
||||||
|
private Integer id;
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.atguigu.java.pojo;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Users {
|
||||||
|
private Integer id;
|
||||||
|
private String username;
|
||||||
|
private String email;
|
||||||
|
private List<Orders> orders;
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
spring:
|
||||||
|
banner:
|
||||||
|
charset: utf-8
|
||||||
|
location: classpath:banner.txt
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://localhost:3306/mybatis-example
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
username: root
|
||||||
|
password: 735289578
|
||||||
|
data:
|
||||||
|
redis:
|
||||||
|
host: 192.168.6.100
|
||||||
|
port: 6379
|
||||||
|
mybatis:
|
||||||
|
mapper-locations: classpath:/mapper/*.xml
|
||||||
|
type-aliases-package: com.atguigu.java.pojo
|
||||||
|
configuration:
|
||||||
|
map-underscore-to-camel-case: true
|
||||||
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
|
auto-mapping-behavior: full
|
||||||
|
lazy-loading-enabled: true
|
||||||
|
aggressive-lazy-loading: false
|
|
@ -0,0 +1,22 @@
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// _ooOoo_ //
|
||||||
|
// o8888888o //
|
||||||
|
// 88" . "88 //
|
||||||
|
// (| ^_^ |) //
|
||||||
|
// O\ = /O //
|
||||||
|
// ____/`---'\____ //
|
||||||
|
// .' \\| |// `. //
|
||||||
|
// / \\||| : |||// \ //
|
||||||
|
// / _||||| -:- |||||- \ //
|
||||||
|
// | | \\\ - /// | | //
|
||||||
|
// | \_| ''\---/'' | | //
|
||||||
|
// \ .-\__ `-` ___/-. / //
|
||||||
|
// ___`. .' /--.--\ `. . ___ //
|
||||||
|
// ."" '< `.___\_<|>_/___.' >'"". //
|
||||||
|
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
|
||||||
|
// \ \ `-. \_ __\ /__ _/ .-` / / //
|
||||||
|
// ========`-.____`-.___\_____/___.-`____.-'======== //
|
||||||
|
// `=---=' //
|
||||||
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
|
||||||
|
// 佛祖保佑 永不宕机 永无BUG //
|
||||||
|
////////////////////////////////////////////////////////////////////
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.java.mapper.EmpMapper">
|
||||||
|
<select id="getAllEmp" resultType="employee">
|
||||||
|
select * from t_emp;
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.java.mapper.OrderItemsMapper">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<select id="selectOrderItems" resultType="orderItems">
|
||||||
|
select * from order_items where order_id=#{id}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.java.mapper.OrdersMapper">
|
||||||
|
<resultMap id="orderMap" type="orders">
|
||||||
|
<id property="id" column="id"/>
|
||||||
|
<association property="orderItems" javaType="orderItems"
|
||||||
|
select="com.atguigu.java.mapper.OrderItemsMapper.selectOrderItems"
|
||||||
|
column="order_number"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<select id="getAllOrders" resultMap="orderMap">
|
||||||
|
select * from orders where user_id=#{userId}
|
||||||
|
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.java.mapper.UsersMapper">
|
||||||
|
|
||||||
|
<!--resultMap-->
|
||||||
|
<!-- <resultMap id="usersmap" type="users">-->
|
||||||
|
<!-- <id property="id" column="id"/>-->
|
||||||
|
<!-- <collection property="orders" ofType="Orders">-->
|
||||||
|
<!-- <id property="userId" column="user_id"/>-->
|
||||||
|
<!-- </collection>-->
|
||||||
|
<!-- </resultMap>-->
|
||||||
|
|
||||||
|
|
||||||
|
<resultMap id="userAllItem" type="users">
|
||||||
|
<id property="id" column="id"/>
|
||||||
|
<collection property="orders" ofType="com.atguigu.java.pojo.Orders"
|
||||||
|
select="com.atguigu.java.mapper.OrdersMapper.getAllOrders"
|
||||||
|
column="id"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<!-- select方法-->
|
||||||
|
|
||||||
|
<!-- <select id="getAllUsers" resultMap="usersmap">-->
|
||||||
|
<!-- select * from users u join orders o on u.id=o.user_id where u.id=#{id};-->
|
||||||
|
<!-- </select>-->
|
||||||
|
|
||||||
|
|
||||||
|
<select id="getAllUsersItem" resultMap="userAllItem">
|
||||||
|
select * from users where id=#{id};
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.atguigu.java;
|
||||||
|
|
||||||
|
import com.atguigu.java.mapper.OrderItemsMapper;
|
||||||
|
import com.atguigu.java.mapper.OrdersMapper;
|
||||||
|
import com.atguigu.java.mapper.UsersMapper;
|
||||||
|
import com.atguigu.java.pojo.Orders;
|
||||||
|
import com.atguigu.java.pojo.Student;
|
||||||
|
import com.atguigu.java.pojo.Users;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class DemoTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String,Object> redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test01(){
|
||||||
|
Student student = new Student();
|
||||||
|
student.setName("张三");
|
||||||
|
student.setAge(18);
|
||||||
|
student.setId(1);
|
||||||
|
redisTemplate.opsForValue().set("student",student);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue