crm开发定制java高级--SpringBoot篇

目录


一、什么是SpringBoot

crm开发定制大家想一想,crm开发定制在我们搭建springcrm开发定制应用程序时,crm开发定制你认为什么最麻烦。

  1. 配置xml文件麻烦。---springcrm开发定制的配置文件。web.xml

  2. 很多依赖。

  3. tomcatcrm开发定制启动速度很忙。

        Spring Boot是由Pivotalcrm开发定制团队提供的全新,crm开发定制其设计目的是用来新应crm开发定制用的初始搭建以及开发过程。crm开发定制该框架使用了特定的方crm开发定制式来进行配置【crm开发定制自动装配类】,crm开发定制从而使开发人员不再需crm开发定制要定义样板化的配置。crm开发定制通过这种方式,Spring Bootcrm开发定制致力于在蓬勃发展的快crm开发定制速应用开发领域(rapid application development)成为领导者。

        二、SpringBoot的特点

(1)可以创建独立的应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;

(2)内嵌Tomcat或Jetty等Servlet容器;

(3)提供自动配置的“starter”项目对象模型(POMS)以简化配置;

(4)尽可能自动配置Spring容器;--xml配置文件麻烦

(5)提供准备好的特性,如指标、健康检查和外部化配置;

(6)绝对没有代码生成,不需要XML配置。

        三、springboot快速入门

        具备条件:

                        JDK至少1.8以上。

                        maven至少3.2.5以上。

                        开发工具---idea [eclipse]

 

        要把pom文件中的版本号更改

        创建一个HelloController类,该类所在的包必须是主函数下的包或子包下。  

        四、介绍配置文件的种类

        springboot支持两种配置文件的类型:

        第一种: 属性文件[.properties] 格式: key=value

        第二种: yaml文件 [.yml] 格式: 层级关系 key: value

        不管是哪种,他们的前置必须都是application.

        属性文件properties:

  1. # 设置tomcat的端口号
  2. server.port=8888
  3. # 上下文访问路径
  4. server.servlet.context-path=/aaa

        yaml文件:  

  1. # 设置端口号
  2. server:
  3. port: 8887
  4. # 设置上下文路径
  5. servlet:
  6. context-path: /bbb

        上面两种配置文件可以同时存在,里面的内容如果相同,则properties的优先级高于yaml文件。  

        五、开发环境配置文件的切换

        我们在真实开发过程中:会存在很多环境,而且每个环境他们的配置文件的内容可能不同。

        比如: 开发环境 测试环境 线上环境。

        我就根据不同的环境给出不同的配置内容,你只需要切换环境---即可变成相应环境下的配置。

        定义多个环境下的不同配置文件: application-环境名.properties

        在application.properties配置文件中激活相应的配置

  1. # 引入相关的环境配置--激活开发环境
  2. spring.profiles.active=test
  3. # 写一些不同环境下相同的配置

        六、读取springboot配置文件中的内容

        springboot中提供了两种读取配置文件内容的方式

        第一种: 把读取的内容封装到实体类中

  1. @Autowired
  2. private AliPay aliPay;
  3. @RequestMapping("/index")
  4. public AliPay index(){
  5. return aliPay;
  6. }

        测试:

        第二种: 单个属性读取

 

        七、Springboot注册web三大组件

        web三大组件: [1] servlet 、[2] filter、 [3] 监听器 listene

        思考: servlet定义的步骤。

        (1) 创建一个类并继承HttpServlet

        (2) 重写service方法 【doGet 或doPost方法】

      (3)把自定义的servlet注册到web.xml容器中。

        我们现在springboot不存在web.xml文件了。

        注册Servlet组件

  1. public class MyServlet extends HttpServlet {
  2. @Override
  3. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  4. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
  5. }
  6. }
  1. package com.ykq.config;
  2. import com.ykq.servlet.MyServlet;
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import javax.servlet.http.HttpServlet;
  7. @Configuration //====>表示该类为配置类 等价于我们之前的xml文件
  8. public class MyConfig {
  9. @Bean //xml文件中<bean class=""> 把方法返回的对象交于spring容器来管理
  10. public ServletRegistrationBean<HttpServlet> registrationBean(){
  11. ServletRegistrationBean<HttpServlet> servletServletRegistrationBean=new ServletRegistrationBean<>();
  12. servletServletRegistrationBean.setName("my");
  13. servletServletRegistrationBean.addUrlMappings("/my");
  14. servletServletRegistrationBean.setServlet(new MyServlet());
  15. return servletServletRegistrationBean;
  16. }
  17. }

        注册Filter组件

        

        步骤: 1.定义一个过滤器类并实现Filter接口

                 2.重写接口中的抽象方法

                 3. 在web.xml文件中注册过滤器。

  1. public class MyFilter implements Filter {
  2. @Override
  3. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  4. System.out.println("过滤器编码");
  5. }
  6. @Override
  7. public void init(FilterConfig filterConfig) throws ServletException {
  8. }
  9. }
  1. //过滤的注册
  2. @Bean
  3. public FilterRegistrationBean<Filter> filterRegistrationBean(){
  4. FilterRegistrationBean<Filter> filterRegistrationBean=new FilterRegistrationBean<>();
  5. filterRegistrationBean.setName("encondigFilter");
  6. filterRegistrationBean.addUrlPatterns("/*");
  7. filterRegistrationBean.setFilter(new MyFilter());
  8. return filterRegistrationBean;
  9. }

        八、SpringBoot的底层原理

         8.1 包扫描的原理

        springboot创建的包必须在主类所在的包以及子包下,才可以被容器扫描到。

