栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot 实战-整合MyBatis和thymeleaf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot 实战-整合MyBatis和thymeleaf

Spring boot 实战 搭建第一个spring-boot项目
  1. 访问 https://start.spring.io/ , 选择好之后, 点击 generate, 会生成对应的压缩包。

  1. idea 导入对应的文件, 设置 maven仓库和jdk(1.8)
  2. resources目录下添加 banner.txt,

​ banner生成网站: http://patorjk.com/software/taag/

  1. 配置application.properties文件
# 端口
server.port=8080
# 文根
server.servlet.context-path=/itboat008
  1. 配置 Controller:
package com.itboat008.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller// (value="/user") 这里做配置请求对应url会报404
@RequestMapping(value="/user")
public class UserController {

    @RequestMapping("getUser")
    @ResponseBody
    public String getUser(){return "itboat008";}

}

  1. 启动类
package com.itboat008.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemoApplication.class, args);
	}

}

项目结构:

  1. 启动项目后, 访问 : http://localhost:8080/itboat008/user/getUser

使用 RestController
package com.itboat008.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController// (value="/order") 这里做配置请求对应url会报404
@RequestMapping("/order")
public class OrderController {
    @GetMapping("/getOrder")
    public String getOrder(){return "asd";}
}

注意:

@RestController 和 @Controller 配置 value属性不会作用到 请求路径中去, 需采用 @RequestMapping里面的

属性去拼接请求url。

使用thymeleaf动态模板 引用pom

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

Controller 中配置页面动态传参
package com.itboat008.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller// (value="/user") 这里做配置请求对应url会报404
@RequestMapping(value="/user")
public class UserController {

    @RequestMapping("sayHello")
    public String sayHello(Map params){
        String hello = "hello , my name is itboat008";
        params.put("hello",hello);
        return "User";
    }

}

classpath下的templates中添加User.html



    
    Title


        


application.properties中添加thymeleaf配置
server.port=8080
server.servlet.context-path=/itboat008
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#HTML5已经弃用
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
#页面不缓存
spring.thymeleaf.cache=false
注意:

因为spring.thymeleaf.prefix配置的是classpath路径下, 修改html或者添加html需要重启或者maven重新clean install,才能生效。

springboot 整合 h2 + MyBatis 注意:

当maven打包出现failure时候, 需要看具体的异常:

#maven 打包命令, 打印异常堆栈, 忽略测试用例
mvn clean install -e -X -DskipTests

报错内容:

org.apache.maven.plugin.MojoExecutionException: Input length = 1

原因是缺失 maven插件, 在pom.xml中添加 如下配置,重新clean install之后,问题解决


    org.apache.maven.plugins
    maven-resources-plugin
    2.7
    
        
            org.apache.maven.shared
            maven-filtering
            1.3
        
    

maven 中添加依赖:

    com.h2database
    h2
    runtime


    org.mybatis.spring.boot
    mybatis-spring-boot-starter


    org.springframework.boot
    spring-boot-starter-jdbc


    org.mybatis
    mybatis-spring
    1.3.2


    org.mybatis
    mybatis
    3.5.0


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.5


    com.github.pagehelper
    pagehelper
    5.1.4

application.properties中添加配置

#####################mybatis###########################
mybatis.mapper-locations=classpath:mappings
    private int pageNum;

    
    private int pageSize;

    
    private long totalSize;

    
    private int totalPages;

    
    private List content;

}

PageUtils:
package com.itboat008.springboot.utils;

import com.github.pagehelper.PageInfo;

public class PageUtils {
    public static PageResult getPageResult(PageRequest pageRequest, PageInfo pageInfo) {
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalSize(pageInfo.getTotal());
        pageResult.setTotalPages(pageInfo.getPages());
        pageResult.setContent(pageInfo.getList());
        return pageResult;
    }

}

测试:
  • 访问 http://localhost:8080/itboat008/user/findById?id=1

  • 访问 http://localhost:8080/itboat008/user/findUsersByCompanyId/1

  • 访问: http://localhost:8080/itboat008/user/findUserByPage

返回:

{
    "pageNum": 3,
    "pageSize": 2,
    "totalSize": 6,
    "totalPages": 3,
    "content": [
        {
            "id": 5,
            "userName": "欧阳锋",
            "userCode": "ouyangfeng",
            "companyId": 2
        },
        {
            "id": 6,
            "userName": "一灯",
            "userCode": "yideng",
            "companyId": 3
        }
    ]
}
  • 访问 http://localhost:8080/itboat008/user/deleteById?id=1

查询数据库:

  • 访问: http://localhost:8080/itboat008/user/addUser

  • 访问 http://localhost:8080/itboat008/user/updateUser

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号