Spring Boot学习笔记-1.4环境搭建-Eclipse SpringBoot Web项目基础环境搭建-接入Thymeleaf模版引擎

一.概述:

  Thymeleaf是一种模板语言,可以动态或者静态显示文本内容,本例主要讲解如何在SpringBoot项目中引入Thymeleaf。

二.环境:

  SpringBoot +maven+mysql+Thymeleaf

三.步骤:

  1. 在项目pom.xml文件中引入Thymeleaf依赖 
    1.         
              
                  org.thymeleaf
                  thymeleaf
                  3.0.9.RELEASE
              

              
                  org.thymeleaf
                  thymeleaf-spring4
                  3.0.9.RELEASE
              

              
                  org.springframework.boot
                  spring-boot-starter-thymeleaf
              

              
  2. 在application.yml配置文件中添加Thymeleaf配置
    1. Spring Boot学习笔记-1.4环境搭建-Eclipse SpringBoot Web项目基础环境搭建-接入Thymeleaf模版引擎_第1张图片
    2. # thymeleaf模板配置 S
        thymeleaf: 
          prefix: classpath:/templates/ 
          suffix: .html
          mode: HTML5
          encoding: UTF-8
          cache: false #热部署文件,页面不产生缓存,及时更新
        resources: 
          chain: 
            strategy: 
              content: 
                enabled: true
                paths: /**
      # thymeleaf模板配置 E
       
  3. 使用Thymeleaf展示数据
    1. User实体类
      package com.kiboy.bean;
      
      import java.util.Date;
      
      public class User {
          private String id;
          private String code;
          private String name;
          private String password;
          private int status;
          private String loginid;
          private Date gmt_create; 
          private Date gmt_modified; 
      
          public String getId() {
              return id;
          }
      
          public void setId(String id) {
              this.id = id == null ? null : id.trim();
          }
      
      	public String getLoginid() {
      		return loginid;
      	}
      
      	public void setLoginid(String loginid) {
      		this.loginid = loginid;
      	}
      
      	public Date getGmt_create() {
      		return gmt_create;
      	}
      
      	public void setGmt_create(Date gmt_create) {
      		this.gmt_create = gmt_create;
      	}
      
      	public Date getGmt_modified() {
      		return gmt_modified;
      	}
      
      	public void setGmt_modified(Date gmt_modified) {
      		this.gmt_modified = gmt_modified;
      	}
      
      	public String getCode() {
      		return code;
      	}
      
      	public void setCode(String code) {
      		this.code = code;
      	}
      
      	public String getName() {
      		return name;
      	}
      
      	public void setName(String name) {
      		this.name = name;
      	}
      
      	public String getPassword() {
      		return password;
      	}
      
      	public void setPassword(String password) {
      		this.password = password;
      	}
      
      	public int getStatus() {
      		return status;
      	}
      
      	public void setStatus(int status) {
      		this.status = status;
      	}
          
      }
      
      
    2. UserService接口
      package com.kiboy.service;
      
      import java.util.List;
      
      import org.springframework.stereotype.Service;
      
      import com.kiboy.bean.User;
      
      @Service
      public interface UserService {
      	
          public List getUser();
          public List getAllUser();
          public int addUser(User user);
          public List getUserById(String id);
          public int delUser(String id);
      }
      
      

       

    3. UserMapper

      package com.kiboy.mapper;
      
      import java.util.List;
      
      import org.apache.ibatis.annotations.Delete;
      import org.apache.ibatis.annotations.Insert;
      import org.apache.ibatis.annotations.Mapper;
      import org.apache.ibatis.annotations.Select;
      import org.apache.ibatis.annotations.Update;
      
      import com.kiboy.bean.User;
      
      @Mapper
      public interface UserMapperNoXml {
      
      	@Delete("delete from user where id=#{id}")
          int deleteByPrimaryKey(String id);
      
      	@Insert("INSERT INTO USER (code,name,loginid,password,status)VALUES(#{code},#{name},#{loginid},#{password},#{status})")
          int insert(User u);
      
          @Select("SELECT * FROM USER WHERE ID=#{id}")
          User selectByPrimaryKey(String id);
      
          int updateByPrimaryKeySelective(User u);
          @Update("UPDATE USER SET code=#{code},name=#{name},status=#{status} where id=#{id}")
          int updateByPrimaryKey(User u);
          
          @Select("SELECT * FROM USER")
          List selectAll();
      }

       

    4. UserService实现类

      package com.kiboy.service.impl;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import com.kiboy.bean.User;
      import com.kiboy.mapper.UserMapperNoXml;
      import com.kiboy.service.UserService;
      
      @Service
      public class UserServiceImplNoXml implements UserService{
          
          @Autowired
          private UserMapperNoXml mapper;
          
          public List getUser(){
              List list = new ArrayList();
              list.add(mapper.selectByPrimaryKey("1"));
              return list;
          }
      	public List getAllUser(){
      	    List list = new ArrayList();
      	    list = mapper.selectAll();
      	    return list;
      	}
      
      	public int addUser(User user) {
      		return mapper.insert(user);
      	}
      
      	public int delUser(String id) {
      		return mapper.deleteByPrimaryKey(id);
      	}
      	
      	public List getUserById(String id) {
      		return null;
      	}
      }
      
      

       

    5. UserController

      package com.kiboy.controller;
      
      import java.util.List;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PostMapping;
      import org.springframework.web.bind.annotation.RequestBody;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.servlet.ModelAndView;
      
      import com.kiboy.bean.User;
      import com.kiboy.service.impl.UserServiceImplNoXml;
      
      @RestController
      @RequestMapping("/UserNoXml")
      public class UserControllerNoXml {
      
          @Autowired
          private UserServiceImplNoXml userService;
          
          
          /**
           * 获取用户列表
           * @return
           */
          @GetMapping("/userList")
          public ModelAndView getAllUser(Model model) {
          	List list = userService.getAllUser();
            	int num = list.size();
            	if(null!=list && num>3){
            		for (int i = 0; i < num-3; i++) {
            			list.remove(0);
            		}
          	}
              model.addAttribute("user", list.get(0));
              model.addAttribute("userList", list);
              model.addAttribute("title", "用户信息列表");
              return new ModelAndView("user/userList", "userModel", model);
          }

       

    6. 前台页面(具体的Thymeleaf语法可以自行百度下,在后续的文章中会做解释,这里先照着用就好)

      
      
      
          
          welcome
      
      
          

      Welcome to springboot

      ID Code Name
      没有用户信息!!
      -1
      ​​​​

       

    7. 启动应用访问页面http://127.0.0.1:20001/UserNoXml/userList,显示如下Spring Boot学习笔记-1.4环境搭建-Eclipse SpringBoot Web项目基础环境搭建-接入Thymeleaf模版引擎_第2张图片

你可能感兴趣的:(SpringBoot学习笔记)