注意:
创建springboot框架时mybatis springboot依赖问题,支持1.3.1,1.3.0会报大错
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
springboot
1.1介绍
1.2特性
独立
web容器
简化依赖
自动配置
1.3springboot手动搭建
helloController测试
1.4全局配文件application.properties
.properties
.yml
1.5自动配置的原理
1.6持久层框架
1.7order-user整个springboot框架
2.开发一个分布式的纵向拆分的系统
2.开发一个分布式的纵向拆分的系统
order-user的demo案例切分成两个独立运行的相互可以访问的web系统
order:负责支付
user:负责积分查询和更新
1.springboot概述
1.1介绍:springboot是一个基于spring框架的工具框架,可以实现spring容器的快速搭建快速配置快速启动
1.2特性
独立运行的spring容器
可以实现通过main方法作为工程的一个cpu运行入口,启动一个spring的容器,容器中各种的bean对象创建完成
内嵌的servlet容器,
没有web应用servlet容器也存在,默认是tomcat(1.5.9.RELEASE版本的springboot内嵌tomcat8.5),整合springmvc非常的简单,将springmvc依赖引入就会根据自动配置检测当前的工程是否是一个web应用,而启动tomcat容器
1.步骤
1.1创建maven工程quickstart
1.2pom文件修改
继承springboot-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
starter-web开发一个web应用(没有持久层)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.3启动类
核心注解@springBootApplication
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* springboot核心注解
* 启动一个spring的运行容器
* @author 在路上
*
*/
@SpringBootApplication
public class StarterDemo1 {
/**
* 启动main方法
*/
public static void main(String[] args) {
/*
* source:Class 表示当前启动类的反射对象
*/
SpringApplication.run(StarterDemo1.class, args);
}
}
1.4controller-server(hello word)
案例:
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* springboot核心注解
* 启动一个spring的运行容器
* @author 在路上
*
*/
//此处的package是cn.tedu所以只扫描cn.tedu包
@SpringBootApplication
public class StarterDemo1 {
/**
* 启动main方法
*/
public static void main(String[] args) {
/*
* source:Class 表示当前启动类的反射对象
*/
SpringApplication.run(StarterDemo1.class, args);
}
}
package cn.tedu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.component.HelloComponent;
import cn.tedu.service.HelloService;
@RestController
public class HelloController {
public HelloController(){
System.out.println("controller创建成功");
}
@Autowired
private HelloService helloService;
//required表示当前注入使用是否是强制的
//true必须注入使用
//false有则注入,无则不注入
@Autowired(required=false)
private HelloComponent helloComponent;
/*
* springmvc的annotation-driven开启注解
*/
@RequestMapping("/hello")
public String sayHi(String name){
return "controller"+helloService.sayHi(name);
}
}
package cn.tedu.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public HelloService(){
System.out.println("service创建成功");
}
public String sayHi(String name) {
return "你好"+name;
}
}
package com.alibaba.component;
import org.springframework.stereotype.Component;
@Component
public class HelloComponent {
public HelloComponent(){
System.out.println("com.alibaba的component创建成功");
}
}
springboot的全局配置文件
server转yml的在线网址
https://toyaml.com/index.html
案例:
实现0xml是将xml转化为注解在Java文件里书写,一个标签就是一个对象,标签下的属性就是对象的属性
package cn.tedu.config01;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.component.HelloComponent;
@Configuration
public class HelloConfig {
//@bean注解生成一个初始化方法,返回一个bean对象
@Bean
public HelloComponent initH(){
return new HelloComponent();
}
}
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* springboot核心注解
* 启动一个spring的运行容器
* @author 在路上
*
*/
//此处的package是cn.tedu所以只扫描cn.tedu包
//@SpringBootApplication
@SpringBootConfiguration
@ComponentScan(value="com.haha")//扫描com.haha包但是没有,只有cn.tedu,所以啥也扫不到,也不会出现默认的8080端口,或者配置文件里的端口
@EnableAutoConfiguration
public class StarterDemo1 {
/**
* 启动main方法
*/
public static void main(String[] args) {
/*
* source:Class 表示当前启动类的反射对象
*/
SpringApplication.run(StarterDemo1.class, args);
}
}
1.5根据依赖的内容实现加载bean对象
@configuration 表示一个类是一个配置类相当于一个xml配置文件
@bean 表示方法返回值需要spring挂载 ioc di
@conditional衍生注解 (根据依赖做条件的核心内容)可以使用条件注解判断当前的配置类是否生效加载
条件注解使用在配置类(configuration配置使用),通过对条件的设定,判断当前配置类局部或者去全部是否需要加载
@Configuration
//没有这个类不会运行报错,但是下边的内容不会执行
@ConditionalOnClass({ConditionA.class})
public class ConditionConfig {
@Bean
//当前环境必须具备某些Bean对象才会加载这个方法
@ConditionalOnBean(HelloController.class)
public ConditionBeanA initCBA(){
return new ConditionBeanA();
}
}
1.6springboot的整合自动配置
@EnableAutoConfiguration
springboot提供的大量的扩展的自动配置类并不满足自定义工程扫描范围,扫描加载这些配置类的机制,第三方实现自动配置整合springboot
spring.factories(SpringFactoriesLoader)保存的是当前autoconfigure包中所有的配置类的全路径名称
可以根据依赖的内容存在自动配置的jar包实现导入功能
@ConfigurationProperties(prefix=“spring.mvc”)
一旦属性类有有这个注解,有**properties注解(ConfigurationProperties)会根据注解实现读取properties文件中的内容(按照前缀读取)
3.支持多级读取
ConfigurationProperties(prefix=1906)
1906.a.className
@Value("${spring.mvc.view.prefix}")
总结主动配置与案例
springboot和持久层整合
1.依赖
springboot-jdbc定义所有的接口规范
mysql定义连接的后端数据库类型
mybatis的springboot的引入
数据源 DruidDatasource:默认springboot使用的底层连接池Tomcat JDBC datasource
1.4测试案例
Usercontroller UserService UserMapper
UserMapper.xml User
代码实现:
package cn.tedu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* springboot核心注解
* 启动一个spring的运行容器
* @author 在路上
*
*/
//此处的package是cn.tedu所以只扫描cn.tedu包
@SpringBootApplication
@MapperScan("cn.tedu.mapper")
public class StarterDemo1 {
@Bean
public String newString(){
return new String("alj");
}
/**
* 启动main方法
*/
public static void main(String[] args) {
/*
* source:Class 表示当前启动类的反射对象
*/
SpringApplication.run(StarterDemo1.class, args);
}
}
package cn.tedu.domain;
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
public User(Integer id, String username, String password, String nickname, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickname=" + nickname
+ ", email=" + email + "]";
}
}
package cn.tedu.mapper;
import cn.tedu.domain.User;
public interface UserMapper {
User selectUserById(Integer id);
}
package cn.tedu.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.domain.User;
import cn.tedu.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryOne(Integer id) {
return userMapper.selectUserById(id);
}
}
package cn.tedu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.tedu.domain.User;
import cn.tedu.service.UserService;
//@Controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="query",method=RequestMethod.GET)
public User queryOneById(Integer id){
return userService.queryOne(id);
}
}
mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.UserMapper">
<select id="selectUserById" parameterType="int" resultType="User">
select * from user where id=#{id};
</select>
</mapper>
pom
<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.tedu</groupId>
<artifactId>springboot-demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo1</name>
<url>http://maven.apache.org</url>
<!-- 当前工程是一个子工程 继承父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--web应用的简化依赖starter-web -->
<!--log jackson embed tomcat spring context beans springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc协议 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<!--springboot mybaits -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
全局配置文件:
#8091
server.port=8091
server.contextPath=/1906
1906.class-name=big1906;
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///easymall?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis.typeAliasesPackage=cn.tedu.domain
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.cacheEnabled=false
#mapperscanner
1.5druid
默认的springboot的底层datasource tomcat jdbc datasource
application.properties 中指定一个连接数据源的实现类的全路径名称
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
2.1引入工程的maven springboot插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将整个jar包复制到桌面
在cmd窗口中运行java -jar加此jar包的绝对路径会出现eclipse里边运行的同样效果(根本原因是装了jdk)
但是在linux里不会有这种现象
springboot整合order user系统
1.ssm框架转向springboot
静态页面交由nginx管理,动态页面交由tomcat,更改nginx的配置文件
springboot 将项目 order-user纵向拆分
1.纵向拆分的目的
根据不同的功能,不同的业务逻辑,实现独立的拆分运行
1.1多个独立运行的系统搭建步骤重复
springboot创建
1.2多个系统间的访问调用
RestTemplate对象
2.功能
2.1用户功能
页面整合
访问www.ssm.com 以/user起始的请求地址转发给8090
更改proxy_pass的地址(防止两个地址一样,那就没必要纵向切分)
代码实现
2.2订单功能
springboot总结
1、pom的配置
引入parent标签,继承父类
引入依赖
web应用 (spring-boot-starter-web)
jdbc协议(spring-boot-starter-jdbc)
mysql(mysql-connector-java)
mybatis springboot(mybatsi-spring-boot-starter)注意:依赖不支持1.3.0
druid(druid)
如果要在服务器里边运行需要打成jar包,还需多写一个build标签
2.application全局文件的配置
端口
驱动
url
username
password
数据源实现类
别名包
映射文件
驼峰命名(mapUnderscoreToCamelCase)
二级缓存关闭(cacheEnabled)
3.controller
service
mapper
domain
mapper.xml