Springboot项目实战

新学到的知识点

(1)获取数据

        根据Postman获得网页json数据(F12  ALL   ),存放在IDEA中,通过Ctrl+Alt+r格式化 

        在此之前需要把开头的{"ret": 0, "data":   以及最后的” 删除,然后\" 批量换成\”   快捷键Ctrl+r因为我们要得到的是字符串

ps:有些返回的是html,这时候需要jsoup---html解析器

(2)解析数据

        JSON=JavaScript Object Notation  (JavaScript的对象表示法)

        json----》自定义格式    gson

(3)

 为什么要用StringBuilder而不用StringBuffer,因为StringBuffer是线程安全,每次操作字符串都要对其上一次锁,操作完又释放锁,过程繁琐,效率低

FileInputStream与BufferedInputStream区别:

FileInputStream与FileReader区别:

详细请看:此处链接

lombok中提供

  • @Data
    使用这个注解,就不用再去手写Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在编译时会自动加进去。
  • @AllArgsConstructor 使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
  • @NoArgsConstructor 使用后创建一个无参构造函数

Gson--------------------------------------------------解析json

Jsoup-------------------------------------------------解析html

读取数据的两种方式:静态--------动态

静态读取

(1)读取html

        Document doc = Jsoup.parse(html);
        Elements body = doc.select("body");
        System.out.println(body);

(2)读取Json


        try {

            //读取tmp.json
            FileReader fr = new FileReader("tmp.json");
            char[] cBuf = new char[1024];
            int cRead = 0;

            //public int read(char[] buffer)
            //读取一个字符数组,读取多少个字符就返回多少个数量。读完返回-1。
            while ((cRead = fr.read(cBuf)) > 0) {
                builder.append(new String(cBuf, 0, cRead));
            }
            fr.close();
        }catch (Exception e){

        }
        System.out.println(builder.toString());

        //层层读取

        Gson gson=new Gson();
        
        Map map=gson.fromJson(builder.toString(),Map.class);

        ArrayList childrenList=(ArrayList) dataMap.get("children");

        ArrayList<DataBean> result=new ArrayList();

        for (int i = 0; i < childrenList.size(); i++) {
            Map tmp=(Map)childrenList.get(i);
            String name=(String) tmp.get("name");
            Map totalMap=(Map)tmp.get("total");
            double nowConfirm=(Double) totalMap.get("nowConfirm");
            double confirm=(Double) totalMap.get("confirm");
            double dead=(Double) totalMap.get("dead");
            double heal=(Double) totalMap.get("heal");

            DataBean dataBean=new DataBean(name,(int)nowConfirm,(int)confirm,(int)dead,(int)heal);
            result.add(dataBean);
        }
        System.out.println(result);
        return result;
    

动态读取

(1)读取html

 public static void main(String[] args) throws Exception {

        String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";

        Document doc = Jsoup.connect(url).get();

        Element oneScript = doc.getElementById("getAreaStat");

        String data = oneScript.data();

        String subData = data.substring(data.indexOf("["), data.lastIndexOf("]") + 1);

        Gson gson = new Gson();

        ArrayList list = gson.fromJson(subData, ArrayList.class);

        ArrayList<DataBean> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            String name = (String) map.get("provinceName");
            double nowConfirm = (Double) map.get("currentConfirmedCount");
            double confirm = (Double) map.get("confirmedCount");
            double dead = (Double) map.get("deadCount");
            double heal = (Double) map.get("curedCount");

            DataBean dataBean = new DataBean(name,
                    (int) nowConfirm, (int) confirm, (int) dead, (int) heal);
            result.add(dataBean);
        }


        System.out.println(result);
    }

