使用myeclipse搭建SpringBoot框架,整合thymeleaf模板和mybatis
2018-03-07 21:00
1066 查看
一、简介SpringBoot在搭建的时候不用XML配置文件,同时嵌入Tomcat,无需部署war,是一个搭建、使用起来都比较方便的框架。二、开始搭建访问http://start.spring.io/
选版本之类的,然后输入包名项目名,下载
打开Myeclipse,导入maven项目
要等一会,因为maven镜像在国外,导入依赖有点慢,可以换成国内的镜像
项目结构这样项目就搭建好啦
三、引入thymeleaf模板现在开始引入thymeleaf模板,springboot官方推荐这个,vm在spring boot1.5版本之后就不支持了
打开pom.xml加入如下内容
如图
把依赖加进去,等读条,还是maven的问题,会有点慢一个是web 的模块,一个是thymeleaf模板,都是spring boot里面集成的
打开application.properties文件
把这些内容扔进去,是web的配置
这句比较重要,是关闭缓存。不关的话调试的时候修改html就得重启项目
现在开始测试
默认已经有一个包了,里面是已经写好的入口程序
建个controller包,要在入口包下面,要在入口包下面,要在入口包下面,不然扫描不到
然后肯定就是建controller啦,这里类似spring mvc
在resources里建个templates文件夹,html就放里面,springboot默认去templates文件夹下找动态的文件,或者去application.properties文件修改放在哪个文件夹
建个Index.html内容
这个不加的话也没问题,不过有报警告
然后去入口程序IpsApplication那里,run
访问http://localhost:8080/index
有HelloWord就对了,Thymeleaf模板就配进来了
四、整合mybatispom.xml加入下面内容
如图
用来引入mybatis和postgresql的依赖
打开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注解,不过这样方便点
实体类
mapper文件相比于xml模式其实就是把语句用注解形式写在Dao里
test.html
然后入口程序那里run,如果之前的还在运行,记得先停掉之前运行的
run完访问http://localhost:8080/test有数据就证明整合成了 然后再去弄个service层,方法类似spring mvc https://download.csdn.net/download/yyyyyuhong/10275498项目demo,可以去下载
选版本之类的,然后输入包名项目名,下载
打开Myeclipse,导入maven项目
要等一会,因为maven镜像在国外,导入依赖有点慢,可以换成国内的镜像
项目结构这样项目就搭建好啦
三、引入thymeleaf模板现在开始引入thymeleaf模板,springboot官方推荐这个,vm在spring 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
访问http://localhost:8080/index
有HelloWord就对了,Thymeleaf模板就配进来了
四、整合mybatispom.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>
如图
用来引入mybatis和postgresql的依赖
打开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,可以去下载
相关文章推荐
- myeclipse下使用maven搭建SSM(spring、springmvc、mybatis)框架
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- 使用IDEA整合Spring+Spring MVC+Mybatis+Maven+Jetty框架的搭建
- 使用maven与MyEclipse整合ssm(Spring MVC、Spring、Mybatis)三大框架并实现用户注册(环境搭载+实例源码下载)
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
- MyEclipse中搭建spring-boot+mybatis+freemarker框架
- thymeleaf :springboot整合mybatis的web项目中使用html而不是jsp当页面渲染
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- Spring boot框架结合MyBatis框架模板搭建
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- 开发框架整合与搭建:spring boot+mybatis+jedis
- 使用intellij idea搭建spring-springmvc-mybatis整合框架环境
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- thymeleaf模板使用笔记(与Spring整合)