• Spring Boot整合 Redis


    <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>
        <groupId>com.itheima</groupId>
        <artifactId>chapter03redis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- Spring Data Redis依赖启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            
            <!-- Spring Data JPA依赖启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            
            <!-- 阿里巴巴的Druid数据源依赖启动器 -->
            <!-- <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency> -->
    
            <!-- MyBatis依赖启动器 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.0</version>
            </dependency>
            <!-- MySQL数据库连接驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version><!--$NO-MVN-MAN-VER$-->
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    server:
      port: 8082
      
    spring:
      redis:
        host: 127.0.0.1
        port: 6379
        password: 
          
    mybatis:
      configuration:
        map-underscore-to-camel-case: true
      mapper-locations:
      - classpath:mapper/*.xml
      type-aliases-package: com.itheima.domain
    package com.itheima.domain;
    
    import java.util.List;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.redis.core.RedisHash;
    import org.springframework.data.redis.core.index.Indexed;
    
    @RedisHash("persons") // 指定操作实体类对象在Redis数据库中的存储空间
    public class Person {
        @Id // 标识实体类主键
        private String id;
        @Indexed // 标识对应属性在Redis数据库中生成二级索引
        private String firstname;
        @Indexed
        private String lastname;
        private Address address;
        private List<Family> familyList;
    
        public Person() {
        }
    
        public Person(String firstname, String lastname) {
            this.firstname = firstname;
            this.lastname = lastname;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public List<Family> getFamilyList() {
            return familyList;
        }
    
        public void setFamilyList(List<Family> familyList) {
            this.familyList = familyList;
        }
    
        @Override
        public String toString() {
            return "Person{" + "id='" + id + '\'' + ", firstname='" + firstname + '\'' + ", lastname='" + lastname + '\''
                    + ", address=" + address + ", familyList=" + familyList + '}';
        }
    }
    package com.itheima.domain;
    
    import org.springframework.data.redis.core.index.Indexed;
    
    public class Address {
        @Indexed
        private String city;
        @Indexed
        private String country;
    
        public Address() {
        }
    
        public Address(String city, String country) {
            this.city = city;
            this.country = country;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        @Override
        public String toString() {
            return "Address{" + "city='" + city + '\'' + ", country='" + country + '\'' + '}';
        }
    }
    package com.itheima.domain;
    
    import org.springframework.data.redis.core.index.Indexed;
    
    public class Family {
        @Indexed
        private String type;
        @Indexed
        private String username;
    
        public Family() {
        }
    
        public Family(String type, String username) {
            this.type = type;
            this.username = username;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        @Override
        public String toString() {
            return "Family{" + "type='" + type + '\'' + ", username='" + username + '\'' + '}';
        }
    }
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    package com.itheima.repository;
    
    import java.util.List;
    
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.repository.CrudRepository;
    
    import com.itheima.domain.Person;
    
    public interface PersonRepository extends CrudRepository<Person, String>{
        List<Person> findByLastname(String lastname);
    
        Page<Person> findPersonByLastname(String lastname, Pageable page);
    
        List<Person> findByFirstnameAndLastname(String firstname, String lastname);
    
        List<Person> findByAddress_City(String city);
    
        List<Person> findByFamilyList_Username(String username);
    }
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    package com.itheima;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.itheima.domain.Address;
    import com.itheima.domain.Family;
    import com.itheima.domain.Person;
    import com.itheima.repository.PersonRepository;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class TestApplication {
        @Autowired
        private PersonRepository repository;
    
        @Test
        public void savePerson() {
            Person person = new Person("张", "有才");
            Person person2 = new Person("James", "Harden");
            // 创建并添加住址信息
            Address address = new Address("北京", "China");
            person.setAddress(address);
            // 创建并添加家庭成员
            List<Family> list = new ArrayList<>();
            Family dad = new Family("父亲", "张良");
            Family mom = new Family("母亲", "李香君");
            list.add(dad);
            list.add(mom);
            person.setFamilyList(list);
            // 向Redis数据库添加数据
            Person save = repository.save(person);
            Person save2 = repository.save(person2);
            System.out.println(save);
            System.out.println(save2);
        }
    
        @Test
        public void selectPerson() {
            List<Person> list = repository.findByAddress_City("北京");
            System.out.println(list);
        }
    
        @Test
        public void updatePerson() {
            Person person = repository.findByFirstnameAndLastname("张", "有才").get(0);
            person.setLastname("小明");
            Person update = repository.save(person);
            System.out.println(update);
        }
    
        @Test
        public void deletePerson() {
            Person person = repository.findByFirstnameAndLastname("张", "小明").get(0);
            repository.delete(person);
        }
    }

     

  • 相关阅读:
    cisco telnet(转载)
    华为bfd配置步骤
    cisco ssh实验--附带配置脚本-2019.11.19
    远程设备管理opendx平台搭建-server,agent以及front实际搭建
    远程设备管理opendx平台搭建-appium和adb的安装
    华为交换机sflow配置
    华为交换机netstream配置
    centos6虚拟机复制后修改网卡
    docker基本操作
    esxi 6 添加硬盘、网卡
  • 原文地址:https://www.cnblogs.com/tszr/p/15911123.html
Copyright © 2020-2023  润新知