导入Spring Boot依赖
1.SpringBoot入门在父节点添加parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
添加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
新建一个controller
@RestController
public class UserController {
@RequestMapping("/")
public String index(){
return "hello333331231";
}
}
新建一个启动类app
@SpringBootApplication(scanBasePackages = "cn.itsource")
//scanBasePackages = "cn.itsource扫描指定的包下。
//如果不加scanBasePackages会在当前文件的子类扫描
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
2.SpringBoot跳转Jsp
<!--必须有才能编译jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
配置application.properties对jsp支持
添加src/main/resources/application.properties:
#tomcat server port
server.port=80
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application
Yaml 方式
server:
port: 8080
name: kd
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
3.SpringBoot热启动
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
</dependency>
4.SpringBoot配置导包(jar)
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5.SpringBoot对Freemaker的支持
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
配置application.properties对Freemaker
# FreeeMarker 模板引擎配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
配置application.yml对Freemaker
spring:
profiles:
active: dev1
freemarker:
allow-request-override: false
cache: false
charset: UTF-8
check-template-location: true
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
6.SpringBoot测试
<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>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class) //这事一个Spring测试,要告诉它在哪儿加载Spring配置文件,其实告诉它应用类型就ok
public class SpringbootTest {
@Autowired
private TestService testService;
@Test
public void test() throws Exception {
System.out.println(testService);
testService.test();
}
}
7.Spring boot-mybtis
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-boot mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- spring boot mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
创建启动类App.java
@SpringBootApplication
@MapperScan("cn.itsource.springboot.mybatis.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
//这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口
(4)在application.properties添加配置文件;
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
Yaml 方式
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/spring-boot-demo?useUnicode=true&characterEncoding=utf-8
username : root
password : root
8.Spring boot-mybtis获取自增id
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("insert into Demo(name,password) values(#{name},#{password})")
public long save(Demo name);//对象上面也有
<intert id=""save" parameterType="User" userGeneratedKeys="true" keyProperty="id" keyColumn="id">
</intert>
9.模块化开发