SpringBoot中怎么整合ssm項(xiàng)目?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
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.自動(dòng)配置 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)建的工程會(huì)報(bào)錯(cuò)或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持較好,下面演示用的是eclipse
簡單的使用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)測試好,開發(fā)者再無需去關(guān)注每個(gè)依賴包的版本及沖突問題,從而簡化開發(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>
看完上述內(nèi)容,你們掌握SpringBoot中怎么整合ssm項(xiàng)目的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞名稱:SpringBoot中怎么整合ssm項(xiàng)目-創(chuàng)新互聯(lián)
URL鏈接:http://www.chinadenli.net/article30/gsjpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站營銷、網(wǎng)站改版、ChatGPT、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容