SpringBoot个人小结

导入Spring Boot依赖

1.SpringBoot入门

在父节点添加parent

 
    		org.springframework.boot
    		spring-boot-starter-parent
    		2.0.5.RELEASE
    

添加spring-boot-starter-web依赖


		org.springframework.boot
		spring-boot-starter-web

新建一个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


		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			provided
		
		
配置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热启动

	
			org.springframework.boot
			spring-boot-devtools
			true 
		

4.SpringBoot配置导包(jar)


	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
			
		
	

5.SpringBoot对Freemaker的支持


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-freemarker
    

配置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测试


			org.springframework.boot
			spring-boot-starter-web


			org.springframework.boot
			spring-boot-starter-test

@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


			mysql
			mysql-connector-java
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.0
		
		
			
		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			1.2.2
		
创建启动类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);//对象上面也有


	

9.模块化开发

你可能感兴趣的:(SpringBoot个人小结)