Spring Boot概述(二)

1.SpringBoot整合Junit

  • 1.搭建SpringBoot工程

  • 2.引入starter-test起步依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
  • 3.编写测试类

    @Service
    public class UserService {
        public void add(){
            System.out.println("add...");
        }
    }
    
    1. 添加测试相关注解 @SpringBootTest
    1. 编写测试方法
    @SpringBootTest(classes = SpringbootTestApplication.class)
    //这里加不加classes取决于当前测试类包所在的位置
    //在引导类所在包的子包或同级则不需要加(会自动找引导类),否则要加
    class SpringbootRedisApplicationTests {
    
        @Autowired
        UserService userService;
        @Test
        void contextLoads() {
            userService.add();
        }
    }
    

2.SpringBoot整合Redis

  • 1.搭建SpringBoot工程

  • 2.引入redis起步依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    1. 配置redis相关属性
    spring:
      redis:
        host: 127.0.0.1 #redis的主机ip
        port: 6379
    
    1. 注入RedisTemplate模板
    1. 编写测试方法,测试(记得打开本机的redis)
    @SpringBootTest
    class SpringbootRedisApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testSet(){
            //存数剧
            redisTemplate.boundValueOps("name").set("zhangsan");
        }
        @Test 
        void testGet(){
            //获取数据
            Object name = redisTemplate.boundValueOps("name").get();
            System.out.println(name);
        }
    }
    
    

3.SpringBoot整洁MyBatis

  • 1.搭建SpringBoot工程

  • 2.引入mybatis起步依赖,添加mysql驱动

    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>2.2.0version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
    dependency>
    
    
    1. 编写DataSource和MyBatis相关配置
    • DataSource配置信息:

      spring:
        datasource:
          url: jdbc:mysql:///mzz
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
      
    • 用注解开发可以不用写MyBatis的配置

    • xml开发MyBatis相关配置:

      mybatis:
        mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径
        type-aliases-package: com.itheima.springbootmybatis.domain #配置别名
      #config-location: 指定mybatis的核心配置文件
      
      
  • 4.定义表和实体类

    USE `mzz`;
    
    DROP TABLE IF EXISTS `t_user`;
    
    CREATE TABLE `t_user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    insert  into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
    
    

    Spring Boot概述(二)_第1张图片

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private Integer id;
        private String username;
        private String password;
    }
    
    
    1. 编写dao和mapper文件/纯注解开发 、
      xml开发:
    @Mapper
    @Repository
    public interface UserXmlMapper {
    
        public List<User> findAll();
    }
    
    
    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.itheima.springbootmybatis.mapper.UserXmlMapper">
        <select id="findAll" resultType="user">
            select * from t_user
        select>
    mapper>
    
    

    纯注解开发:

    
    @Mapper
    @Repository//这里可加可不加,mybatis提供@Mapper可以代替
    public interface UserMapper {
        @Select("select * from t_user")
        public List<User> findAll();
    }
    
  • 6.测试

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserXmlMapper userXmlMapper;

    @Test
    void testFindAll() {
        List<User> res = userMapper.findAll();
        for (User user : res) {
            System.out.println(user);
        }
    }

    @Test
    void testFindAll1() {
        List<User> res = userXmlMapper.findAll();
        for (User user : res) {
            System.out.println(user);
        }
    }
}

参考视频:
https://www.bilibili.com/video/BV1Lq4y1J77x?spm_id_from=333.999.0.0

参考链接:
https://blog.csdn.net/qq_45966440/article/details/120450596?spm=1001.2014.3001.5502

你可能感兴趣的:(spring,boot,mybatis,java)