一、spring boot 是什么?

spring boot 基于spring 的框架。目的:提供给开发人员简单易用的工具包,提高开发效率。 最重要的特点:自动配置

springboot 优点

  1. 直接嵌入Tomcat
  2. 自动解决jar包冲突
  3. 不需要xml配置,yml

二、springboot整合mybatis

  1. 添加起步依赖
  2. 配置yml 指定映射文件位置
  3. 创建启动类,加入注解扫描

1.添加起步依赖

<dependencies>
        <!--驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--mybatis的 起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--spring web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

2. 创建配置application.yml文件:

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC
username: root
password: 1231

#配置mapper的映射文件的位置
mybatis:
mapper-locations: classpath:mappers/*Mapper.xml

3. 创建启动类,加入mapper接口注解扫描

@SpringBootApplication
@MapperScan(basePackages = "com.itheima.dao")
//MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spriing容器
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class,args);
    }
}

三、springboot整合redis

  1. 添加起步依赖:   spring-boot-starter-data-redis
  2. 准备好redis服务器并启动
  3. 在Service中的方法中使用 
  4. 注入@RedisTemplate或者@StringRedisTemplate
  5. 在方法中进行调用
  6. 配置yml 配置redis的服务器的地址

yml中连接redis:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
  redis:
    host: localhost
    port: 6379
#配置mapper的映射文件的位置
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml

 service中进行调用:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAllUser() {

        //1.获取redis中的数据
        List<User> list = (List<User>) redisTemplate.boundValueOps("key_all").get();
        //2.判断 是否有,如果有则返回,如果没有则从mysql中获取设置到redis中再返回
        if (list != null && list.size() > 0) {
            return list;
        }
        List<User> allUser = userMapper.findAllUser();

        //3 从mysql中获取设置到redis中再返回
        redisTemplate.boundValueOps("key_all").set(allUser);

        return allUser;
    }
}

四、自动注册原理

更多推荐

java框架高频面试题3(SpringBoot面试题)