zoukankan      html  css  js  c++  java
  • Springboot 入门笔记

    springbootdemo
    HelloWorldController
    package com.controller;
    
    import com.entity.Student;
    import com.service.StudentService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    
    @RestController
    public class HelloWorldController {
        @Autowired
        ApplicationContext context;     //spring ioc容器
        @Autowired
        Student student;
    
        Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
    
        @ResponseBody
        @RequestMapping("/welcome")
        public String welcome() {
            StudentService stuService = (StudentService) context.getBean("stuService");
            // Student student = new Student();
            logger.trace("trace********");
            logger.debug("debug********");
            logger.info("info*******");
            logger.warn("warn******");
            logger.error("error****");
    
            return "spring boot " + new Date() + " 8888== " + student + "";
        }
    }
    ThymeleafController
    package com.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class ThymeleafController {
        @RequestMapping(value = "show", method = RequestMethod.GET)
        public String show(Model model){
            model.addAttribute("uid","123456789-------");
            model.addAttribute("name","Jerry2222222");
            return "show";
        }
    }
    AppConfig
    package com.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.service.StudentService;
    
    //配置类
    @Configuration
    public class AppConfig {
        
        @Bean        
        public StudentService stuService(){//<bean  id="xxxxxxxxxxxxx">
            StudentService stuService = new StudentService();
            
    //        StudentDao stuDao = new StudentDao()  ;
    //        stuService.setStudentDao(stuDao);
            return stuService;//返回值 <bean  class="xxxxxxxxxxxxx">
        }
    }
    Student
    package com.entity;
    
    import java.util.Arrays;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    import javax.validation.Valid;
    import javax.validation.constraints.Email;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.annotation.Validated;
    
    @Component //将此Javabean
    
    //@Validated//开启jsr303数据校验的注解
    @ConfigurationProperties(prefix = "student")
    @PropertySource(value={"classpath:conf.yml"})
    public class Student {
        // @Value("safasd")
        //@Email
        private String email;
        private String name;
    
        // @Value("${student.uname}")
        private String userName;
        // @Value("33")
        private int age;
        private boolean sex; //true:男  false:女
        private Date birthday;
        //{province:陕西, city:西安,zone:莲湖区}
        //@Value("{province:陕44西, city:西44安,zone:莲湖区}")
        private Map<String, Object> location;
        private String[] hobbies;
        private List<String> skills;
        private Pet pet;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public boolean isSex() {
            return sex;
        }
    
        public void setSex(boolean sex) {
            this.sex = sex;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public Map<String, Object> getLocation() {
            return location;
        }
    
        public void setLocation(Map<String, Object> location) {
            this.location = location;
        }
    
        public String[] getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(String[] hobbies) {
            this.hobbies = hobbies;
        }
    
        public List<String> getSkills() {
            return skills;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public void setSkills(List<String> skills) {
            this.skills = skills;
        }
    
        public Pet getPet() {
            return pet;
        }
    
        public void setPet(Pet pet) {
            this.pet = pet;
        }
    
        @Override
        public String toString() {
            return "Student [email=" + email + ", name=" + name + ", age=" + age + ", sex=" + sex + ", birthday="
                    + birthday + ", location=" + location + ", hobbies=" + Arrays.toString(hobbies) + ", skills=" + skills
                    + ", pet=" + pet + "]";
        }
    }
    Pet
    package com.entity;
    
    public class Pet {
        private String nickName ;//松散写法nick-name
        private String strain ;
        public String getNickName() {
            return nickName;
        }
        public void setNickName(String nickName) {
            this.nickName = nickName;
        }
        public String getStrain() {
            return strain;
        }
        public void setStrain(String strain) {
            this.strain = strain;
        }
        @Override
        public String toString() {
            return "Pet [nickName=" + nickName + ", strain=" + strain + "]";
        }
    }

    static/h1.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>static</title>
    </head>
    <body>
        weclome static
    </body>
    </html>

    templates/show.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>SpringBoot模版渲染</title>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    </head>
    <body>
    <p th:text="'用户ID:' + ${uid}"/>
    <p th:text="'用户名称:' + ${name}"/>
    </body>
    </html>

    application.properties

    server.port=30000
    logging.file.path=log
    logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
    logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} %msg%n

    application.yml  |  conf.yml

    #server:
      #port: 30001
    #=======================================
    student:
      name: zs
      age: 23
      sex: true
      birthday: 2019/02/12
  • 相关阅读:
    Java流程控制语句
    Linux文件过滤及内容编辑处理
    Java运算符优先级
    Java位运算基础知识
    【Linux】【FastDFS】FastDFS安装
    【Linux】【redis】redis安装及开启远程访问
    【Linux】【sonarqube】安装sonarqube7.9
    【Linux】【PostgreSQL】PostgreSQL安装
    【Linux】【maven】maven及maven私服安装
    【Linux】【jenkins】自动化运维七 整合sonarqube代码审查
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11921591.html
Copyright © 2011-2022 走看看