SpringBoot是什么?

  Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。

Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。成为SpringBoot全家桶,成为一把万能钥匙。

SpringBoot的特点

  1.创建独立的Spring应用程序

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

  3.简化Maven配置

  4.自动配置Spring

  5.提供生产就绪型功能,如指标,健康检查和外部配置

 

Spring官方支持SpringBoot提供的项目框架生成页面

https://start.spring.io/

使用SpringBoot整合ssm项目-LMLPHP

在eclipse上创建springboot工程

jdk版本必须1.8以上,springboot基本上废除了1.61.7)

eclipse版本也有要求,版本过低,创建的工程会报错或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持较好,下面演示用的是eclipse

 

简单的使用springboot整合ssm

 1.创建Maven工程,创建simple project,类型为jar

使用SpringBoot整合ssm项目-LMLPHP

pom.xml

额外需要的jar,还得自己依赖,例如:mysql驱动包,阿里的数据源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>

创建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 + "]";
    }

}

创建 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>

创建UserMapper接口

public interface UserMapper {
    //调用xml方式
    public List<User> find();

    //调用注解方式
    @Select("select * from user where id=#{id}")
    public User get(@Param("id") Integer id);
}

创建UserService接口

public interface UserService {
    public List<User> find();
    public User get(Integer id);
}

创建UserServiceImpl接口实现类

@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);
    }
}

创建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);
    }
}

创建application.yml

全局配置文件,yml为新的配置文件方式,注意其中格式为空格,不能有tab

配置端口,配置数据源,配置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

创建RunApplication.java

@SpringBootApplication
@MapperScan("cn.lmq.mapper")        //扫描Mybatis接口文件
public class RunApplication {
    public static void main(String[] args) {
        SpringApplication.run(RunApplication.class, args);
    }
}

初步整合完毕,比三大框架ssm好用太多了

传统构建Maven项目,pom中的依赖包繁多,升级一个jar版本时,会引发新的冲突,调试许久。而SpringBoot接管了jar的版本,它构建好一套,这套中各jar的版本已经测试好,开发者再无需去关注每个依赖包的版本及冲突问题,从而简化开发。

再者,它启动也非常快,直接运行一个类,使用tomcatmaven插件。开发调试时效率提高。

热部署支持

配置pom.xml

<!-- 热部署支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
11-08 12:10