使用SpringBoot整合ssm項(xiàng)目的實(shí)例詳解
SpringBoot是什么?
Spring Boot 是由 Pivotal 團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新 Spring 應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。
Spring Boot 現(xiàn)在已經(jīng)成為 Java 開(kāi)發(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版本也有要求,版本過(guò)低,創(chuàng)建的工程會(huì)報(bào)錯(cuò)或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持較好,下面演示用的是eclipse
簡(jiǎn)單的使用springboot整合ssm
1. 創(chuàng)建 Maven 工程,創(chuàng)建 simple project ,類(lèi)型為 jar
pom.xml
額外需要的 jar ,還得自己依賴(lài),例如: 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)類(lèi)
@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類(lèi)
使用 @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 ,說(shuō)明此文件未加載。檢查原因一般是文件名或者路徑不正確。
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 中的依賴(lài)包繁多,升級(jí)一個(gè) jar 版本時(shí),會(huì)引發(fā)新的沖突,調(diào)試許久。而 SpringBoot 接管了 jar 的版本,它構(gòu)建好一套,這套中各 jar 的版本已經(jīng)測(cè)試好,開(kāi)發(fā)者再無(wú)需去關(guān)注每個(gè)依賴(lài)包的版本及沖突問(wèn)題,從而簡(jiǎn)化開(kāi)發(fā)。
再者,它啟動(dòng)也非???,直接運(yùn)行一個(gè)類(lèi),使用 tomcat 的 maven 插件。開(kāi)發(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
javaweb中ajax請(qǐng)求后臺(tái)servlet(實(shí)例)
下面小編就為大家?guī)?lái)一篇javaweb中ajax請(qǐng)求后臺(tái)servlet(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06java多線程join()方法的作用和實(shí)現(xiàn)原理解析(應(yīng)用場(chǎng)景)
join方法主要是用于將當(dāng)前線程掛起,等待其他線程結(jié)束后在執(zhí)行當(dāng)前線程,本文通過(guò)應(yīng)用場(chǎng)景分析代碼示例講解java多線程join()方法的作用和實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-07-07Java線程中的常見(jiàn)方法(start方法和run方法)
這篇文章主要介紹了Java線程中的常見(jiàn)方法(start方法和run方法),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07mybatis單元測(cè)試過(guò)程(無(wú)需啟動(dòng)容器)
在MyBatis中,單元測(cè)試無(wú)需啟動(dòng)容器即可進(jìn)行,主要涉及Configuration類(lèi)、Executor接口及其實(shí)現(xiàn)類(lèi),以及XMLMapperBuilder的作用,Configuration類(lèi)是配置的承載者,負(fù)責(zé)初始化并解析配置文件,Executor接口及其實(shí)現(xiàn)類(lèi)2024-09-09Springboot+AOP實(shí)現(xiàn)時(shí)間參數(shù)格式轉(zhuǎn)換
前端傳過(guò)來(lái)的時(shí)間參數(shù),后端可以自定義時(shí)間格式轉(zhuǎn)化使用,這樣想轉(zhuǎn)成什么就轉(zhuǎn)成什么。本文將利用自定義注解AOP實(shí)現(xiàn)時(shí)間參數(shù)格式轉(zhuǎn)換,感興趣的可以了解一下2022-04-04Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解
這篇文章主要介紹了Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08