使用myeclipse搭建SpringBoot框架,整合thymeleaf模板和mybatis

一、简介
SpringBoot在搭建的时候不用XML配置文件,同时嵌入 Tomcat,无需部署war,是一个搭建、使用起来都比较方便的框架。
二、开始搭建

选版本之类的,然后输入包名项目名,下载

打开Myeclipse,导入maven项目

要等一会,因为maven镜像在国外,导入依赖有点慢,可以换成国内的镜像

项目结构

这样项目就搭建好啦

三、引入thymeleaf模板

现在开始引入thymeleaf模板,springboot官方推荐这个,vmspring boot1.5版本之后就不支持了

打开pom.xml
加入如下内容
        <dependency>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-starter-web</artifactId>
 		</dependency>
 		<!-- thymeleaf模板 -->
    	<dependency>  
        	<groupId>org.springframework.boot</groupId>  
        	<artifactId>spring-boot-starter-thymeleaf</artifactId>  
	</dependency>
如图

把依赖加进去,等读条,还是maven的问题,会有点慢

一个是web 的模块,一个是thymeleaf模板,都是spring boot里面集成的

打开 application.properties文件
把这些内容扔进去,是web的配置
server.port=8080
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5

这句比较重要,是关闭缓存。不关的话调试的时候修改html就得重启项目


现在开始测试

默认已经有一个包了,里面是已经写好的入口程序

建个 controller 包,要在入口包下面,要在入口包下面,要在入口包下面,不然扫描不到
然后肯定就是建 controller啦, 这里类似spring mvc
@Controller
public class HelloWordController {
	@RequestMapping("/index")
    public String index(Model  model) {
        model.addAttribute("hello","HelloWord");
        return "index";
    }
}


在resources 里建个templates文件夹,html就放里面,springboot默认去templates文件夹下找动态的文件,或者去application.properties文件修改放在哪个文件夹


建个Index.html
内容
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta content="text/html;charset=UTF-8"/>
</head>
<body>
	<div th:text="${hello}"></div>
</body>
</html>

这个不加的话也没问题,不过有报警告

然后去入口程序IpsApplication那里,run

HelloWord就对了,Thymeleaf模板就配进来了


四、整合mybatis

pom.xml加入下面内容


                <!-- mybatis --> 
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- postgresql驱动包 -->  
    	        <dependency>  
      		        <groupId>org.postgresql</groupId>  
      		        <artifactId>postgresql</artifactId>  
      		        <version>9.4.1209</version>  
    	        </dependency>

如图

用来引入mybatispostgresql的依赖

打开application.properties

在刚才配置下面加上数据库配置和entity扫描地址,这里我用的是postgresql,你们自己需要什么数据库自己加


#数据库配置
spring.datasource.driverClassName = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://xxxx
spring.datasource.username = xxx
spring.datasource.password = xxx
#扫描entity
mybatis.type-aliases-package=com.huyun.ips.entity

mapper 有两种方法,一种是写注解,一种就比较传统写xml,这里用写注解的方法

我在test库里弄了个t_user表,不能叫user,跟保留字冲突,总之就是会有坑
在入口文 件加句注解扫描mapper,其实也可以每个mapper都写个@Mapper注解,不过这样方便点
@MapperScan("com.huyun.ips.mapper")

entity包跟mapper包,都要在入口包下面

实体类

mapper文件

相比于xml模式其实就是把语句用注解形式写在Dao


@Select("SELECT * FROM t_user")
	//@Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
/*	@Results({
		@Result(property = "id",  column = "id", javaType = Integer.class),
		@Result(property = "name", column = "name", javaType = String.class)
	})*/
	List<User> getAll();
	
	@Insert("INSERT INTO t_user(id,name) VALUES(#{id}, #{name})")
	void insert(User user);

然后弄个测试的controller

test.html

然后入口程序那里run,如果之前的还在运行,记得先停掉之前运行的

run完访问http://localhost:8080/test

有数据就证明整合成了

 

然后再去弄个service层,方法类似spring mvc


https://download.csdn.net/download/yyyyyuhong/10275498项目demo,可以去下载


使用 Spring Boot 的优势 使用 Spring Boot 开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明 Spring Boot 让开发变得更简单 Spring Boot 对开发效率的提升是全方位的,我们可以简单做一下对比: 在没有使用 Spring Boot 之前我们开发一个 web 项目需要做哪些工作: 1)配置 web.xml,加载 Spring 和 Spring mvc 2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 Spring Boot 之后我们需要开发一个 web 项目需要哪些操作呢? 1)登录网址 http://start.spring.io/ 选择对应的组件直接下载 2)导入项目,直接开发 上面的 N 步和下面的2步形成巨大的反差,这仅仅只是在开发环境搭建的这个方面。 Spring Boot 使测试变得更简单 Spring Boot 对测试的支持不可谓不强大,Spring Boot 内置了7种强大的测试框架: JUnit: 一个 Java 语言的单元测试框架 Spring Test & Spring Boot Test:为 Spring Boot 应用提供集成测试和工具支持 AssertJ:支持流式断言的 Java 测试框架 Hamcrest:一个匹配器库 Mockito:一个 java mock 框架 JSONassert:一个针对 JSON 的断言库 JsonPath:JSON XPath 库 我们只需要在项目中引入 spring-boot-start-test依赖包,就可以对数据库、Mock、 Web 等各种情况进行测试。 Spring Boot Test 中包含了我们需要使用的各种测试场景,满足我们日常项目的测试需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值