当前位置: 首页 > news >正文

做动图网站搜什么关键词能搜到好片

做动图网站,搜什么关键词能搜到好片,网站建设系统公司,做网站工资怎么样你能学到什么 表白墙(升级版)Mybatis的一些简单应用 正文 前⾯的案例中, 我们写了表⽩墙, 但是⼀旦服务器重启, 数据就会丢失. 要想数据不丢失, 需要把数据存储在数据库中,接下来咱们借助MyBatis来实现数据库的操作。 数据准备 如果我们…

你能学到什么

  • 表白墙(升级版)
  • Mybatis的一些简单应用

正文

前⾯的案例中, 我们写了表⽩墙, 但是⼀旦服务器重启, 数据就会丢失.
要想数据不丢失, 需要把数据存储在数据库中,接下来咱们借助MyBatis来实现数据库的操作。

数据准备

如果我们想将数据存起来首先要有一个数据库吧,所以首先就是要创建一个数据库 ,由于直接在Mysql 小黑框里写有点麻烦,我们可以借助一些软件来更加简单的创建数据库,我使用的是Navicat Premium17,挺好用的,虽然它收费,但是搜搜教程,免费的就来了,懂我的意思吧。
在这里插入图片描述
最好使用Mysql 8 ,因为 Mysql5 好像和新的一些功能不兼容了。
这是创建一个message_info表的代码,有兴趣的可以跟着敲一下,就当复习了,没兴趣的直接复制就行。

