SpringBoot

基本介绍

SpringBoot主要功能为简化配置

内置Sping能整合的所有第三方框架

内置Tomcat、Jetty、Undertow等Web服务器

代码实现

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
@RestController
@SpringBootApplication
@Configuration
public class UserController {
    @GetMapping("/method01")
    public Object method01() {
        return "buliyat";
    }
    public static void main(String[] args) {
        SpringApplication.run(UserController.class, args);
    }
}

开发代码

POM

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
​
    <groupId>com.qfedu.edu</groupId>
    <artifactId>com-buliyat-qcy-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>com-buliyat-qcy-springboot</name>
    <description>com-buliyat-qcy-springboot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

启动类

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

Controller

@RestController
public class UserController {
    @RequestMapping("/method01")
    public String method01() {
        return "/buliyat.html";
    }
}

AppConfig

@SpringBootConfiguration
@ComponentScan(basePackages = {"com.qfedu.edu"})
public class AppConfig {}

全局配置

yml申明键值对

username01: "I am buliyat \n heihei"    # 会换行
username02: 'I am buliyat \n heihei'    # 不会换行
username03:  I am buliyat \n heihei     # 会换行

yml申明对象

user01:
  username: buliyat
  password: 123
​
user02: {"username":"buliyat","password":"111"}

yml申明数组

user01:
  habbits:
    - 男
    - 女
    - 其他
​
user02:
  habbits: ["boy","girl","other"]

开发模式

spring:
  profiles:
    active: test
server:
  port: 8000
spring:
  profiles: dev     # 开发环境
  application:
    name: dev-pro
​
---
server:
  port: 9000
spring:
  profiles: test    # 测试环境
  application:
    name: test-dev
​
---
server:
  port: 10000
spring:
  profiles: prod    # 生产环境
  application:
    name: prod-server

自动配置

操作类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class NetworkService {
    
    private String host;
    private Integer port;
​
    public void print(){
        System.out.println("HOST:"+host+"----port:"+port);
    }
}

属性抽取

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "qcy.framwork")
public class NetworkServiceProperties {
    
    private String host;
    private Integer port;
}

自动配置类

@Configuration                                  // 表示是一个配置文件
@ConditionalOnClass({NetworkService.class})     // 判断类路径下是否有这个类存在
@EnableConfigurationProperties(NetworkServiceProperties.class) // 引入配置文件
public class NetworkServiceAutoConfiguration {
​
    @Bean
    public NetworkService networkService(NetworkServiceProperties networkServiceProperties){
        return new NetworkService(networkServiceProperties.getHost(),networkServiceProperties.getPort());
    }
}

resources下创建META-INF文件夹 编写spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.qfedu.framwork.auto.NetworkServiceAutoConfiguration

导包

<dependency>
    <groupId>org.qcy.framwork</groupId>
    <artifactId>buliyat-network-framwork</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>
​
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Controller

@RestController
public class NetController {
​
    @Autowired
    private NetworkService networkService;
​
    @GetMapping("/test")
    public void test() {
        networkService.print();
    }
}

启动类

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

注解代替配置

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import({NetworkServiceAutoConfiguration.class})
public @interface EnableNetworkService {}
@SpringBootApplication
@EnableNetworkService
public class Application{
    public static void main(String[] args){
        
        SpringApplication.run(Application.class,args);
    }
}

常用配置

@SpringBootConfiguration
@ComponentScan(basePackages = {"com.buliyat.qcy"})
public class AppConfig extends WebMvcConfigurationSupport {
​
    // 添加拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
​
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
​
                return true; // 拦截与否
            }
        });
        super.addInterceptors(registry);
    }
​
    // 消息转换器
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("ISO_8859-1"));
        return fastJsonHttpMessageConverter;
    }
​
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        return stringHttpMessageConverter;
    }
    
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("ISO-8859-1"));
        converters.add(fastJsonHttpMessageConverter);
​
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        converters.add(stringHttpMessageConverter);
​
        super.configureMessageConverters(converters);
    }
}

MyBatis

导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.0</version>
</dependency>
​
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>
​
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
​
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

全局配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    url: jdbc:mysql:///user
    driver-class-name: com.mysql.jdbc.Driver
​
mybatis:
  type-aliases-package: com.buliyat.qcy.pojo
  mapper-locations: mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

AppConfig

@SpringBootConfiguration
@ComponentScan(basePackages = {"com.buliyat.qcy"})      // Spring的扫描
@MapperScan(basePackages = {"com.buliyat.qcy.mapper"})  // 设置接口所在的位置
public class AppConfig {}

Controller

@Controller
public class UserController {
​
    @Autowired
    private IUserService userService;
​
    @GetMapping("/findAll")
    public Object findAll() {
        return userService.findAll();
    }
}

Service

@Service
public class UserService implements IUserService {
​
    @Autowired
    private UserMapper userMapper;
​
    @Override
    public List<User> findAll() {
        return userMapper.userList();
    }
}

Mapper

@Repository
public interface UserMapper {
    
   List<User> userList();
}

Mapper.xml

<mapper namespace="com.buliyat.qcy.mapper.UserMapper">
    <select id="userList" resultType="user">
        select * from t_user
    </select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值