技术编程|SpringBoot怎么整合MongoDB?( 三 )


文章图片

文章图片

MongoDB做缓存小Demo数据库
懒得新建项目也可以拿刚才的直接测哈 , 在数据里找了一圈发现有一个叫blog的比较合适 , 先看看数据表长什么样
技术编程|SpringBoot怎么整合MongoDB?
文章图片

文章图片

建表语句CREATETABLE`blog`(`id`varchar(50)NOTNULLCOMMENT'博客id',`title`varchar(100)NOTNULLCOMMENT'博客标题',`author`varchar(30)NOTNULLCOMMENT'博客作者',`create_time`datetimeNOTNULLCOMMENT'创建时间',`views`int(30)NOTNULLCOMMENT'浏览量')ENGINE=InnoDBDEFAULTCHARSET=utf8
在SpringBoot项目中加入mysql依赖
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
在配置文件application.properties中加入mysql配置spring.datasource.username=root#自己的mysql数据库密码spring.datasource.password=*****#自己把上面的表建立在哪个库中 , 就连哪个库spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
创建实体类Blog.javapackagecom.feng.pojo;importjava.util.Date;/***
spirngboot-mongodb-test
***@author:Nicer_feng*@date:2020-10-1210:10**/publicclassBlog{privateIntegerid;privateStringtitle;privateStringauthor;privateDatecreate_time;privateIntegerviews;publicBlog(){}publicBlog(Integerid,Stringtitle,Stringauthor,Datecreate_time,Integerviews){this.id=id;this.title=title;this.author=author;this.create_time=create_time;this.views=views;}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(Stringauthor){this.author=author;}publicDategetCreate_time(){returncreate_time;}publicvoidsetCreate_time(Datecreate_time){this.create_time=create_time;}publicIntegergetViews(){returnviews;}publicvoidsetViews(Integerviews){this.views=views;}@OverridepublicStringtoString(){return"Blog{"+"id="+id+",title='"+title+'\''+",author='"+author+'\''+",create_time="+create_time+",views="+views+'}';}}测试
直接去SpringBoot测试类 , 我们什么都先用模板类实现 , 这里面我们先测试一个查询 , 代码逻辑就是先从缓存(MongoDB)中查 , 如果缓存中有 , 直接返回 , 如果缓存中没有 , 我们去关系型数据库(MySQL)中查询 , 并将查询的结果存入缓存中 , 下次再查询直接返回结果 。packagecom.feng;importcom.feng.dao.UserDao;importcom.feng.pojo.Blog;importcom.feng.pojo.User;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.data.mongodb.core.MongoTemplate;importorg.springframework.data.mongodb.core.query.Criteria;importorg.springframework.data.mongodb.core.query.Query;importorg.springframework.jdbc.core.JdbcTemplate;importjava.sql.SQLException;importjava.util.Date;importjava.util.List;importjava.util.Map;importjava.util.Set;@SpringBootTestclassSpirngbootMongodbTestApplicationTests{//********@AutowiredMongoTemplatemongoTemplate;@AutowiredJdbcTemplatejdbcTemplate;@TestvoidgetBlogByTitle()throwsSQLException{Stringtitle="Java如此简单2";Queryquery=newQuery(Criteria.where("title").is(title));Blogone=mongoTemplate.findOne(query,Blog.class);if(one!=null){System.out.println("从缓存数据库直接拿的数据:"+one);}else{Stringsql="SELECT*FROMmybatis.blogWHEREblog.title=?";List>maps=jdbcTemplate.queryForList(sql,"Java如此简单2");System.out.println("从mysql数据库拿的数据:"+maps.toString());Map


推荐阅读