参考文章
Github项目地址:https://github.com/githubgxr/news
传统的SSM配置方式有多个配置文件,如:spring-config.xml、springMVC-servlet.xml、web.xml。但是,使用SpringBoot配置SSM时,可以不用配置这些文件,只要有一个启动类。
使用SpringBoot配置SSM开发环境步骤如下:
springboot依赖(内置了tomcat依赖):
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
springmvc依赖:
org.springframework.boot
spring-boot-starter-web
mybatis依赖:
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
jdbc依赖:
org.springframework.boot
spring-boot-starter-jdbc
数据库连接驱动类依赖:
mysql
mysql-connector-java
runtime
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.gxr
news
0.0.1-SNAPSHOT
news
News system project for Spring Boot
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-devtools
2.1.3.RELEASE
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
thymeleaf(springboot官方推荐的)模板引擎(类似于jsp模板)依赖(所以可以不用再引入servlet、jsp依赖):
org.springframework.boot
spring-boot-starter-thymeleaf
springboot开发工具依赖:
org.springframework.boot
spring-boot-devtools
2.1.3.RELEASE
springboot开发工具作用:项目修改可以不用重新开始启动类,只要重新构建项目(build project)即可应用修改。
# tomcat访问端口
server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#加载mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mybatis-config.xml
#数据库表对应实体类所在包
mybatis.type-aliases-package = com.gxr.news.DO
#数据源配置
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/newssys?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
package com.gxr.news.DO;
public class testDO {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "testDO{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
}
package com.gxr.news.mapper;
import com.gxr.news.DO.testDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface testDOMapper {
@Select("select * from test")
public List listTestDOs();
}
package com.gxr.news.test.service;
import com.gxr.news.DO.testDO;
import java.util.List;
public interface TestService {
public List listTestDOs();
}
package com.gxr.news.test.service.impl;
import com.gxr.news.DO.testDO;
import com.gxr.news.mapper.testDOMapper;
import com.gxr.news.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private testDOMapper mapper;
@Override
public List listTestDOs() {
return mapper.listTestDOs();
}
}
package com.gxr.news.test.controller;
import com.gxr.news.DO.testDO;
import com.gxr.news.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/all")
public ModelAndView all(Model model) {
// 数据库查询的数据
List testDOs = testService.listTestDOs();
model.addAttribute("testDOs", testDOs);
// test/users实际指的是:resources/templates/test/users.html,这个页面就是返回浏览器的页面
return new ModelAndView("test/users","model",model);
}
}
用户列表
用户列表:
id
name
package com.gxr.news;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.gxr.news.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意: “启动类可能扫描不到访问数据库的”问题
解决方法: 在启动类的 @SpringBootApplication 下面再加一个 @MapperScan 注解在启动类上。
浏览器访问:http://localhost:80/all,结果如下:
数据库数据如下:
结果一致,开发环境配置正确。
与传统SSM配置方式相比,使用Springboot配置SSM十分方便,只有一个application.properties配置文件。