前边文章写了如何从0搭建ssm 普通web项目,链接:https://www.jianshu.com/p/b84ee274234f
配置环境什么的比较复杂,但是很有掌控感,springboot基本上是在ssm的基础上做了封装,约定优于配置,大大减少了配置项目的时间,但一些东西不在面上总觉得不太习惯,来搭建一个简单的基于springboot2的ssm项目
下载sts工具
打开springboot官网
https://spring.io/projects/spring-boot
点击最下面的tools进入sts工具下载页面,可以选择相应系统版本
sts其实是安装了sts4插件的eclipse,所以也可以用普通的eclipse直接marketPlace搜索下载sts4
用sts工具新建springboot项目
打开sts工具,new一个springboot项目,如图,一路next
上图中把type改为jar包,直接用springboot内置的tomcat启动项目,不用额外下载tomcat服务器了
上图中Available里面可以搜索所有引入模块,这里我们搜索选中搭建ssm项目所需要的mybatis,oracle,thymeleaf,spring web等模块,没错,springboot推荐使用thymeleaf而不是jsp,配置jsp需要额外配置,这里也不建议,作为模板引擎thymeleaf会更强大。
点击finish等待maven下载依赖jar包工程构建完成即可。
application.properties加入以下必须配置项,配置数据源,服务器端口等必须信息
server.port=8081
spring.datasource.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:xe
spring.datasource.username=XXXX
spring.datasource.password=XXXX
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
#spring.datasource.hikari.auto-commit=false
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
logging.config=classpath:log4j2-spring.xml
pom.xml引入log4j依赖,springboot热部署,阿里巴巴的json依赖等
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
com.zhaohy
springboot-ssmDemo
0.0.1-SNAPSHOT
jar
springboot-ssmDemo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
com.oracle.ojdbc
ojdbc8
runtime
org.springframework.boot
spring-boot-devtools
true
com.alibaba
fastjson
1.2.58
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
src/main/resource下建立log4j的配置文件 log4j2-spring.xml
/home/zhaohy/myspace/logs
MyLogs
在resources下建立mybatis文件夹以及mybatis-config.xml
resources下建立mapper文件夹以及testMapper.xml
建立controller,service,serviceImpl,dao等业务层
TestController.java
package com.zhaohy.app.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhaohy.app.service.TestService;
@Controller
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/test.do")
public String test() {
Map paramsMap = new HashMap();
List
TestService.java
package com.zhaohy.app.service;
import java.util.List;
import java.util.Map;
public interface TestService {
List
TestServiceImpl.java
package com.zhaohy.app.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhaohy.app.dao.TestMapper;
import com.zhaohy.app.service.TestService;
@Service("TestService")
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public List
TestMapper.java
package com.zhaohy.app.dao;
import java.util.List;
import java.util.Map;
public interface TestMapper {
List
SpringbootSsmApplication.java里配置mybatis扫描mapper接口路径
package com.zhaohy.app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.zhaohy.app.dao")
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
src/main/resources/templates下面新建pages文件夹以及所需html
/pages/hello.html
Insert title here
hello a
templates下的index.html
Insert title here
hello world!
sdf
至此项目搭建完毕 目录如图
浏览器访问根目录
如图所示thymeleaf生效
访问/test.do
控制台log4j打印sql成功
项目可以正常使用。
代码已上传github:
https://github.com/haiyong6/haiyongsRepository/tree/master/code/springbootSSM