• SpringBoot_03_SpringBoot对其他技术的整合


    1.SpringBoot整合Mybatis

    1.2 添加Mybatis的起步依赖

    <!--mybatis起步依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>

     1.3添加数据库驱动坐标

    <!-- MySQL连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    1.4添加数据库连接信息

    #DB Configuration:
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=root

    2 SpringBoot整合Junit

    2.1 添加Junit的起步依赖

    <!--测试的起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    2.2测试

    SpringRunner继承自SpringJUnit4ClassRunner,使用哪一个Spring提供的测试测试引擎都可以

    public final class SpringRunner extends SpringJUnit4ClassRunner 

    @SpringBootTest的属性指定的是引导类的字节码对象

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MySpringBootApplication.class)
    public class MapperTest {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        public void test() {
            List<User> users = userMapper.queryUserList();
            System.out.println(users);
        }
    
    }

    5.3 SpringBoot整合Spring Data JPA

    3.1 添加Spring Data JPA的起步依赖

    <!-- springBoot JPA的起步依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    3.2 添加数据库驱动依赖

    <!-- MySQL连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    3.3 在application.properties中配置数据库和jpa的相关属性

    #DB Configuration:
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=root
    
    #JPA Configuration:
    spring.jpa.database=MySQL
    spring.jpa.show-sql=true
    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

    如果是JDK1.9测试会报错需要加上以下依赖

    <!--jdk9需要导入如下坐标-->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>

    4 SpringBoot整合Redis

    4.1 添加redis的起步依赖

    <!-- 配置使用redis启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    4.2 配置redis的连接信息

    #Redis
    spring.redis.host=127.0.0.1
    spring.redis.port=6379

    4.3 注入RedisTemplate测试redis操作

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SpringbootJpaApplication.class)
    public class RedisTest {
    
        @Autowired
        private UserRepository userRepository;
    
        @Autowired
        private RedisTemplate<String, String> redisTemplate;
    
        @Test
        public void test() throws JsonProcessingException {
            //从redis缓存中获得指定的数据
            String userListData = redisTemplate.boundValueOps("user.findAll").get();
            //如果redis中没有数据的话
            if(null==userListData){
                //查询数据库获得数据
                List<User> all = userRepository.findAll();
                //转换成json格式字符串
                ObjectMapper om = new ObjectMapper();
                userListData = om.writeValueAsString(all);
                //将数据存储到redis中,下次在查询直接从redis中获得数据,不用在查询数据库
                redisTemplate.boundValueOps("user.findAll").set(userListData);
                System.out.println("===============从数据库获得数据===============");
            }else{
                System.out.println("===============从redis缓存中获得数据===============");
            }
    
            System.out.println(userListData);
    
        }
    
    }

    <你是我自发三杯也不肯开口的秘密>

  • 相关阅读:
    memcached与redis的一些区别小结
    ubuntu下定时任务
    hdu4135 容斥定理
    hdu1251 统计难题 字典树
    hdu1710 Binary Tree Traversals(二叉树的遍历)
    Codeforces Round #364 (Div. 2) B. Cells Not Under Attack
    KMP算法 hdu4686 Oulipo
    判断字符串是否在字典里
    floyd算法 poj2253
    最短路径dijkstra算法
  • 原文地址:https://www.cnblogs.com/asndxj/p/11826960.html
Copyright © 2020-2023  润新知