DROP TABLE IF EXISTS message_info;
CREATE TABLE `message_info` (
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
`from` VARCHAR ( 127 ) NOT NULL,
`to` VARCHAR ( 127 ) NOT NULL,
`message` VARCHAR ( 256 ) NOT NULL,
`delete_flag` TINYINT ( 4 ) DEFAULT 0 COMMENT '0-正常, 1-删除',
`create_time` DATETIME DEFAULT now(),
`update_time` DATETIME DEFAULT now() ON UPDATE now(),
PRIMARY KEY ( `id` )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;

在这里插入图片描述

引入依赖

引⼊MyBatis 和 MySQL驱动依赖

方法一:修改pom文件,然后刷新Maven,就能将依赖加进来

在这里插入图片描述

在这里插入图片描述

  • 下面是用到的依赖,有需要的可以复制
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
方法二:使⽤插件EditStarters来引入依赖
  • 1,先打开settings 然后在Plugins(插件)里面搜EditStarters,点击install就行了,然后重启IDEA就能用了
    在这里插入图片描述
  • 2,在pom文件页面右键generate,然后按照以下的简图进行

在这里插入图片描述

方法三:在创建项目的时候就将依赖加进来

在这里插入图片描述

以上的方法任选其一就行。
对于方法三:吃一堑长一智,下次我们在创建项目的时候直接添加进来就行了,就不用后期再次添加了。

配置Mysql账号密码

这个操作需要在applicantion.properties文件下配置,由于这里我只提供了yml格式的配置信息,大家可以将这个文件重命名改成applicantion.yml,照着下面的流程图改就行了。

在这里插入图片描述
将相应的配置信息添加进来

在这里插入图片描述

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/blogs_text?characterEncoding=utf8&useSSL=falseusername: rootpassword: 456123driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:configuration: # 配置打印 MyBatis⽇志log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true #配置驼峰⾃动转换

编写后端代码

实体类info

在这里插入图片描述
我们可以看到数据库里有很多属性,如果只靠我们之前定义的类中的属性肯定是不够的,所以我们要重新定义一个实体类和数据库里的属性对应。
在这里插入图片描述
实体类代码

package com.example.leavemessage_blogs.model;
import lombok.Data;
import java.util.Date;
@Data
public class Info {private Integer id;private String from;private String to;private String message;private Integer deleteFlag;private Date createTime;private Date updateTime;
}

使用Mybatis 操作数据库

  • 说到这里我们就需要想一想接口文档了,我们的需求是什么,要用增删改查的哪一个。我们知道表白墙的两大功能就是
    • 1,显示数据
    • 2,添加信息
      由此我们可以回忆一下之前我们的初级表白墙怎么做的,我们是用一个list来储存数据,如果要显示数据就将list返回给前端。而此刻,我们不再用list储存数据了,而是用数据库,所以我们要提供的功能就是
    • 1,查找数据(select)返回数据给前端页面显示
    • 2,新增数据(insert)将用户输入的数据存到数据库中

InfoMapper接口的代码(由于我们不需要方法有具体的实现,所以定义成接口更合适)

我们先完成一个功能,添加

package com.example.leavemessage_blogs.Mapper;import com.example.leavemessage_blogs.model.Info;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;//使用Mybatis操作数据库,别忘记加Mapper注解
@Mapper
public interface InfoMapper {//我们采用传对象的方式,由于前端只提交这几个值,所以我们只需要将这几个值传入数据库就行了,其他的默认值就行@Insert("insert into message_info(from,to,message) values (#{from},#{to},#{message})")public boolean addInfo(Info info);}

编写并测试新增addInfo功能

上面我们已经写好了,可以测试一下我们写的到底对不对:
右键——》generate——》text——》勾选你要测试的方法(其他不用动)。

然后加上@SpringBootText 测试注解,编写测试代码。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

因此我们将sql语句加上反引号

在这里插入图片描述

测试结果:成功

在这里插入图片描述

新增的测试代码:

package com.example.leavemessage_blogs.Mapper;import com.example.leavemessage_blogs.model.Info;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class InfoMapperTest {@AutowiredInfoMapper infoMapper;@Testvoid addInfo() {Info info = new Info();info.setFrom("wulanzheng");info.setTo("chengaiying");info.setMessage("I love you");}
}

编写并测试查询querryAllInfo功能

查询功能的代码:

package com.example.leavemessage_blogs.Mapper;import com.example.leavemessage_blogs.model.Info;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;//别忘记加Mapper注解
@Mapper
public interface InfoMapper {//我们采用传对象的方式,由于前端只提交这几个值,所以我们只需要将这几个值传入数据库就行了,其他的默认值就行
//    @Insert("insert into message_info(`from`,`to`,message) values (#{from},#{to},#{message})")
//    public boolean addInfo(Info info);@Select("select * from Message_info")public List<Info> querryAllInfo();}

测试:

注意如果你在:右键——》generate——》text——》勾选你要测试的方法。
的过程中出现了下面的Error 不要慌,直接点击ok就行了。
在这里插入图片描述
我们能能看到代码运行成功,还打印出了结果
可以看到运行橙红

查询的测试代码:

package com.example.leavemessage_blogs.Mapper;import com.example.leavemessage_blogs.model.Info;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class InfoMapperTest {@AutowiredInfoMapper infoMapper;//    @Test
//    void addInfo() {
//        Info info = new Info();
//        info.setFrom("wulanzheng");
//        info.setTo("chengaiying");
//        info.setMessage("I love you");
//        infoMapper.addInfo(info);
//    }@Test
void querryAllInfo() {//将查询的数据遍历打印下来,这是一种lamda表达式的写法infoMapper.querryAllInfo().forEach(System.out::println);
}
}

这样我们的后端主要的架构就写完了,但是由于应用分层的理念,我们还要写service方法,和controller进行交互

编写service代码(应用分层交互)

package com.example.leavemessage_blogs.Service;import com.example.leavemessage_blogs.Mapper.InfoMapper;
import com.example.leavemessage_blogs.model.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class InfoService {@AutowiredInfoMapper infoMapper;public List<Info> getInfo(){return infoMapper.querryAllInfo();}public boolean addInfo(Info info){boolean ret =infoMapper.addInfo(info);if(ret) {return true;}return false;}
}

修改controller 代码

我们已经将数据库引入了,自然就不需要list来存储数据了,所以controller中的代码也要修改,
修改后的controller代码:

package com.example.leavemessage_blogs.Controller;import com.example.leavemessage_blogs.Service.InfoService;
import com.example.leavemessage_blogs.model.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RequestMapping("/message")
@RestController
public class MessageController {@AutowiredInfoService infoService;@RequestMapping("getList_Mysql")public List<Info> getList(){return infoService.getInfo();}@RequestMapping("/addInfo")public boolean addInfo(Info info){System.out.println(info);if(StringUtils.hasLength(info.getFrom())&&StringUtils.hasLength(info.getTo())&&StringUtils.hasLength(info.getMessage())){infoService.addInfo(info);return true;}return false;}
}

测试后端代码

此时我们就已经将后端代码全部修改完毕了,就是测试了,我们使用postman来测试。

测试新增的功能

可以看到发送成功

在这里插入图片描述
我们再看看数据库:

在这里插入图片描述
没有任何问题

测试查询的功能

也是没有任何问题
在这里插入图片描述

  • 小tips:对于测试单一接口:先将controller 类和service类Mapper类都先写了,然后再经过Postman访问controller的url,从而达到测试的目的,这也是一种测试的方法。但是相比较于我们上面的直接generate自动生成的方法,自动生成的方法更加的方便,高效,所以更推荐在测试一个接口的时候使用自动生成的方法。

编写前端代码

确定了后端代码全部正确以后,此时我们再次点开messagewall.html文件对前端代码进行修改
看来看去,好想只有load里的url需要修改一下

 load();function load() {$.ajax({type: "get",url: "/message/getList_Mysql",//修改这里的url,改成我们新的urlsuccess: function (result) {for (var message of result) {var divE = "<div>" + message.from + "对" + message.to + "说:" + message.message + "</div>";$(".container").append(divE);}}});}

测试前端代码

访问http://127.0.0.1:8080/messagewall.html之后,会出现我们之前添加过的数据,这表示查询功能正常
在这里插入图片描述
我们此时再添加一个数据,可以看到功能正常
在这里插入图片描述
以上的表白墙无论你怎么重复启动程序都不会丢失数据了,这就是表白墙进阶全部内容了,如果有什么问题,欢迎留言,我们共同探讨。


文章转载自:
http://pessimistically.c7513.cn
http://monetization.c7513.cn
http://apocalyptical.c7513.cn
http://bascule.c7513.cn
http://villeinage.c7513.cn
http://contribute.c7513.cn
http://abattis.c7513.cn
http://fian.c7513.cn
http://phalanx.c7513.cn
http://genocidist.c7513.cn
http://akathisia.c7513.cn
http://leeboard.c7513.cn
http://testudinal.c7513.cn
http://immobilism.c7513.cn
http://cervantite.c7513.cn
http://resegregate.c7513.cn
http://spatterware.c7513.cn
http://caledonian.c7513.cn
http://kalium.c7513.cn
http://exemplariness.c7513.cn
http://geognosy.c7513.cn
http://navel.c7513.cn
http://kalmia.c7513.cn
http://gelatine.c7513.cn
http://tailforemost.c7513.cn
http://domiciliation.c7513.cn
http://protanopia.c7513.cn
http://bouffe.c7513.cn
http://egyptianization.c7513.cn
http://rev.c7513.cn
http://dogfish.c7513.cn
http://redevelop.c7513.cn
http://unreversed.c7513.cn
http://inbox.c7513.cn
http://semidet.c7513.cn
http://vogue.c7513.cn
http://areologist.c7513.cn
http://dissilient.c7513.cn
http://leptosome.c7513.cn
http://deprecatingly.c7513.cn
http://maximality.c7513.cn
http://batrachoid.c7513.cn
http://patience.c7513.cn
http://poco.c7513.cn
http://fibrination.c7513.cn
http://stamina.c7513.cn
http://bedsettee.c7513.cn
http://teleshopping.c7513.cn
http://balkan.c7513.cn
http://ambagious.c7513.cn
http://chowry.c7513.cn
http://geologician.c7513.cn
http://myxasthenia.c7513.cn
http://nicy.c7513.cn
http://riff.c7513.cn
http://armamentarium.c7513.cn
http://orobanchaceous.c7513.cn
http://nonentanglement.c7513.cn
http://antiphrasis.c7513.cn
http://holidic.c7513.cn
http://canaled.c7513.cn
http://didymous.c7513.cn
http://glassboro.c7513.cn
http://reinsert.c7513.cn
http://forniciform.c7513.cn
http://cassimere.c7513.cn
http://lavishment.c7513.cn
http://cappuccino.c7513.cn
http://tubuliflorous.c7513.cn
http://adhere.c7513.cn
http://latakia.c7513.cn
http://augustinianism.c7513.cn
http://leucocratic.c7513.cn
http://abjectly.c7513.cn
http://misdescription.c7513.cn
http://coracoid.c7513.cn
http://burgh.c7513.cn
http://med.c7513.cn
http://mesothorium.c7513.cn
http://equilibrator.c7513.cn
http://primigravida.c7513.cn
http://castock.c7513.cn
http://riverly.c7513.cn
http://dictatorially.c7513.cn
http://fistula.c7513.cn
http://assignee.c7513.cn
http://camphoraceous.c7513.cn
http://frenchify.c7513.cn
http://metrician.c7513.cn
http://quizee.c7513.cn
http://foretype.c7513.cn
http://filler.c7513.cn
http://somnolency.c7513.cn
http://sundsvall.c7513.cn
http://aerophore.c7513.cn
http://countrified.c7513.cn
http://cadent.c7513.cn
http://linalool.c7513.cn
http://asthenosphere.c7513.cn
http://plainchant.c7513.cn
http://www.zhongyajixie.com/news/91500.html

相关文章:

  • 广告传媒网站模板网站标题优化排名
  • 教做潮男的网站宁波seo网络推广主要作用
  • 做网站怎么查看来访ip怎么做优化关键词
  • pc网站同步手机网站seo课程哪个好
  • 做货到付款的购物网站网络销售
  • 做网站平台的注册什么商标重庆seo是什么
  • 传统网站建设 成本市场运营和市场营销的区别
  • 网站logo一般做多大佛山网站快速排名提升
  • 禾天姿网站开发扬州网络优化推广
  • 用wordpress建一个网站吗湖人最新消息
  • 你在四川省建设安全与质量监督网站模板网站建站公司
  • 常德营销型网站建设端口扫描站长工具
  • 株洲新站建设抖音关键词优化排名靠前
  • 静态网站开发预期效果谷歌浏览器下载手机版最新版
  • 宿州信息网招聘优化网站关键词排名软件
  • 猎头可以做单的网站新闻发稿平台有哪些?
  • wordpress分类目录描述长沙网站优化公司
  • 网页入口网站推广优化大师绿色版
  • 网站页尾内容桂林seo顾问
  • 自己做网站推广关键词网站流量分析工具
  • 求个没封的w站2021你懂北京最新消息今天
  • 网站建设模拟软件搜索引擎bing
  • 北京网站建设手机app微信营销策略
  • 有没有专业做二手老车的网站商丘seo博客
  • 自己动手建立个人网站互联网全网营销
  • 网站模版的优化热点事件营销案例
  • 专业做二手网站有哪些seo 工具
  • 465端口 WordPressseo云优化平台
  • 进行网站开发 如何搭建环境凡客建站
  • 美食网站网页设计代码网络培训班