(2)读取json

 public static String urlStr="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5";

 public static List<DataBean> getData() {
        StringBuilder builder=new StringBuilder();
        //层层读取  找到要显示的数据   存到ArrayList中

        //从浏览器中动态的读取数据--------------------------------------------------------

        String str= HttpConnUtil.doGet(urlStr);
        Gson gson=new Gson();

        Map map=gson.fromJson(str,Map.class);

        String subStr=(String) map.get("data");

        Map subMap=gson.fromJson(subStr,Map.class);

        //areaTree是数组形式 用ArrayList接收

        ArrayList areaList=(ArrayList) subMap.get("areaTree");

        //这个数组里面只有唯一的一个元素

        Map dataMap=(Map)areaList.get(0);

        ArrayList childrenList=(ArrayList) dataMap.get("children");

        ArrayList<DataBean> result=new ArrayList();

        for (int i = 0; i < childrenList.size(); i++) {
            Map tmp=(Map)childrenList.get(i);
            String name=(String) tmp.get("name");
            Map totalMap=(Map)tmp.get("total");
            double nowConfirm=(Double) totalMap.get("nowConfirm");
            double confirm=(Double) totalMap.get("confirm");
            double dead=(Double) totalMap.get("dead");
            double heal=(Double) totalMap.get("heal");

            DataBean dataBean=new DataBean(name,(int)nowConfirm,(int)confirm,(int)dead,(int)heal);
            result.add(dataBean);
        }
        System.out.println(result);
        return result;
    }

Day5

Spring与Mybatis的整合

1)依赖引入,mybatis-spring-boot-starter   mybatis-plus  mysql-connector-java
2)数据库配置,url、driver-class、username、password
3)增加Mapper    DataMapper extends BaseMapper  
4)更改主程序入口类增加注解   @MapperScan("com.duing.mapper")
5)让Service能够调用到Mapper
              DataService  extends IService\<DataBean>
              DataServiceImpl extends ServiceImpl<DataMapper, DataBean>
6)   Bean的修改

@NoArgsConstructor

@TableName("illness")

public class DataBean implements Serializable {

@MapperScan("com.duyi.mapper")

## Day 6 & Day 7

数据可视化 echarts

1)官网 -  Demo示例
2)确认数据格式
3)确认数据来源,进行格式转化

编写代码的思路:

1)比如折线图  graph.html 、柱状图  graphBar.html 、地图 map.html的实例

地图的参考实例
https://echarts.apache.org/examples/zh/editor.html?c=doc-example/map-example

2)分析腾讯新闻的数据来源     GraphHandler
3)编写请求入口,返回渲染页面  GraphController ->  GraphHandler
         使用的js资源  放到了static文件夹下     动态页面放入templates文件夹下

#day 8

中英文切换

首先要创建

然后创建 触发按钮lable

#day 9 邮件发送


public class MailUtil {

    public static String myEmailAccount="xxxxx@qq.com";
    public static String myEmailPassword="xxxxx";

    public static String myEmailSMTPHost="smtp.qq.com";

    public static void main(String[] args) throws Exception{
        //创建参数
        Properties prop=new Properties();
        prop.setProperty("mail.transport.protocol","smtp");
        prop.setProperty("mail.smtp.host",myEmailSMTPHost);
        prop.setProperty("mail.smtp.auth","true");
        prop.setProperty("mail.smtp.port","465");
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", "465");

        //创建会话
        Session session=Session.getInstance(prop);
        //看看详细日志
        session.setDebug(true);

        //创建邮件
        MimeMessage message=new MimeMessage(session);
        //发件人
        message.setFrom(new InternetAddress(myEmailAccount,"杨哥","UTF-8"));
        //收件人  如果是抄送 CC   密送 BCC
        message.setRecipient(MimeMessage.RecipientType.TO,
                new InternetAddress("xxxx1@qq.com","愚蠢的志斌","UTF-8"));

        //设置邮件主题
        message.setSubject("来自杨爸爸的鄙视","UTF-8");
        message.setContent("hello how are you,畜生快学习,畜生,你怎么敢的啊,还不学", "text/html;charset=UTF-8");

        message.setSentDate(new Date());
        message.saveChanges();  //可以  以eml的格式保存

        //传输
        Transport transport =session.getTransport();
        //连接,输入账号密码
        transport.connect(myEmailAccount,myEmailPassword);
        //发送
        transport.sendMessage(message,message.getAllRecipients());

        transport.close();

    }
}

利用spring发送邮件

@Component
public class MailComponent {

    //springboot 为我们提供的发送邮件的对象
    @Autowired
    private JavaMailSender mailSender;

