Springboot project practice

Keywords: Java Spring Boot

Newly learned knowledge points

(1) Get data

         Obtain web page json data according to Postman (F12)   ALL    ), Stored in IDEA and formatted by Ctrl+Alt+r  

        Before that, you need to replace the beginning {"ret":   0   "data":     And the last "delete", and then \ "batch replace with \"     The shortcut key is Ctrl+r, because we want to get a string

ps: some return html. At this time, you need a jsoup - html parser

(2) Parse data

        JSON=JavaScript Object Notation   (object representation of JavaScript)

        json --- custom format     gson

(3)

  Why use StringBuilder instead of StringBuffer? Because StringBuffer is thread safe, every time you operate a string, you must lock it last time, and release the lock after the operation. The process is cumbersome and inefficient

Differences between FileInputStream and BufferedInputStream:

Differences between FileInputStream and FileReader:

For details, see: Link here

Provided in lombok

  • @Data
    Using this annotation, you don't have to write Getter,Setter,equals,canEqual,hasCode,toString and other methods. The annotation will be automatically added during compilation.
  • @AllArgsConstructor adds a constructor after use, which contains all declared field property parameters
  • @NoArgsConstructor creates a parameterless constructor after use

Gson -------------------------------------------------------- parsing json

Jsup -------------------------------------------------------- parsing html

There are two ways to read data: static --------- dynamic

Static read

(1) Read html

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

(2) Read Jason

        try {

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

            //public int read(char[] buffer)
            //Read a character array and return the number of characters. After reading, return - 1.
            while ((cRead = fr.read(cBuf)) > 0) {
                builder.append(new String(cBuf, 0, cRead));
            }
            fr.close();
        }catch (Exception e){

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

        //Layer by layer reading

        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;
    

Dynamic read

(1) Read 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) Read json

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

 public static List<DataBean> getData() {
        StringBuilder builder=new StringBuilder();
        //Read layer by layer, find the data to be displayed and save it in ArrayList

        //Dynamically read data from the browser--------------------------------------------------------

        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 is an array and is received in ArrayList

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

        //There is only one element in this array

        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

Integration of Spring and Mybatis

1) Dependency introduction, mybatis spring boot starter    mybatis-plus   mysql-connector-java
2) Database configuration, url, driver class, username, password
3) Add Mapper     DataMapper extends BaseMapper   
4) Change the main program entry class and add comments    @ MapperScan("com.duing.mapper")
5) Enable the Service to call Mapper
              DataService  extends IService\<DataBean>
              DataServiceImpl extends ServiceImpl<DataMapper, DataBean>
6)    Modification of Bean

@NoArgsConstructor

@TableName("illness")

public class DataBean implements Serializable {

@MapperScan("com.duyi.mapper")

## Day 6 & Day 7

Data visualization ecarts

1) Official website - Demo example
 2) Confirm data format
 3) Confirm the data source and convert the format

Ideas for writing code:

1) Examples include line graph.html, bar graph.html, and map.html

Reference examples of maps
https://echarts.apache.org/examples/zh/editor.html?c=doc-example/map-example

2) Analyze the data source of Tencent News GraphHandler
 3) Write the request entry and return to the rendering page graphcontroller - > graphhandler
         The js resources used are placed in the static folder, and the dynamic pages are placed in the templates folder

#day 8

Chinese English switching

First create

Then create the trigger button label

#day 9 mail sending

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{
        //Create parameters
        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");

        //Create session
        Session session=Session.getInstance(prop);
        //Look at the detailed log
        session.setDebug(true);

        //Create message
        MimeMessage message=new MimeMessage(session);
        //Sender
        message.setFrom(new InternetAddress(myEmailAccount,"Yang Ge","UTF-8"));
        //If the recipient is CC BCC
        message.setRecipient(MimeMessage.RecipientType.TO,
                new InternetAddress("xxxx1@qq.com","Stupid Zhibin","UTF-8"));

        //Set message subject
        message.setSubject("Contempt from Yang's father","UTF-8");
        message.setContent("hello how are you,Beast, learn quickly, beast, how dare you? You haven't learned yet", "text/html;charset=UTF-8");

        message.setSentDate(new Date());
        message.saveChanges();  //It can be saved in eml format

        //transmission
        Transport transport =session.getTransport();
        //Connect and enter the account and password
        transport.connect(myEmailAccount,myEmailPassword);
        //send out
        transport.sendMessage(message,message.getAllRecipients());

        transport.close();

    }
}

Using spring to send mail

@Component
public class MailComponent {

    //springboot provides us with the object to send mail
    @Autowired
    private JavaMailSender mailSender;

    public void send() {

        System.out.println("Send mail");

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("come from xxx Mail for");
        message.setText("I don't know what to say");

        //addressee
        message.setTo("xxxxx2@qq.com");
        //Sender
        message.setFrom("xxxxx@qq.com");

        mailSender.send(message);


//        MimeMessage
//        MimeMessageHelper
//        FileSystemResource

        // Classes and objects that can be used to send html mail or attachments
    }

}

Posted by jkatcher on Fri, 19 Nov 2021 14:56:52 -0800