SpringBoot框架-介绍及使用

1.springboot:用来简化spring应用的初始搭建以及开发过程

Spring Boot从根本上讲其实就是一些maven库的集合,在maven项目中导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本.

2.Spring Boot入门使用

2.1创建maven项目,在父模块中导入依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
    
    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                2.0.5.RELEASE
                pom
                import
            
        
    

引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。

2.2创建普通子模块,在子模块中导入依赖


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

2.3编码测试

新建一个Controller类
@RestController(类上)=@Controller(类上)+@RequestMapper(方法上)

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

新建启动类(App – Main方法)

@SpringBootApplication
public class SpringbootApp {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApp.class);
    }
}

运行main方法,输入网址

3.对FreeMark支持

3.1新建子模块,导入依赖


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

3.2在resource中配置application.properties文件

# FreeeMarker 模板引擎配置
# 设定ftl文件路径---一定要是templates
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

3.3在resource中创建templates文件夹,其中创建模板index.ftl




    Hello World!


你是 ${msg}

3.4新建controller类

@Controller
public class FreemarkController {

    @RequestMapping("/index")
    public String freemarkTest(Model model){
        //为模板添加数据
        model.addAttribute("msg", "蛤蟆皮");
        return "index";
    }
}

新建启动类,启动即可

4.Spring Boot 持久化

4.1集成原生jdbc

4.1.1新建模块,导入依赖,Maven依赖,mysql,jdbc


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

        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

4.1.2数据库信息配置application.properties

#配置连接mysql的datasourde
spring.datasource.url = jdbc:mysql://localhost:3306/springboot
spring.datasource.username = root
spring.datasource.password = 92597
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

4.1.3完成三层结构,持久层为dao层

dao层实现类中注入JdbcTemplate
service层中直接添加事务注解即可,Spring Boot已经集成了事务

//类上
@Transactional(propagation = Propagation.SUPPORTS)
//增删改方法上
@Transactional(propagation = Propagation.SUPPORTS,readOnly = false)

4.1.4新建启动类,需要扫描包

@SpringBootApplication
@ComponentScan("cn.zr")
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class);
    }
}

4.2集成mybatis

4.2.1新建模块,导入,maven依赖,mysql驱动,mybatis依赖包,mysql分页PageHelper


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

        
        
            mysql
            mysql-connector-java
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
        
            com.github.pagehelper
            pagehelper
            4.1.0
        

4.2.2完成数据库信息配置

4.2.3完成三层结构,持久层为mapper层

可以通过注解写sql
也可以通过mapper.xml写sql

4.2.4新建启动类,需要扫描mapper

@SpringBootApplication
//扫描mapper包
@MapperScan("cn.zr.mapper")
@ComponentScan("cn.zr")
public class MybatisApp {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApp.class);
    }
}

4.2.5如果使用mapper,xml,可以在application.properties中配置别名

mybatis.type-aliases-package=cn.zr.domain

4.2.6使用PageHelper分页

新建分页信息类,配置分页信息

package cn.zr.pageconfig;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration //相当于我们建了applicationContext-xxx.xml 
public class MyBatisConfiguration {

    //@Bean:相当于配置了一个bean
    /*
        
        
        
      
    */
    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}


在service层实现类中的方法里添加分页信息

public List queryPage() {
        //分页信息
        PageHelper.startPage(1, 2);
        return userMapper.query();
    }

使用分页还需要在启动类上扫描分页信息类,或者直接扫描所有的类

//扫描mapper包
@MapperScan("cn.zr.mapper")
@ComponentScan("cn.zr")
public class MybatisApp {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApp.class);
    }
}

你可能感兴趣的:(SpringBoot框架-介绍及使用)