使用SpringBoot整合ssm項(xiàng)目的實(shí)例詳解
SpringBoot是什么?
Spring Boot 是由 Pivotal 團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新 Spring 應(yīng)用的初始搭建以及開發(fā)過程。
Spring Boot 現(xiàn)在已經(jīng)成為 Java 開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬(wàn)象的,可以跟各種技術(shù)集成。成為 SpringBoot 全家桶,成為一把萬(wàn)能鑰匙。
SpringBoot的特點(diǎn)
1.創(chuàng)建獨(dú)立的 Spring 應(yīng)用程序
2.嵌入的 Tomcat ,無(wú)需部署 WAR 文件
3.簡(jiǎn)化 Maven 配置
4.自動(dòng)配置 Spring
5.提供生產(chǎn)就緒型功能,如指標(biāo),健康檢查和外部配置
Spring 官方支持 SpringBoot 提供的項(xiàng)目框架生成頁(yè)面

在eclipse上創(chuàng)建springboot工程
( jdk 版本必須 1.8 以上, springboot 基本上廢除了 1.6 、 1.7)
eclipse版本也有要求,版本過低,創(chuàng)建的工程會(huì)報(bào)錯(cuò)或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持較好,下面演示用的是eclipse
簡(jiǎn)單的使用springboot整合ssm
1. 創(chuàng)建 Maven 工程,創(chuàng)建 simple project ,類型為 jar

pom.xml
額外需要的 jar ,還得自己依賴,例如: mysql 驅(qū)動(dòng)包,阿里的數(shù)據(jù)源 druid 包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> </dependencies>
創(chuàng)建 pojo 對(duì)象
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", address=" + address + "]";
}
}
創(chuàng)建 UserMapper.xml
<?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"> <!-- namespace命名空間,唯一特性 --> <mapper namespace="com.lmq.mapper.UserMapper"> <select id="find" resultType="User"> select id,name,birthday,address from user </select> </mapper>
創(chuàng)建UserMapper 接口
public interface UserMapper {
//調(diào)用xml方式
public List<User> find();
//調(diào)用注解方式
@Select("select * from user where id=#{id}")
public User get(@Param("id") Integer id);
}
創(chuàng)建UserService接口
public interface UserService {
public List<User> find();
public User get(Integer id);
}
創(chuàng)建UserServiceImpl接口實(shí)現(xiàn)類
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
public List<User> find() {
return userMapper.find();
}
public User get(Integer id){
return userMapper.get(id);
}
}
創(chuàng)建UserController類
使用 @RestController 替代 @Controller 和 @ResponseBody (返回 json 串)
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/find")
public List<User> find() {
return userService.find();
}
@RequestMapping("/get/{id}")
public User get(@PathVariable Integer id){
return userService.get(id);
}
}
創(chuàng)建application.yml
全局配置文件, yml 為新的配置文件方式,注意其中格式為空格,不能有 tab 。
配置端口,配置數(shù)據(jù)源,配置 mybatis 全局配置。
注意:如果端口,啟動(dòng)時(shí)日志顯示 8080 ,說明此文件未加載。檢查原因一般是文件名或者路徑不正確。
server: port: 8080 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mybatisdb username: root password: root mybatis: typeAliasesPackage: com.lmq.pojo mapperLocations: classpath:mappers/*.xml logging: level: com.lmq.mapper: debug
創(chuàng)建RunApplication.java
@SpringBootApplication
@MapperScan("cn.lmq.mapper") //掃描Mybatis接口文件
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class, args);
}
}
初步整合完畢,比三大框架ssm好用太多了
傳統(tǒng)構(gòu)建 Maven 項(xiàng)目, pom 中的依賴包繁多,升級(jí)一個(gè) jar 版本時(shí),會(huì)引發(fā)新的沖突,調(diào)試許久。而 SpringBoot 接管了 jar 的版本,它構(gòu)建好一套,這套中各 jar 的版本已經(jīng)測(cè)試好,開發(fā)者再無(wú)需去關(guān)注每個(gè)依賴包的版本及沖突問題,從而簡(jiǎn)化開發(fā)。
再者,它啟動(dòng)也非???,直接運(yùn)行一個(gè)類,使用 tomcat 的 maven 插件。開發(fā)調(diào)試時(shí)效率提高。
熱部署支持
配置pom.xml
<!-- 熱部署支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
總結(jié)
以上所述是小編給大家介紹的使用SpringBoot整合ssm項(xiàng)目的實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
談?wù)凧ava中對(duì)象,類和this,super,static關(guān)鍵字的使用
對(duì)象:對(duì)象是類的一個(gè)實(shí)例,有狀態(tài)和行為。類:類是一個(gè)模板,它描述一類對(duì)象的行為和狀態(tài)。本文就來(lái)和大家聊聊Java中對(duì)象,類和關(guān)鍵字的使用,需要的可以參考一下2022-08-08
SpringBoot使用Sa-Token實(shí)現(xiàn)權(quán)限認(rèn)證
本文主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)權(quán)限認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Mybatis一對(duì)多查詢的兩種姿勢(shì)(值得收藏)
這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多查詢的兩種姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
詳解SpringBoot定制@ResponseBody注解返回的Json格式
這篇文章主要介紹了詳解SpringBoot定制@ResponseBody注解返回的Json格式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
如何解決SpringBoot啟動(dòng)時(shí)無(wú)法加載配置文件或環(huán)境變量問題
文章主要介紹了在Spring Boot項(xiàng)目中遇到配置文件加載失敗和資源目錄圖標(biāo)異常的問題,并提供了詳細(xì)的解決步驟,解決方法包括在pom.xml文件中添加特定配置,確保資源目錄順序正確,以及注意節(jié)點(diǎn)的正確使用,通過這些步驟,可以有效解決資源加載問題,提高開發(fā)效率2024-12-12
詳解JDBC的概念及獲取數(shù)據(jù)庫(kù)連接的5種方式
Java?DataBase?Connectivity是將Java與SQL結(jié)合且獨(dú)立于特定的數(shù)據(jù)庫(kù)系統(tǒng)的應(yīng)用程序編程接口,一種可用于執(zhí)行SQL語(yǔ)句的JavaAPI。本文主要介紹了JDBC的概念及獲取數(shù)據(jù)庫(kù)連接的5種方式,需要的可以參考一下2022-09-09
Springboot整合ActiveMQ實(shí)現(xiàn)消息隊(duì)列的過程淺析
昨天仔細(xì)研究了activeMQ消息隊(duì)列,也遇到了些坑,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合ActiveMQ的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
SpringBoot攔截器與文件上傳實(shí)現(xiàn)方法與源碼分析
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了。本文主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)登陸攔截器與文件上傳功能,需要的朋友可以參考下2022-10-10
詳解SpringBoot和Mybatis配置多數(shù)據(jù)源
本篇文章主要介紹了詳解SpringBoot和Mybatis配置多數(shù)據(jù)源,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05