为什么会在主类所在的包以及子包下扫描?

在主函数中调用run方法,而run方法传递了一个被@SpringBootApplication注解修改的类的反射类对象 该@SpringBootApplication它是复合注解

        @EnableAutoConfiguration 开启自动配置的注解,而该注解也是一个复合注解。

        这里@AutoConfigurationPackage 该注解完成包的自动扫描。  

        通过上面的源码分析 我们得到默认扫描的是主类所在的包以及子包。  

        我们是否可以自定义包扫描。---可以

        8.2  springboot自动装配原理

        思考: 我们并没有配置DispatcherServlet。 为何为拦截请求路径到相应的mapping中。

        因为springboot在启动时会加载一个自动装配类,而这些自动装配会完成相应的自动装配功能。

        流程:

                (1)主函数调用run方法,而run方法加载了一个被@SpringBootApplication注解修饰的类。  

                而该注解是一个复合注解。其中包含@EnableAutoConfiguration该注解可以开启自动装配,而@EnableAutoConfiguration这个也是一个复合注解。其中包含@Import({AutoConfigurationImportSelector.class})而import注解导入了一个AutoConfigurationImportSelector类,该类用来加载需要的自动装配类。而这些自动装配类完成对应的自动装配功能。

        思考: 根据源码 我们看到上来加载了所有的自动装配了,而所有的自动装配类默认127个。这127个从哪来的。  

        分析: 自动装配类如何完成自动装配功能        

        (1)DipatchServletAutoConfiguration [可以完成DispatchServlet的自动装配功能

        发现传递中文没有乱码,而且返回的json数据中也没有出现乱码,因为springboot加载了编码过滤的自动装配了,而这个装配类完成了编码设置的自动装配。  

        九、自定义starter依赖

        如果想自定义Starter,首选需要实现自动化配置,而要实现自动化配置需要满足以下两个条件:

                能够自动配置项目所需要的配置信息,也就是自动加载依赖环境

                能够根据项目提供的信息自动生成Bean,并且注册到Bean管理容器中;

        9.1 创建一个springboot工程并引入相关得依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-autoconfigure</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-configuration-processor</artifactId>
  13. <optional>true</optional>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>

        9.2 创建一个属性类

                作用: 解析springboot配置文件中提供得内容。

  1. /**
  2. * Created by Intellij IDEA
  3. *
  4. * @author 王俊凯
  5. * @Date: 2022/11/17 14:44
  6. * @Version 1.0
  7. */
  8. package com.wjk;
  9. import org.springframework.boot.context.properties.ConfigurationProperties;
  10. @ConfigurationProperties(prefix = "spring.wjk")
  11. public class WjkProperties {
  12. private String name;
  13. private String address;
  14. private Integer age;
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public String getAddress() {
  22. return address;
  23. }
  24. public void setAddress(String address) {
  25. this.address = address;
  26. }
  27. public Integer getAge() {
  28. return age;
  29. }
  30. public void setAge(Integer age) {
  31. this.age = age;
  32. }
  33. }

        9.3 定义一个业务类

        作用: 完成相关得业务操作

  1. /**
  2. * Created by Intellij IDEA
  3. *
  4. * @author 王俊凯
  5. * @Date: 2022/11/17 14:46
  6. * @Version 1.0
  7. */
  8. package com.wjk;
  9. public class WjkService {
  10. private WjkProperties wjkProperties;
  11. public WjkService() {
  12. }
  13. public WjkService(WjkProperties wjkProperties) {
  14. this.wjkProperties = wjkProperties;
  15. }
  16. //业务代码
  17. public void hello(){
  18. System.out.println("姓名:"+wjkProperties.getName()+"地址:"+wjkProperties.getAddress()+"年龄:"+wjkProperties.getAge());
  19. }
  20. }

        9.4 定义一个自动装配类

        完成自动装配得功能。用于完成Bean创建等工作

  1. /**
  2. * Created by Intellij IDEA
  3. *
  4. * @author 王俊凯
  5. * @Date: 2022/11/17 14:49
  6. * @Version 1.0
  7. */
  8. package com.wjk;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  11. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  12. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  13. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.context.annotation.Configuration;
  16. @Configuration //该类为配置类
  17. @EnableConfigurationProperties(WjkProperties.class) //开启属性配置
  18. @ConditionalOnClass(WjkService.class) //只要wjkService被引后入,当中的这个类才会被创建
  19. @ConditionalOnProperty(prefix = "spring.wjk",value = "enabled",matchIfMissing = true) //如果引用者没有给定相关的属性值,则采用默认值
  20. public class WjkAutoConfiguration {
  21. @Autowired
  22. private WjkProperties wjkProperties;
  23. @Bean
  24. @ConditionalOnMissingBean
  25. public WjkService wjkService(){
  26. return new WjkService(wjkProperties);
  27. }
  28. }

        9.5 在resource下创建一个目录MATE-INF 里面创建一个文件名spring.factories

        org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aaa.YkqAutoConfiguration

        9.6 打包--maven 配置了本地仓库

        

        打包成功后,就可以使用一个新的工程,引入自定义的starter的依赖

        以上自定义starter依赖是一个简单的例子

        想看复杂的例子

        十、springboot整合数据源

        数据源:指操作数据库

        10.1 引入数据源得启动依赖

  1. <!--引入数据源依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-jdbc</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. </dependency>

        10.2 指定数据源得信息

  1. # 指定数据源信息
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai
  4. spring.datasource.username=root
  5. spring.datasource.password=wjk351066

        10.3 测试

  1. @SpringBootTest
  2. @MapperScan(basePackages = "com.wjk.dao")
  3. class Java111501ApplicationTests {
  4. /**
  5. * 该依赖自动装配类 帮你建好DataSource对象 交与容器管理
  6. */
  7. @Autowired
  8. private DataSource dataSource;
  9. @Test
  10. void contextLoads() {
  11. System.out.println(dataSource);
  12. }
  13. }

        10.4 springboot整合第三方数据源

        数据源: springboot默认整合得数据源HikariDataSource. 很多公司使用得可能是其他得数据源。比如Druid数据源 C3p0数据源。

        druid案例提供一个starter依赖。只需要引入该依赖。就会有相应得自动装配。

  1. <!--引入druid得数据源-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. <version>1.2.1</version>
  6. </dependency>
  1. # 指定数据源信息
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai
  4. spring.datasource.username=root
  5. spring.datasource.password=wjk351066
  6. spring.datasource.druid.initial-size=5
  7. spring.datasource.druid.max-active=10

        10.5 测试

  1. @SpringBootTest
  2. class Qy158Springboot01ApplicationTests {
  3. //数据源对象---自己有没有创建该类对象---因为你引入spring-boot-starter-jdbc启动依赖。
  4. //该依赖得自动装配类 帮你创建好DataSource对象 交于容器管理
  5. @Autowired
  6. private DataSource dataSource;
  7. @Test
  8. void contextLoads() throws SQLException {
  9. System.out.println(dataSource);
  10. }
  11. }

 

        十一、springBoot整合Mybatis

        11.1 引依赖

  1. <!--mybatis得整合启动依赖-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.2.0</version>
  6. </dependency>

        11.2 修改配置文件

  1. # 指定mybatis得配置内容
  2. mybatis.mapper-locations=classpath:/mapper/*.xml
  3. # 指定mybatis
  4. mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

        11.3 写一个实体类(省略)

        11.4 创建Dao接口

  1. package com.wjk.dao;
  2. import com.wjk.entity.Student;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. public interface StudentDao {
  6. public List<Student> selectAll();
  7. int insert(Student student);
  8. int delete(Integer id);
  9. int update(Student student);
  10. }

        11.5 映射文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.wjk.dao.StudentDao">
  6. <insert id="insert">
  7. insert into t_student values (null ,#{name},#{age},#{address})
  8. </insert>
  9. <update id="update">
  10. update t_student set name =#{name},age=#{age},address=#{address} where id=#{id}
  11. </update>
  12. <delete id="delete">
  13. delete from t_student where id=#{id}
  14. </delete>
  15. <select id="selectAll" resultType="com.wjk.entity.Student">
  16. select * from t_student
  17. </select>
  18. </mapper>

        11.6 在springboot启动类上加上--dao接口得扫描

        11.7 测试---必须为springboot得单元测试【不能使用之前junit得测试】

  1. @Resource
  2. private StudentDao studentDao;
  3. @Test
  4. void testSelectAll(){
  5. PageHelper.startPage(1,3);
  6. List<Student> list = studentDao.selectAll();
  7. System.out.println(list);
  8. }

        十二、springboot整合pageHelper

  1. <!--pagehelper分页插件得依赖-->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper-spring-boot-starter</artifactId>
  5. <version>1.4.2</version>
  6. </dependency>
  1. @Resource
  2. private StudentDao studentDao;
  3. @Test
  4. void testSelectAll(){
  5. PageHelper.startPage(1,3);
  6. List<Student> list = studentDao.selectAll();
  7. PageInfo<Student> pageInfo = new PageInfo<>(list);
  8. System.out.println("总条数:"+pageInfo.getTotal());
  9. System.out.println("当前页的条数:"+pageInfo.getList());
  10. }

        十三、springboot整合定时器

        什么是定时器?  在指定的时间间隔内执行相关的任务。

        应用场景:

                比如: 未支付取消订单。

                定时删除OSS中冗余的文件。

                注册成功后,在七天发送短信问候。

        13.1 引入定时器的依赖

  1. <!--引入定时器的依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-quartz</artifactId>
  5. </dependency>

        13.2 定义定时器的业务类

             在线Cron表达式生成器的链接:

  1. package com.wjk.quartz;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. @Component //交与容器管理
  5. public class MyQuartz {
  6. /**
  7. * 业务代码 Scheduled规定什么时候执行业务代码
  8. */
  9. @Scheduled(cron = "0/5 * * * * ? ")
  10. public void test(){
  11. System.out.println("********************");
  12. }
  13. }

        13.3 开启定时器的注解

        十四、 springboot整合swagger2

        什么是swagger2?   使用Swagger开源和专业工具集简化用户、团队和企业的API开发。了解Swagger如何帮助您大规模设计和记录API文档

        为什么使用API文档?

 

        14.1 我们如何使用swagger2?   springboot的版本不能那么高。2.2~2.5之间

  1. <!--引入swagger2的依赖-->
  2. <dependency>
  3. <groupId>com.spring4all</groupId>
  4. <artifactId>swagger-spring-boot-starter</artifactId>
  5. <version>1.9.1.RELEASE</version>
  6. </dependency>
  7. <!--swagger图形化界面-->
  8. <dependency>
  9. <groupId>com.github.xiaoymin</groupId>
  10. <artifactId>swagger-bootstrap-ui</artifactId>
  11. <version>1.9.6</version>
  12. </dependency>

        14.2 创建一个swagger配置类---所有的功能都集成在Docket类

  1. @Configuration //标记为配置类
  2. public class SwaggerConfig {
  3. @Bean
  4. public Docket docket(){
  5. Docket docket = new Docket(DocumentationType.SWAGGER_2)
  6. return docket;
  7. }
  8. }

        14.3 开启swagger2注解

  1. package com.wjk;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  6. @SpringBootApplication
  7. @EnableScheduling //开启定时器注解
  8. @EnableSwagger2 //开启swagger2的注解
  9. public class SpringBoot1118Application {
  10. public static void main(String[] args) {
  11. SpringApplication.run(SpringBoot1118Application.class, args);
  12. }
  13. }

        14.4 定义相关的接口

  1. @RestController
  2. public class HelloController {
  3. @GetMapping("hello01")
  4. public String hello01(){
  5. return "hello01";
  6. }
  7. }

        14.5 查看swagger2的界面

        14.6 最终的配置类完整格式

  1. /**
  2. * Created by Intellij IDEA
  3. *
  4. * @author 王俊凯
  5. * @Date: 2022/11/18 10:06
  6. * @Version 1.0
  7. */
  8. package com.wjk.config;
  9. import com.google.common.base.Predicates;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import springfox.documentation.builders.PathSelectors;
  13. import springfox.documentation.builders.RequestHandlerSelectors;
  14. import springfox.documentation.service.ApiInfo;
  15. import springfox.documentation.service.Contact;
  16. import springfox.documentation.service.VendorExtension;
  17. import springfox.documentation.spi.DocumentationType;
  18. import springfox.documentation.spring.web.plugins.Docket;
  19. import java.util.ArrayList;
  20. @Configuration //标记为配置类
  21. public class SwaggerConfig {
  22. @Bean
  23. public Docket docket(){
  24. Docket docket = new Docket(DocumentationType.SWAGGER_2)
  25. //组名
  26. .groupName("wjk")
  27. //api信息
  28. .apiInfo(getInfo())
  29. //选择哪些生成api接口--根据请求路径选择 (2)根据包名选择
  30. .select()
  31. //根据请求路径选择
  32. .paths(Predicates.and(PathSelectors.regex("/api/.*")))
  33. //根据报名选择
  34. .apis(RequestHandlerSelectors.basePackage("com.wjk.controller"))
  35. .build()
  36. ;
  37. return docket;
  38. }
  39. private ApiInfo getInfo(){
  40. Contact DEFAULT_CONTACT = new Contact("王俊凯", "http://www.jd.com", "1315272325@qq.com");
  41. ApiInfo info = new ApiInfo("在线预约挂号系统", "在线挂号系统", "9.9", "http://www.baidu.com",
  42. DEFAULT_CONTACT, "上海富有银行有限公司", "http://www.taobao.com", new ArrayList<VendorExtension>());
  43. return info;
  44. }
  45. }

        我们还有另外一个网址  更简洁更容易看懂           

 

介绍: swagger常用的注解

  • @Api:修饰整个类,描述Controller的作用

  • @ApiOperation:描述一个类的一个方法,或者说一个接口

  • @ApiImplicitParam:一个请求参数

  • @ApiImplicitParams:多个请求参数

  • @ApiModel:用对象来接收参数

  • @ApiModelProperty:用对象接收参数时,描述对象的一个字段

         十五、thymeleaf模板引擎

        就是一个网页模板【jsp 现在不能使用jsp的原因:springboot中内置的tomcat 不支持jsp模板引擎】。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  1. @RestController
  2. public class PageController {
  3. @RequestMapping("/index")
  4. public String index(){
  5. return "index";
  6. }
  7. }

 

           具体使用请看文档  懒得写了

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发