写点什么

Java 之 Spring Boot 入门到精通【IDEA 版】SpringBoot 整合其他框架

  • 2022 年 4 月 29 日
  • 本文字数:2622 字

    阅读完需:约 9 分钟





[](()2、引入 redis 起步依赖

  • 通过上述的自动创建的工程 redis 的依赖已经自动添加好


[](()3、在测试方法当中默认连接本地的 redis 数据库

不需要配置如何信息



package cn.itbluebox.springbootredis;


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.BoundValueOperations;


import org.springframework.data.redis.core.RedisTemplate;


@SpringBootTest


class SpringbootRedisApplicationTests {


@Autowired


private RedisTemplate redisTemplate;


@Test


void testSet() {


//存入数据


redisTemplate.boundValueOps("name").set("张三");


}


@Test


void testGet() {


//获取数据


Object name = redisTemplate.boundValueOps("name").get();


System.out.println(name);


}


}


  • 启动 redis




  • 运行测试类(测试方法 testSet()存入数据)




  • 运行测试类(测试方法 testGet()获取数据)



[](()4、配置 redis 相关属性




spring:


redis:


host: 127.0.0.1 #redis 的主机地址


port: 6379


  • 运行测试





[](()三、SpringBoot 整合 MyBatis



[](()1、搭建 SpringBoot 工程





[](()2、引入 mybatis 起步依赖,添加 mysq|驱动

通过上述的工程搭建自动添加好了依赖


[](()3、定义表和实体类

  • 创建数据库


CREATE DATABASE /!32312 IF NOT EXISTS/springboot /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;


USE springboot;


/*Table structure for table t_user */


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;


/*Data for the table t_user */


insert into t_user(id,username,password) values (1,'zhangsan','123'),(2,'lisi','234');



  • 创建实体类




package cn.tbluebox.springbootmybatis.domain;


public class User {


private int id;


private String username;


private String password;


public User() {


}


public User(int id, String username, String password) {


this.id = id;


this.username = username;


this.password = password;


}


public int getId() {


return id;


}


public void setId(int id) {


this.id = id;


}


public String getUsername() {


return username;


}


public void setUsername(String username) {


this.username = username;


}


public String getPassword() {


return password;


}


public void setPassword(String password) {


this.password = password;


}


@Override


public String toString() {


return "User{" +


"id=" + id +


", username='" + username + ''' +


", password='" + password + ''' +


'}';


}


}

[](()4、编写 DataSource 和 MyBatis 相关配置




#datasource


spring:


datasource:


url: jdbc:mysql:///springboot


username: root


password: root


driver-class-name: com.mysql.jdbc.Driver

[](()5、编写 dao 和 mapper 文件/纯注解开发



package cn.tbluebox.springbootmybatis.mapper;


import cn.tbluebox.springbootmybatis.domain.User;


import org.apache.ibatis.annotations.Mapper;


import org.apache.ibatis.annotations.Select;


import java.util.List;


@Mapper


@Repository


public interface UserMapper {


@Select("select * from t_user")


public List<User> findAll();


}

[](()6、测试


package cn.tbluebox.springbootmybatis;


import cn.tbluebox.springbootmybatis.domain.User;


import cn.tbluebox.springbootmybatis.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 SpringbootMybatisApplicationTests {


@Autowired


private UserMapper userMapper;


@Test


void findAllUser() {


List<User> all = userMapper.findAll();


System.out.println(all);


}


}


运行测试类



[](()7、编写 dao 和 mapper 文件/XML 开发

[](()(1)创建 UserXmlMapper 接口



package cn.tbluebox.springbootmybatis.mapper;


import cn.tbluebox.springbootmybatis.domain.User;


import org.apache.ibatis.annotations.Mapper;


import org.springframework.stereotype.Repository;


import java.util.List;


@Mapper


@Repository


public interface UserXmlMapper {


public List<User> findAll();


}

[](()(2) 编写配置文件



![在这里插入图片描述](https://img-blog.csdnimg.cn/32c070ca8b0b42629c3e71ca467b566a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6JOd55uS5a2QaXRib 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 HVlYm94,size_20,color_FFFFFF,t_70,g_se,x_16)


<?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="cn.tbluebox.springbootmybatis.mapper.UserXmlMapper">


<select id="findAll" resultType="user">


select * from t_user


</select>


</mapper>

[](()(3) 配置配置文件


#datasource


spring:


datasource:


url: jdbc:mysql:///springboot


username: root


password: root


driver-class-name: com.mysql.jdbc.Driver

mybatis

mybatis:


mapper-locations: classpath:mapper/*Mapper.xml #mapper 映射文件的路径


type-aliases-package: cn.tbluebox.springbootmybatis.domain

config-location: #指定 mybatis 核心配置文件

[](()(4)完善测试类


package cn.tbluebox.springbootmybatis;


import cn.tbluebox.springbootmybatis.domain.User;


import cn.tbluebox.springbootmybatis.mapper.UserMapper;


import cn.tbluebox.springbootmybatis.mapper.UserXmlMapper;


import org.junit.jupiter.api.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;

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Java之Spring Boot入门到精通【IDEA版】SpringBoot整合其他框架_Java_爱好编程进阶_InfoQ写作社区