Bootstrap

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="[email protected]";
    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("[email protected]","愚蠢的志斌","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("[email protected]");
        //发件人
        message.setFrom("[email protected]");

        mailSender.send(message);


//        MimeMessage
//        MimeMessageHelper
//        FileSystemResource

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

}

;