springboot+mybatis+thymeleaf简单demo

21 篇文章 1 订阅
8 篇文章 0 订阅

今天再来个springboot+mybatis逆向工程+thymeleaf的简单demo,比springmvc坑还要多~

话不多说,言归正传,开门见山!!!首先大家可以先到springboot官网上生成一个项目,这样比较方便,而且启动类和pom.xml的一些依赖也可以直接生成出来,可以省事不少,当然要是想手动自己建也可以,本片介绍前种官网生成法~

1、进入springboot官网:https://spring.io/projects/spring-boot/

2、在首页上拉到底点击Spring Initializr

3、进入后页面上面的三个选项可以默认不变,自己想改名的话就改,注意红箭头有个See all点开会有各种功能,看你的项目里需要什么就选择什么,最后出来点击最后的绿色大按钮把生成项目下载到本地

4、配置pom.xml,带注释的是我后加的(这里有个坑,oracle要用我这里配置的这种,不要直接使用14,项目会报错

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>
 
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>2.0.1</version>
	</dependency>

	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-test</artifactId>
	    <scope>test</scope>
	</dependency>
		
        <dependency>
	    <groupId>org.springframework.boot</groupId>
    	    <artifactId>spring-boot-starter-tomcat</artifactId>
	    <scope>provided</scope>
	</dependency>
		
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
		
	<dependency>
	    <groupId>com.oracle</groupId>
	    <artifactId>ojdbc14</artifactId>
	    <version>11.2.0.2.0</version>
            <scope>provided</scope>
	</dependency>
		
	<!-- Mybatis -->
	<dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
	    <version>1.2.15</version>
	</dependency>
	<dependency>
	    <groupId>com.github.pagehelper</groupId>
	    <artifactId>pagehelper</artifactId>
	    <version>4.1.2</version>
	</dependency>
    </dependencies>

    <build>
        <plugins>
	    <plugin>
	        <groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
	    </plugin>
	</plugins>
    </build>

</project>

5、配置application.properties(这里面也有个坑,spring.thymeleaf.prefix这项配置好后启动项目之前templates文件夹下至少要先放一个后缀名为.html的文件否则项目起不来,或者把spring.thymeleaf.check-templatespring.thymeleaf.check-template-location两项设为false

spring.aop.auto=true
spring.aop.proxy-target-class=true

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=haha
spring.datasource.password=haha
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

server.address=127.0.0.1
server.compression.enabled=true
server.compression.mime-types=text/*,application/*,image/*,audio/*,video/*
server.compression.min-response-size=1024
server.connection-timeout=10000
server.port=8080
server.session.timeout=1800

6、配置好项目构成

7、最坑的一步,会直接导致项目无法启动,解决办法

a:在启动类文件DemoApplication.java中加两段代码:@ComponentScan和@MapperScan加上这两个注释,然后把项目中有带注释的类的包名加进去

b:把所有的类文件都放到和DemoApplication同级目录下(但是一般做项目都会分层建包,所以正式项目中不建议这么做)

这样做是因为springboot不会像springmvc那样启动是由配置好的xml去扫描各个包里的文件,它是通过启动类来扫描以后要执行的类文件,所以不特意加注解的话启动项目时会直接报错bean创建失败,就和springmvc的xml文件配置错了是一个效果

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.controllers", "com.example.demo.service.impl"})
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {

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

}

8、由于小编用的是mybatis的逆向工程所以把生成好的文件放到各个包里实现业务逻辑就好了,各位请自便

后端代码:

@RequestMapping("/haha")
public String haha(Model model){
    List<Haha> list = Hahaservice.selectByExamples();
    model.addAttribute("list", list);
    return "haha/haha";
}

前端页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
	<title>测试</title>
    </head>
    <body>
        <h1>Hello World!</h1>
	<table border="1">
	    <tr>
	        <td>ID</td>
	    </tr>
	    <tr th:each="list:${list}">
		<td th:text="${list.id}"></td>
	    </tr>
	</table>
    </body>
</html>

运行效果大功告成~有不对的地方还请各位指教,有搞不懂的朋友可以留言评论~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值