Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。java
Spring Boot 如今已經成爲Java 開發領域的一顆璀璨明珠,它自己是包容萬象的,能夠跟各類技術集成。成爲SpringBoot全家桶,成爲一把萬能鑰匙。mysql
1.建立獨立的Spring應用程序web
2.嵌入的Tomcat,無需部署WAR文件spring
3.簡化Maven配置sql
4.自動配置Springjson
5.提供生產就緒型功能,如指標,健康檢查和外部配置tomcat
Spring官方支持SpringBoot提供的項目框架生成頁面springboot
https://start.spring.io/mybatis
在eclipse上建立springboot工程app
(jdk版本必須1.8以上,springboot基本上廢除了1.6、1.7)
eclipse版本也有要求,版本太低,建立的工程會報錯或者可使用springboot低版本。也可使用STS或IDEA,版本支持較好,下面演示用的是eclipse
1.建立Maven工程,建立simple project,類型爲jar
額外須要的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的版本已經測試好,開發者再無需去關注每一個依賴包的版本及衝突問題,從而簡化開發。
再者,它啓動也很是快,直接運行一個類,使用tomcat的maven插件。開發調試時效率提升。
配置pom.xml
<!-- 熱部署支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>