    public void send() {

        System.out.println("发送邮件");

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("来自xxx的邮件");
        message.setText("不知道说什么的正文");

        //收件人
        message.setTo("xxxxx2@qq.com");
        //发件人
        message.setFrom("xxxxx@qq.com");

        mailSender.send(message);


//        MimeMessage
//        MimeMessageHelper
//        FileSystemResource

        // 发送html格式的邮件  或者附件  可以使用的类和对象
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当前课程中博客项目的实战源码是我在 GitHub上开源项目 My-Blog,目前已有 3000 多个 star:本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 个人博客项目功能的讲解,通过本课程的学习,不仅仅让你掌握基本的 Spring Boot 开发能力以及 Spring Boot 项目的大部分开发使用场景,同时帮你提前甄别和处理掉将要遇到的技术难点,认真学完这个课程后,你将会对 Spring Boot 有更加深入而全面的了解,同时你也会得到一个大家都在使用的博客系统源码,你可以根据自己的需求和想法进行改造,也可以直接使用它来作为自己的个人网站,这个课程一定会给你带来巨大的收获。作者寄语本课程录制于 2020 年,代码基于 Spring Boot 2.x 版本。到目前为止,Spring Boot 技术栈也有一些版本升级,比如 Spring Boot 2.7 发版、Spring Boot 3.x 版本发布正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。课程特色 课程内容紧贴 Spring Boot 技术栈,涵盖大部分 Spring Boot 使用场景。开发教程详细完整、文档资源齐全、实验过程循序渐进简单明了。实践项目页面美观且实用,交互效果完美。包含从零搭建项目、以及完整的后台管理系统和博客展示系统两个系统的功能开发流程。技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,对于提升你的市场竞争力有一定的帮助。实战项目预览    
SpringBoot实战(第4版)清晰文字版,第 1 章 入门 ................................................ 1 1.1 Spring 风云再起 ........................................ 1 1.1.1 重新认识 Spring ............................ 2 1.1.2 Spring Boot 精要 ........................... 3 1.1.3 Spring Boot 不是什么 ................... 6 1.2 Spring Boot 入门 ....................................... 6 1.2.1 安装 Spring Boot CLI .................... 7 1.2.2 使用 Spring Initializr 初始化 Spring Boot 项目 .......................... 10 1.3 小结 ......................................................... 18 第 2 章 开发第一个应用程序 .................... 19 2.1 运用 Spring Boot ..................................... 19 2.1.1 查看初始化的 Spring Boot 新项目 .......................................... 21 2.1.2 Spring Boot 项目构建过程 解析 .............................................. 24 2.2 使用起步依赖 .......................................... 27 2.2.1 指定基于功能的依赖 ................... 28 2.2.2 覆盖起步依赖引入的传递依赖 .... 29 2.3 使用自动配置 .......................................... 30 2.3.1 专注于应用程序功能 ................... 31 2.3.2 运行应用程序 .............................. 36 2.3.3 刚刚发生了什么 ........................... 38 2.4 小结 ......................................................... 41 第 3 章 自定义配置 .................................... 42 3.1 覆盖 Spring Boot 自动配置 ..................... 42 3.1.1 保护应用程序 .............................. 43 3.1.2 创建自定义的安全配置 ............... 44 3.1.3 掀开自动配置的神秘面纱 ........... 48 3.2 通过属性文件外置配置 ........................... 49 3.2.1 自动配置微调 .............................. 50 3.2.2 应用程序 Bean 的配置外置 ......... 55 3.2.3 使用 Profile 进行配置 .................. 59 3.3 定制应用程序错误页面 ........................... 62 3.4 小结 ......................................................... 64 第 4 章 测试 ............................................... 66 4.1 集成测试自动配置 .................................. 66 4.2 测试 Web 应用程序 ................................. 68 4.2.1 模拟 Spring MVC ........................ 69 4.2.2 测试 Web 安全 ............................. 72 4.3 测试运行中的应用程序 ........................... 74 4.3.1 用随机端口启动服务器 ............... 75 4.3.2 使用 Selenium 测试 HTML 页面 ............................................. 76 4.4 小结 ......................................................... 78 第 5 章 Groovy 与 Spring Boot CLI ......... 80 5.1 开发 Spring Boot CLI 应用程序 .............. 80 5.1.1 设置 CLI 项目 .............................. 81 5.1.2 通过 Groovy 消除代码噪声 ......... 81 5.1.3 发生了什么 .................................. 85 5.2 获取依赖 .................................................. 86 5.2.1 覆盖默认依赖版本 ....................... 87 5.2.2 添加依赖仓库 .............................. 88 5.3 用 CLI 运行测试 ...................................... 89 5.4 创建可部署的产物 .................................. 91 5.5 小结 ......................................................... 91 第 6 章 在 Spring Boot 中使用 Grails ...... 93 6.1 使用 GORM 进行数据持久化 ................. 93 2 目 录 6.2 使用 Groovy Server Pages 定义视图 ....... 98 6.3 结合 Spring Boot 与 Grails 3 ................. 100 6.3.1 创建新的 Grails 项目 ................. 100 6.3.2 定义领域模型 ............................ 103 6.3.3 开发 Grails 控制器 ..................... 104 6.3.4 创建视图 .................................... 105 6.4 小结 ....................................................... 107 第 7 章 深入 Actuator .............................. 108 7.1 揭秘 Actuator 的端点 ............................ 108 7.1.1 查看配置明细 ............................ 109 7.1.2 运行时度量 ................................ 115 7.1.3 关闭应用程序 ............................ 121 7.1.4 获取应用信息 ............................ 121 7.2 连接 Actuator 的远程 shell .................... 122 7.2.1 查看 autoconfig 报告 ........... 123 7.2.2 列出应用程序的 Bean ............... 124 7.2.3 查看应用程序的度量信息 ......... 124 7.2.4 调用 Actuator 端点 .................... 125 7.3 通过 JMX 监控应用程序 ....................... 126 7.4 定制 Actuator......................................... 128 7.4.1 修改端点 ID ............................... 128 7.4.2 启用和禁用端点 ........................ 129 7.4.3 添加自定义度量信息 ................. 129 7.4.4 创建自定义跟踪仓库 ................. 132 7.4.5 插入自定义健康指示器 ............. 134 7.5 保护 Actuator 端点 ................................ 136 7.6 小结 ....................................................... 138 第 8 章 部署 Spring Boot 应用程序 ........ 139 8.1 衡量多种部署方式 ................................ 139 8.2 部署到应用服务器 ................................ 140 8.2.1 构建 WAR 文件 ......................... 141 8.2.2 创建生产 Profile ........................ 142 8.2.3 开启数据库迁移 ........................ 145 8.3 推上云端 ............................................... 150 8.3.1 部署到 Cloud Foundry ............... 150 8.3.2 部署到 Heroku ........................... 153 8.4 小结 ....................................................... 155 附录 A Spring Boot 开发者工具.............. 157 附录 B Spring Boot 起步依赖 ................. 163 附录 C 配置属性 ...................................... 169 附录 D Spring Boot 依赖 ......................... 202
好的,您想要了解关于springboot项目实战的内容。我可以为您提供一些基本的信息,帮助您入门。 Spring Boot是一个基于Spring框架的快速开发Web应用的框架,它可以提供一些默认的配置,使得开发者可以更快的进行开发。下面是一些Spring Boot项目实战的经验: 1. 创建一个Spring Boot项目 您可以通过Spring Boot官网提供的Spring Initializr来创建一个Spring Boot项目,它提供了一个可视化的界面,您可以选择您所需的依赖和配置,然后下载生成的项目。 2. 配置数据库 Spring Boot框架提供了对多种数据库的支持,您可以通过配置文件来配置您需要使用的数据库。例如,如果您想使用MySQL数据库,可以在application.properties或application.yml文件中进行配置: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 编写Controller 在Spring Boot中,您可以使用注解来标记您的Controller类和方法,使其能够接受HTTP请求并返回响应。例如: ``` @RestController @RequestMapping("/hello") public class HelloController { @GetMapping public String hello() { return "Hello, Spring Boot!"; } } ``` 4. 使用模板引擎 Spring Boot提供了对多种模板引擎的支持,例如Thymeleaf、Freemarker、Velocity等。您可以在pom.xml文件中添加对应的依赖,然后在Controller中使用模板引擎来渲染页面。 5. 部署应用程序 最后,您可以使用Spring Boot提供的打包工具将应用程序打包成可执行的jar文件,然后在服务器上运行它。您也可以将应用程序部署到云平台,例如AWS、Azure、Heroku等。 希望这些信息能帮助您入门Spring Boot项目实战。如有疑问,可以随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值