使用idea+springboot+Mybatis搭建一个简单的web项目。css
首先新建一个项目;html
在这里选择Maven项目也能够,可是IDEA为咱们提供了一种更方便快捷的建立方法,即Spring Initializr。选择后点击Next;java
把项目信息写好,Next;mysql
按下面三张图勾选设置;web
最后Finish。spring
等待Maven自动加载完成后,最初的项目结构以下图。在Springboot属性文件application.properties中,把数据库链接属性加上,同时能够设置服务端口。sql
spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver #页面热加载 spring.thymeleaf.cache = false #端口 server.port=8888
resources目录下,static文件夹是放置各类静态资源,如css,js,img等文件的。templates文件夹则是默认放置网页的。固然也能够更改。数据库
在static文件夹下新建一个测试css,test.css。浏览器
body{ color: red; }
在templates文件夹下新建一个html,要注意的是meta这个标签的结束符软件并无自动加上,须要手动加上,不然访问网页时会报错。并引入test.cssspringboot
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<link rel="stylesheet" href="test.css" type="text/css" />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
接下来能够写一个controller了
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/index") public String index(){ return "hello"; } }
完成以后,经过方式1和方式2均可以启动项目
接下来能够在浏览器中测试了
=====================2019-4-2更新==================================
很久没登陆.忘了这个文章了
添加application.properties中的数据库配置
# 数据库访问配置 # 主数据源,默认的 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/flag spring.datasource.username = root spring.datasource.password = root # 下面为链接池的补充设置,应用到上面全部数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取链接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测须要关闭的空闲链接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个链接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,而且指定每一个链接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql没法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 经过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #页面热加载 spring.thymeleaf.cache = false #mybatis配置 #mapper位置 mybatis.mapper-locations=classpath:mapper/*Mapper.xml #领域类包别名 mybatis.type-aliases-package=com.legoo.flag.model #mybatis配置文件 mybatis.config-location=classpath:config/mybatis-config.xml #pagehelper分页插件配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql pagehelper.row-bounds-with-count=true
在resource文件夹下添加mapper,用来存放mybatis的xml.
config/mybatis-config.xml包下的文件暂时是空的,都在application.properties里面配置了.
而后就能够写业务了
到此,一个简单的项目搭建完成。