IDEA下创建Springboot,thymeleaf,Mybatis,Postgresql,Gradle项目
来源:互联网 发布:网络项目外包合同范本 编辑:程序博客网 时间:2024/05/18 17:04
最近Springboot框架越来越流行,因为集成了tomcat,可以直接打成jar包,方便部署,又可以完全摒弃配置文件,很适用于现在流行的微服务。下面让我们来完成Springboot项目的搭建。
1、目录结构
2、gradle文件
下载相应的jar包
group 'bingo'version '1.0.0'apply plugin: 'java'repositories { mavenCentral()}buildscript { ext { springBootVersion = '1.4.1.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'spring-boot'jar { baseName = 'bingo' version = '1.0.0'}sourceCompatibility = 1.8targetCompatibility = 1.8repositories { mavenCentral()}dependencies { compile('org.springframework.boot:spring-boot-devtools') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-aop') compile('org.springframework.boot:spring-boot-starter-cache') compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1') compile('org.postgresql:postgresql:9.4.1212.jre7') testCompile('org.springframework.boot:spring-boot-starter-test')}task wrapper(type: Wrapper) { gradleVersion = '2.12'}
3、BingoApplication启动文件
整个Springboot项目由此class中的main程序启动,由@SpringBootApplication完成对项目初始化的bean创建。其中mybatis首先需要由mybatis-spring jar中的sqlSessionFactoryBean生成SqlSessionFactory,然后再配置dateSource。最后由@MapperScan注解完成对dao的bean的生成
@SpringBootApplication@MapperScan("com.blog.sqlmap")public class BingoApplication { public static void main(String[] args) { new SpringApplicationBuilder().bannerMode(Banner.Mode.OFF); SpringApplication application = new SpringApplication(BingoApplication.class); application.setBannerMode(Banner.Mode.OFF); application.run(args); } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); }}
4、application.properties配置文件
配置了关于thymeleaf的配置和postgresql数据库连接dataSource的配置。
server.session-timeout= -1 server.tomcat.uri-encoding = UTF-8# THYMELEAF (ThymeleafAutoConfiguration)spring.thymeleaf.prefix=classpath:/web/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.cache=truespring.mvc.favicon.enabled = falsespring.datasource.url = jdbc:postgresql://localhost/postgresspring.datasource.username = postgresspring.datasource.password = rootspring.datasource.driver-class-name=org.postgresql.Driver
5、Controller,server,dao,及sqlmap
dao层只需要定义一个接口,由BingoApplication中的注解@MapperScan(“com.blog.sqlmap”)完成dao bean的创建。
@RequestMapping("/datebase")@Controllerpublic class DatebaseController { @Autowired private DatebaseService datebaseService; @RequestMapping("/page") public ModelAndView page(){ List<Datebase> datebaseList = datebaseService.findDatebaseList(); ModelAndView mav = new ModelAndView("/datebase/datebase1"); mav.addObject("test",datebaseList.get(0)); return mav; }public interface DatebaseService { List<Datebase> findDatebaseList();}@Servicepublic class DatebaseServiceImpl implements DatebaseService{ @Autowired private DatebaseDao datebaseDao; @Override public List<Datebase> findDatebaseList() { return datebaseDao.findDatebaseList(); }}public interface DatebaseDao { List<Datebase> findDatebaseList();}<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.blog.sqlmap.DatebaseDao" > <select id="findDatebaseList" resultType="com.blog.datebase.model.Datebase"> select * from datebase </select></mapper>
6、datebase1 HTML页面
thymeleaf中用标签th:text=”${…}”或者 th:value=”{…}”来接受后台传出来的数据,这样就会展示从后台传过来的text对象。
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><meta charset="utf-8"/><div th:fragment="header"> <link rel="stylesheet" href="/css/common/init.css" type="text/css"/> <link rel="stylesheet" href="/css/common/common.css" type="text/css"/> <div class="log" th:text="${test.username}">登录</div></div></html>
现在呢,我们的Springboot项目已经完成,也已经能够跑起来了。截图呢,我也不贴了,也就是一个字段而已。
关于mybatis,目前在学习源码,以后会写几篇关于mybatis源码的博文。
如果你觉得有什么可以改进的地方,请不要吝啬你的手指,给我留言吧。
- IDEA下创建Springboot,thymeleaf,Mybatis,Postgresql,Gradle项目
- idea 下gradle创建springboot 项目
- IDEA下gradle创建springboot项目
- 初来乍道多多关照,用idea构建springboot+gradle+mybatis+thymeleaf项目
- idea+springboot+gradle创建项目
- IDEA下创建Springboot,freemarker,mockserver,Gradle项目
- 使用IDEA+GRADLE创建springboot项目
- 使用idea+gradle创建springboot项目
- Gradle创建springboot项目
- idea创建springboot项目
- idea+springboot创建项目
- SpringBoot + Thymeleaf + JPA创建一个web项目
- [SpringBoot学习]-IDEA创建Gradle多Module结构的SpringBoot项目
- 使用gradle 创建springboot项目
- SpringBoot-mybatis- thymeleaf
- springboot + thymeleaf + mybatis + ueditor
- IDEA SpringBoot gradle打项目jar包
- 使用IDEA创建SpringBoot项目
- Huffman树与Huffman编码
- VS2015 + CUDA 8.0 配置GTX1070的OpenCL 开发环境
- Spring简介
- poj_1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)
- hql常用语句
- IDEA下创建Springboot,thymeleaf,Mybatis,Postgresql,Gradle项目
- 在线赛题解以及总结
- 在virtual box上安装ubuntu server失败的解决方法
- 【HNOI2013】游走
- ETL的经验总结
- (dubbo)java.lang.reflect.MalformedParameterizedTypeException
- 如何完善自己的知识结构
- 内存池的实现
- MongoDB可视化工具RoboMongo----Windows安装