1、springbootSpring Boot知识点大纲 Spring 发展史 Spring Boot 简介 Spring Boot 环境搭建 Spring Boot 整合mybatis Spring Boot 开发 Spring Boot 整合redisSpring 发展史Spring1.x时代在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。Spring2.x时代随着JDK 1.5带来的注解支持,Spring2.x可以使用注解对Bean进行声明和注入,大大的减少了xml配置文件,同
2、时也大大简化了项目的开发。那么,问题来了,究竟是应该使用xml还是注解呢?最佳实践:1、 应用的基本配置用xml,比如:数据源、资源文件db.properties等;2、业务开发用注解,比如:Service中注入bean等;Spring3.x到Spring4.x从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x和Springboot都推荐使用java配置的方式。Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置。Configuration和Bean和ComponentScanSp
3、ring的Java配置方式是通过Configuration 和 BeanComponentScan这两个注解实现的:1、Configuration作用于类上,相当于一个xml配置文件;2、Bean作用于方法上,相当于xml配置中的标签;3、ComponentScan作用于类上,扫描包;创建Maven项目添加依赖 org.springframework spring-webmvc 4.1.6.RELEASE com.alibabadruid1.0.14 $project.artifactId org.apache.maven.plugins maven-resources-plugin UTF-
4、8 org.apache.maven.plugins maven-compiler-plugin 1.7 1.7 UTF-8 org.apache.tomcat.maven tomcat7-maven-plugin 2.2 定义一个实体类package com.bjsxt.springboot.javaconfig;publicclass User private String username;private String password;private Integer age;public String getUsername() returnusername; publicvoid s
5、etUsername(String username) this.username = username; public String getPassword() returnpassword; publicvoid setPassword(String password) this.password = password; public Integer getAge() returnage; publicvoid setAge(Integer age) this.age = age; public User(String username, String password, Integer
6、age) super(); this.username = username; this.password = password; this.age = age; public User() super(); / TODO Auto-generated constructor stub 定义一个类UserDao,在类中添加一个方法.packagecom.bjsxt.springboot.javaconfig;import java.util.ArrayList;import java.util.List;/* ClassName:UserDao* Description:* Company:
7、北京尚学堂科技有限公司 * authormengqx* date 2017年9月14日*/publicclass UserDao public List queryUserList() Listresult = new ArrayList(); / 模拟数据库的查询 result.add(new User( admin, 123,12); result.add(new User( 马蓉, 不离婚,38); result.add(new User( 宋哲, 被抓了,28); returnresult; 定义一个类UserService,调用UserDao类中的方法.packagecom.bjsx
8、t.springboot.javaconfig;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/* ClassName:UserService* Description:* Company: 北京尚学堂科技有限公司 * authormengqx* date 2017年9月14日*/Servicepublicclass UserService Autowired/ 注入Spring容器
9、中的bean对象private UserDao userDAO;public List queryUserList() / 调用userDAO中的方法进行查询returnthis.userDAO.queryUserList(); 编写添加配置文件类package com.bjsxt.springboot.javaconfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframewor
10、k.context.annotation.Configuration;/* ClassName:SpringConfig* Description:* Company: 北京尚学堂科技有限公司 * authormengqx* date 2017年9月14日*/ConfigurationComponentScan(basePackages = com.bjsxt.springboot.javaconfig)publicclass SpringConfig Bean/ 通过该注解来表明是一个Bean对象,相当于xml中的 public UserDao getUserDAO() returnnew
11、UserDao(); / 直接new对象做演示 测试:package com.bjsxt.springboot.javaconfig;import java.util.List;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/* ClassName:Main* Description:* Company: 北京尚学堂科技有限公司 * authormengqx* date 2017年9月14日*/publicclass Main publicstaticvoid main(Stri
12、ng args) / 通过Java配置来实例化Spring容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); / 在Spring容器中获取Bean对象 UserService userService = context.getBean(UserService.class); / 调用对象中的方法 Listlist = userService.queryUserList(); for (User user : list) System
13、.out.println(user.getUsername() + , + user.getPassword() + , + user.getPassword(); / 销毁该容器 context.destroy(); Spring Boot简介什么是SpringBootSpring Boot的优缺点官网:http:/projects.spring.io/spring-boot/Spring Boot环境搭建创建maven项目导入springboot依赖4.0.0com.bjsxtspringdeom0.0.1-SNAPSHOT org.springframework.boot spring-
14、boot-starter-parent 1.5.7.RELEASE org.springframework.boot spring-boot-starter-web 创建springboot主程序入口Spring boot 主程序类,必须在包的顶层。package com.bjsxt;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.anno
15、tation.RequestMapping;/* ClassName:SpringBootDemo* Description:* Company: 北京尚学堂科技有限公司 * authormengqx* date 2017年9月7日*/SpringBootApplicationpublicclass SpringBootDemo publicstaticvoid main(String args) / 启动springboot应用程序同时,同时启动tomcat。 SpringApplication.run(SpringBootDemo.class, args); 创建一个controller返
16、回基本数据类型:/RestController 即可用代理restful风格,同时也有Controller功能,还有ResponseBody的功能RestControllerpublicclass UserController RequestMapping(index) public String index() returnhello Stringboot; 返回实体类对象RequestMapping(getUser) public User getUser() returnnew User(1, admin, 123); 返回list集合对象,同时也可以返回一个map集合RequestMa
17、pping(lists) public List findAll() Listlist = new ArrayList(); list.add(new User(1,0张三丰,123); list.add(new User(2,1张三丰,123); list.add(new User(3,2张三丰,123); list.add(new User(4,3张三丰,123); returnlist; SpringBoot+mybatis创建目录结构添加mybatis的依赖 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql
18、mysql-connector-java 5.1.39 创建mapper,service,controllerpublicinterface UserMapper List findAll();publicinterface UserService List findAll();Servicepublicclass UserServiceImpl implements UserService Autowired private UserMapper userMapper; Override public List findAll() returnuserMapper.findAll(); Re
19、stControllerpublicclass UserController Autowired private UserService userService; /* * * Description:整合mybatis的查询所有数据。 * return * authormengqx * date 2017年9月7日 */ RequestMapping(getAll) public List getAll() Listlist = userService.findAll(); returnlist; 创建Mapper的映射文件 select * from t_user order by id;
20、 创建数据库连接配置文件application.propertiesapplication.properties是spring boot 项目的核心配置文件,xxx.yml这种配置文件。spring.datasource.url=jdbc:mysql:/localhost:3306/mybatisspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver#mybaits.type-aliases-package=co
21、m.bjsxt.pojomybatis.mapper-locations = classpath:mapper/*Mapper.xml测试:新添加一个注解扫描MapperScan(com.bjsxt.mapper)SpringBootApplicationMapperScan(com.bjsxt.mapper)publicclass SpringBootDemo publicstaticvoid main(String args) / 启动springboot应用程序同时,同时启动tomcat。 SpringApplication.run(SpringBootDemo.class, args)
22、; 修改数据库连接配置文件application.propertiesspring.datasource.url=jdbc:mysql:/localhost:3306/mybatisspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver测试:SpringBootApplicationMapperScan(com.bjsxt.mapper)publicclass SpringBootDemo publicstaticvoid main(String args) / 启动springboot应用程序同时,同时启动tomcat。 SpringApplication.run(SpringBootDemo.class, args); Mybatis-注解开发:不推荐使用publicinterface UserMapper Select(select * from t_user order by id) List findAll();Mapper和MapperScan(com.bjsxt.mapper)比较Mapper:放在接口上。每个接口必须有自己的Mapper注解Mappe
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1