SpringBoot集成

  • 时间:
  • 来源:互联网
  • 文章标签:

集成Redis

2020.10.24参考视频

默认使用的jdk序列化,可以重写redisTemplate配置类,配置具体的序列化方式。

在企业中,我们所有的pojo类都会序列化。

  • 依赖包

    <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  • 配置参数

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    
  • 单元测试

    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        void contextLoads() {
            //opsForValue() 操作字符串
            //opsForList() 操作列表
            //opsForSet()
            redisTemplate.opsForValue().set("myKey","YaoJun");
            System.out.println(redisTemplate.opsForValue().get("myKey"));
        }
    
    }
    

整合JDBC

2020.10.20狂神学习视频

Spring Boot底层统一采用Spring data进行处理

  • 控制层

    @RestController
    public class JdbcController {
        @Autowired
        JdbcTemplate jdbcTemplate;
    
        //查询数据库的所有信息
        @GetMapping("/userList")
        public List<Map<String, Object>> userList(){
            String sql = "select * from department";
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            return maps;
        }
    
        @GetMapping("/addUser")
        public String addUser(){
            String sql = "insert into yaojun.department(id,name,phone) values(3, 'Jack',8910)";
            jdbcTemplate.update(sql);
            return "update_ok";
        }
    }
    
  • 依赖包

    <dependencies>
    <!--1.jdbc-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    <!--2.mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.22</version>
                <scope>runtime</scope>
            </dependency>
    <!--3.web依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
  • 配置文件

    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/yaojun?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
        driver-class-name: com.mysql.cj.jdbc.Driver
    

整合Mybatis

主要是将sql语句的编写统一放到了配置文件中,需要结合jdbc使用。

  • 依赖包

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.3</version>
    </dependency>
    
  • 附加配置

    # 整合mybatis
    mybatis:
      type-aliases-package: com.bytedance.pojo
      mapper-locations: mybatis/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.bytedance.mapper.UserMapper">
    <!--查询数据-->
        <select id="queryUserList" resultType="User">
            select * from user;
        </select>
    <!--添加数据-->
        <insert id="addUser" parameterType="User">
            insert into user (id, name, pwd) values (#{id}, #{name}, #{pwd})
        </insert>
    </mapper>
    
  • 模型层

    @Repository
    @Mapper
    //表示这是一个mybatis的mapper类
    public interface UserMapper {
        List<User> queryUserList();
        User queryUserById(int id);
        int addUser(User user);
        int updateUser(User user);
        int deleteUser(int id);
    }
    
  • 实体类

    @Data
    @NoArgsConstructor
    public class User {
        private int id;
        private String name;
        private int pwd;
    
        public User(int id, String name, int pwd) {
            this.id = id;
            this.name = name;
            this.pwd = pwd;
        }
    }
    
  • 控制层

    @RestController
    public class UserController {
        @Autowired
        private UserMapper userMapper;
    
        //查询用户
        @GetMapping("/queryUserList")
        public List<User> queryUserList(){
            List<User> users = userMapper.queryUserList();
            for (User user : users) {
                System.out.println(user);
            }
            return users;
        }
    
        //添加用户
        @GetMapping("/addUser")
        public String addUser(){
           userMapper.addUser(new User(5,"Tim",12345));
            return "addUser_ok";
        }
    }
    

本文链接http://www.taodudu.cc/news/show-1781754.html