- springboot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。SpringBoot是伴随着Spring4.0诞生的;从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架;SpringBoot继承了原有Spring框架的优秀基因;SpringBoot简化了使用Spring的过程。 - springboot的优点
改变了以前三层架构开发的配置繁琐,简化了配置,而且有springsource tool suite插件可以快速的搭建开发环境,注解功能十分强大。使部署更简单,只需一个jar包 - springboot的应用场景
大数据、高并发的电商项目 - springboot搭建
新建maven project,导入所需要的jar包
pom.xml文件内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>springBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</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.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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
结构(controller等要放在MySpringBootDemo同级的包或下一级包下面,启动主程序才能有效)
主程序入口,通过主程序来启动项目
package cn.itcast;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//必要的注解
public class MySpringBootDemo {
//项目入口
public static void main(String[] args) {
SpringApplication.run(MySpringBootDemo.class, args);
}
}
测试
import org.springframework.web.bind.annotation.RestController;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
@RestController//这个注解包含了以前的@Controller,@ResponseBody
public class UserController {
@Autowired
private UserService userService;
/**
* @return 基本数据类型json格式
*/
@RequestMapping("hello")
public String showHello(){
return "hello world!";
}
/**
* @return POJO
*/
@RequestMapping("pojo")
public User showUser(){
User u = new User();
u.setId(01);
u.setUsername("张飞");
u.setSex("男");
u.setAddress("燕");
return u;
}
/**
* @return 集合对象map
*/
@RequestMapping("maps")
public Map<String, Object> showMaps(){
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("username", "岳飞");
maps.put("age", "21");
return maps;
}
/**
* @return 集合对象list
*/
@RequestMapping("list")
public List<User> showList(){
//创建list集合对象
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setId(01);
u1.setUsername("张飞");
u1.setSex("男");
u1.setAddress("燕");
User u2 = new User();
u2.setId(02);
u2.setUsername("岳飞");
u2.setSex("男");
u2.setAddress("南宋");
list.add(u1);
list.add(u2);
return list;
}
}
结果图
5. springboot整合mybatis
总体结构
整合mybatis在application.properties文件
#springboot整合Mybatis框架
#加载Mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
#定义别名
mybatis.type-aliases-package = cn.itcast.domain
#数据源
spring.datasource.url = jdbc:mysql://localhost:3306/normal
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.username = root
spring.datasource.password = root
在pom.xml文件中加入以下
<!-- 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
整合完了,似不似很简单。
测试就跟以前的三层架构一样,这里就不列出来啦