springboot integrates mybatis and redis

About SpringBoot

SpringBoot is not an extension of spring technology In fact, its appearance is to make us better use spring technology, simplify our development, and make it more convenient for us to use spring technology to develop projects

SpringBoot features

==Start dependence==
The so-called start dependency means that we import some coordinates at the beginning of the project. Importing one coordinate here is equivalent to importing many coordinates in spring, which greatly simplifies the operation of importing coordinates

==Auto configuration==
The so-called automatic configuration means that when we use spring, we usually write a large number of configuration files, but springboot is to import these same things when the project is started, and the coordinates will help us automatically configure Only some things we are not sure about need to be configured by ourselves, such as some IP addresses

SpringBoot integrates mybatis

There are two ways to integrate:
By annotation

  1. Import coordinates:
   <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</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>
  1. Configure special configuration related to database
# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  type-aliases-package: com.itheima.springbootmybatis.domain

  1. Write mapper file with annotations
@Mapper
@Repository
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();
}
// @The mapper annotation is to tell us that this file is the mapper file that tells spring to help us create this interface object. At the same time, if this annotation is used in xml, it also tells us that the xml configuration is under the same directory

Configuring via xml

  1. The first step is also to import coordinates
  2. Step 2 write the yml configuration file
# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper map file path
  type-aliases-package: com.itheima.springbootmybatis.domain
  # Mapper locations are used to specify the location of our mapper's xml file so that it can perform query access. If we do not specify it here, we will go to the same location of the interface to find the configuration file by default
  1. Step 3 write the mapper interface file
@Mapper
@Repository
public interface UserXmlMapper {

    public List<User> findAll();
}
// @The annotation "Repository" is only used to ensure that our compilation does not report errors. In fact, it has no practical effect

In fact, our SpringBoot integration mybatis is completed

SpringBoot integrates Redis

In fact, integration is the same step. You can use it by importing jar and writing configuration file. Here are the steps

  1. Import start dependency
       <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>
        </dependency>
  1. Configure Redis related properties
    In fact, your Redis in Emei has relevant default properties. We can configure them here. Generally, we need to reconfigure them
spring:
  redis:
    host: 127.0.0.1 # redis host ip
    port: 6379
  1. It can be directly used by injecting RedisTemplate template
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet() {
        //Save data
        redisTemplate.boundValueOps("name").set("zhangsan");
    }

    @Test
    public void testGet() {
        //get data
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }

}

Just remember one thing about SpringBoot: SpringBoot is actually designed to simplify the development of Spring and has no other role Another point is that you may not know what is configured in the yml file? In fact, we need to be clear about the role of SpringBoot. In order to simplify our configuration, we don't need to configure things in SpringBoot as long as you configure the same things as me. For example, the ports and scanned configuration files are different from each other. These things need to be configured

Tags: Java Spring Boot Mybatis

Posted by Elangler on Fri, 03 Jun 2022 07:33:57 +0530