欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用SpringBoot整合ssm項目的實例詳解

 更新時間:2018年11月07日 10:03:32   作者:彳亍風  
Spring Boot 現(xiàn)在已經(jīng)成為 Java 開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術(shù)集成。這篇文章主要介紹了使用SpringBoot整合ssm項目,需要的朋友可以參考下

SpringBoot是什么?

Spring Boot 是由 Pivotal 團隊提供的全新框架,其設(shè)計目的是用來簡化新 Spring 應用的初始搭建以及開發(fā)過程。

Spring Boot 現(xiàn)在已經(jīng)成為 Java 開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術(shù)集成。成為 SpringBoot 全家桶,成為一把萬能鑰匙。

SpringBoot的特點

1.創(chuàng)建獨立的 Spring 應用程序

2.嵌入的 Tomcat ,無需部署 WAR 文件

3.簡化 Maven 配置

4.自動配置 Spring

5.提供生產(chǎn)就緒型功能,如指標,健康檢查和外部配置

Spring 官方支持 SpringBoot 提供的項目框架生成頁面

https://start.spring.io/

在eclipse上創(chuàng)建springboot工程

( jdk 版本必須 1.8 以上, springboot 基本上廢除了 1.6 、 1.7)

eclipse版本也有要求,版本過低,創(chuàng)建的工程會報錯或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持較好,下面演示用的是eclipse

簡單的使用springboot整合ssm

1. 創(chuàng)建 Maven 工程,創(chuàng)建 simple project ,類型為 jar

pom.xml

額外需要的 jar ,還得自己依賴,例如: mysql 驅(qū)動包,阿里的數(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 對象

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接口實現(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 全局配置。

注意:如果端口,啟動時日志顯示 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 項目, pom 中的依賴包繁多,升級一個 jar 版本時,會引發(fā)新的沖突,調(diào)試許久。而 SpringBoot 接管了 jar 的版本,它構(gòu)建好一套,這套中各 jar 的版本已經(jīng)測試好,開發(fā)者再無需去關(guān)注每個依賴包的版本及沖突問題,從而簡化開發(fā)。

再者,它啟動也非常快,直接運行一個類,使用 tomcat 的 maven 插件。開發(fā)調(diào)試時效率提高。

熱部署支持

配置pom.xml

<!-- 熱部署支持 -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
  </dependency>

總結(jié)

以上所述是小編給大家介紹的使用SpringBoot整合ssm項目的實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • javaweb中ajax請求后臺servlet(實例)

    javaweb中ajax請求后臺servlet(實例)

    下面小編就為大家?guī)硪黄猨avaweb中ajax請求后臺servlet(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java多線程join()方法的作用和實現(xiàn)原理解析(應用場景)

    java多線程join()方法的作用和實現(xiàn)原理解析(應用場景)

    join方法主要是用于將當前線程掛起,等待其他線程結(jié)束后在執(zhí)行當前線程,本文通過應用場景分析代碼示例講解java多線程join()方法的作用和實現(xiàn)原理,感興趣的朋友一起看看吧
    2021-07-07
  • Java線程中的常見方法(start方法和run方法)

    Java線程中的常見方法(start方法和run方法)

    這篇文章主要介紹了Java線程中的常見方法(start方法和run方法),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • mybatis單元測試過程(無需啟動容器)

    mybatis單元測試過程(無需啟動容器)

    在MyBatis中,單元測試無需啟動容器即可進行,主要涉及Configuration類、Executor接口及其實現(xiàn)類,以及XMLMapperBuilder的作用,Configuration類是配置的承載者,負責初始化并解析配置文件,Executor接口及其實現(xiàn)類
    2024-09-09
  • Java8 Comparator排序方法實例詳解

    Java8 Comparator排序方法實例詳解

    這篇文章主要介紹了Java8 Comparator排序方法實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 深入解析Session工作原理及運行流程

    深入解析Session工作原理及運行流程

    這篇文章主要介紹了深入解析Session工作原理及運行流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Springboot+AOP實現(xiàn)時間參數(shù)格式轉(zhuǎn)換

    Springboot+AOP實現(xiàn)時間參數(shù)格式轉(zhuǎn)換

    前端傳過來的時間參數(shù),后端可以自定義時間格式轉(zhuǎn)化使用,這樣想轉(zhuǎn)成什么就轉(zhuǎn)成什么。本文將利用自定義注解AOP實現(xiàn)時間參數(shù)格式轉(zhuǎn)換,感興趣的可以了解一下
    2022-04-04
  • 淺談String.split()遇到空字符串的幾種情況

    淺談String.split()遇到空字符串的幾種情況

    這篇文章主要介紹了淺談String.split()遇到空字符串的幾種情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解

    Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解

    這篇文章主要介紹了Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 一文探尋Java裝箱和拆箱的奧妙

    一文探尋Java裝箱和拆箱的奧妙

    Java中的裝箱(boxing)和拆箱(unboxing)是指將基本數(shù)據(jù)類型與其對應的包裝類之間進行轉(zhuǎn)換的過程。本文就來帶大家探索一下Java裝箱和拆箱的奧妙吧
    2023-04-04

最新評論