springboot 整合其他框架

  • springboot整合mybatis
  1. 在创建springboot项目的时候选择mybatis框架和数据库驱动依赖。

     

     

  2. 写一个实体类。
    package com.example.entity;
    
    public class User {
    
        private Integer id;
    
        private String name;
    
        private Integer age;
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        private String tel;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getTel() {
            return tel;
        }
    
        public void setTel(String tel) {
            this.tel = tel;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id + "age=" + age +
                    ", name='" + name + '\'' +
                    ", tel='" + tel + '\'' +
                    '}';
        }
    }

     

  3. 写一个mapper接口。
    package com.example.mapper;
    
    import com.example.entity.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Mapper
    @Repository //可加可不加。加了之后在后面注入的时候代码不会报红。不加也没事
    public interface UserMapper {
    
        List<User> findAll();
    }

     

  4. 写一个mapper.xml文件。对应mapper接口中的方法。
    <?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.example.mapper.UserMapper">
        <select id="findAll" resultType="User">
            select * from user
        </select>
    
    </mapper>

     

  5. application.yml配置文件。
    #datasource
    spring:
      datasource:
        url: jdbc:mysql:///springboot_mybatis?serverTimezone=UTC  
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    
    # mybatis
    mybatis:
      mapper-locations: classpath:mapper/*Mapper.xml  # mapper映射文件位置
      type-aliases-package: com.example.entity #持久化类包位置
      # cofig-location #指定mybatis核心配置文件

     

  6. 在测试类中测试。
    package com.example;
    
    import com.example.entity.User;
    import com.example.mapper.UserMapper;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class SpringbootMybaitsApplicationTests {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        void contextLoads() {
        }
    
        @Test
        public void test(){
            List<User>  userList = userMapper.findAll();
            System.out.println(userList);
        }
    }

    测试结果: 

             

 

 

  •  springboot整合redis.
  1. 在创建springboot项目时选择Redis依赖。

          

  2.直接在测试类中测试(前提是本地已经下载安装好了Redis)。因为这里用的是本地的redis,默认主机号是本机,端口是6379,所以这里没写配置文件直接就用了。实际开发中想远程连接redis的话,可以在配置文件中配置,后面会讲到。

package com.example;

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
    void contextLoads() {
    }

    @Test
    public void testSet(){
        redisTemplate.boundValueOps("name").set("张三");
    }

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

  

  3.测试结果(别忘了开启redis)。测试testSet()时,如果没报错就说明成功了。也可以通过testGet()方法检查。         

 

 

         

 

 

 

  4.redis配置信息。

spring:
  redis:
    host: 127.0.0.1 # redis主机ip
    port: 6379

 

posted @ 2021-10-24 14:03  晚安1024  阅读(151)  评论(0)    收藏  举报