十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
SpringBoot中怎么整合ssm項(xiàng)目?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
SpringBoot是什么?
Spring Boot 是由 Pivotal 團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新 Spring 應(yīng)用的初始搭建以及開發(fā)過程。
Spring Boot 現(xiàn)在已經(jīng)成為 Java 開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術(shù)集成。成為 SpringBoot 全家桶,成為一把萬能鑰匙。
SpringBoot的特點(diǎn)
1.創(chuàng)建獨(dú)立的 Spring 應(yīng)用程序
2.嵌入的 Tomcat ,無需部署 WAR 文件
3.簡化 Maven 配置
4.自動配置 Spring
5.提供生產(chǎn)就緒型功能,如指標(biāo),健康檢查和外部配置
Spring 官方支持 SpringBoot 提供的項(xiàng)目框架生成頁面
https://start.spring.io/
在eclipse上創(chuàng)建springboot工程
( jdk 版本必須 1.8 以上, springboot 基本上廢除了 1.6 、 1.7)
eclipse版本也有要求,版本過低,創(chuàng)建的工程會報(bào)錯或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持較好,下面演示用的是eclipse
簡單的使用springboot整合ssm
1. 創(chuàng)建 Maven 工程,創(chuàng)建 simple project ,類型為 jar
pom.xml
額外需要的 jar ,還得自己依賴,例如: mysql 驅(qū)動包,阿里的數(shù)據(jù)源 druid 包
org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE UTF-8 UTF-8 1.8 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java com.alibaba druid-spring-boot-starter 1.1.0
創(chuàng)建 pojo 對象
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
創(chuàng)建UserMapper 接口
public interface UserMapper { //調(diào)用xml方式 public Listfind(); //調(diào)用注解方式 @Select("select * from user where id=#{id}") public User get(@Param("id") Integer id); }
創(chuàng)建UserService接口
public interface UserService { public Listfind(); public User get(Integer id); }
創(chuàng)建UserServiceImpl接口實(shí)現(xiàn)類
@Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public Listfind() { 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 Listfind() { 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 全局配置。
注意:如果端口,啟動時日志顯示 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 中的依賴包繁多,升級一個 jar 版本時,會引發(fā)新的沖突,調(diào)試許久。而 SpringBoot 接管了 jar 的版本,它構(gòu)建好一套,這套中各 jar 的版本已經(jīng)測試好,開發(fā)者再無需去關(guān)注每個依賴包的版本及沖突問題,從而簡化開發(fā)。
再者,它啟動也非???,直接運(yùn)行一個類,使用 tomcat 的 maven 插件。開發(fā)調(diào)試時效率提高。
熱部署支持
配置pom.xml
org.springframework.boot spring-boot-devtools true
看完上述內(nèi)容,你們掌握SpringBoot中怎么整合ssm項(xiàng)目的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!