IDEA创建SpringBoot项目并整合SSM+Redis

2 篇文章 0 订阅
2 篇文章 0 订阅

1.创建SpringBoot项目

(1). File---->New---->Project,选择Spring Initiatilizr
在这里插入图片描述
(2)点击下一步,填入相应的GroupId,选择Java Version等,在选择下一步
在这里插入图片描述
(3)选择包依赖
如果只是创建一个简单的SpringBoot项目,可以只选择Web—>Spring Web,其他的依赖可以之后加到pom.xml中,也可以在这个页面选择
在这里插入图片描述
(4)选好依赖点击下一步,输入项目名称,点击Finish
在这里插入图片描述
2.测试案例1,输出数据

(1)在主函数对应所在的包或子包下创建java文件,我创建了Controller包,并在包下面创建了一个测试文件TestController
在这里插入图片描述

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class TestController {
    @RequestMapping("/test")
    public String test(){
        return "hello";
    }
}

然后运行DemoApplication
在这里插入图片描述
在这里插入图片描述
3.测试案例2,SpringBoot跳转到jsp页面

SpringBoot跳转到jsp页面需要以下几步:
(1). 创建webapp/WEB-INF/jsp目录,目录名称可以随意改变,将webapp目录设置为web目录,File—>Project Structure
在这里插入图片描述
在Module中添加web,并添加Web Resource Diectories
在这里插入图片描述
(2)在webapp/WEB-INF/jsp目录下创建对应的jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>Test 页面</p>
</body>
</html>

(3)加入对应的依赖和进行相应的配置
pom.xml依赖:

<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

application.properties配置

#spring mvc视图响应配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

(3)Controller

@Controller
@ResponseBody
public class TestController {
    @RequestMapping("/test")
    public ModelAndView test(){
        return new ModelAndView("Test");
    }
}

如果是String类型输出的还是字符串而不是页面的话,建议将方法类型改成ModelAndView,结果
在这里插入图片描述

4.连接数据库(mybatis)

1.pom.xml依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2.application.properties

#mysql配置
spring.datasource.url=jdbc:mysql://localhost:3306/gp?serverTimezone=GMT&useSSL=false&userUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234
#mybatis配置
mybatis.mapper-locations=classpath*:Mapper/*.xml

运行
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
5.连接Redis

1.pom.xml依赖

   <!--redis依赖配置-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.application.properties

#redis配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=12345
spring.redis.jedis.pool.max-idle=50
spring.redis.jedis.pool.max-wait=3000
spring.redis.timeout=5000

3.创建RedisConfig类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@PropertySource("classpath:/application.properties")
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int MaxIdle;
    @Value("${spring.redis.jedis.pool.max-wait}")
    private int Maxwait;
    @Bean
    public JedisPool redisPoolFactory()  throws Exception{

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(MaxIdle);
        jedisPoolConfig.setMaxWaitMillis(Maxwait);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;
    }

}

4.在需要缓存的流程中进行注入

  @Autowired
    private JedisPool jedisPool;
    Jedis jedis = jedisPool.getResource();

5.运行结果
在这里插入图片描述
在这里插入图片描述
这个缓存是没有序列化,也存不了jason数据,需要序列化,存jason数据的可以看看其他博主的

6.项目使用HTML页面

项目用HTML页面的话,可以将application.properties里面的spring.mvc.view.suffix=.jsp改成spring.mvc.view.suffix=.html,或者使用thymeleaf模板
1.pom.xml依赖

 <!--thymeleaf模板依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--自动标签解析-->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

2.application.properties

#thymeleaf配置
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode =LEGACYHTML5
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#配置静态资源路径
spring.mvc.static-path-pattern=/static/**

html页面需要写在templates目录下面才能跳转,不然会报错
这个templates配置会将spring mvc的配置覆盖掉,不能同时用
项目一定要放在主函数对应所在的包或子包下,对应的资源文件放在resources目录下面,最后,附上我的包结构吧
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值