笔者自学的框架,知识点来自网上学习教程,方便实惠,推荐,特此声明。
(但是没有很多细节,真的很伤啊学起来)
sringboot官网
- springbootGitHub地址:https://github.com/spring-projects/spring-boot
- springboot官方文档:https://spring.io/guides/gs/spring-boot
springboot配置文件
- pom.xml
springboot热部署
使得springboot支持jsp,让springboot编译webapp下文件,同时在Project Structure中设置web.xml<!-- springboot 开发自动热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
在built标签下加入<!--springboot tomcat jsp 支持开启--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>compile</scope> </dependency>
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources>
- application.properties
例可以设置几种不同的环境#字符编码位置要放到下面中文的上面,下面是指定字符编码 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true #修改内置tomcat端口号 server.port=8080 #设置项目上下文 server.servlet.context-path=/myspringboot #设置开发环境得配置文件,优先级比application.properties高 spring.profiles.active=dev #自定义配置 author.name=java author.address=广东 author.age=18
让springboot支持jsp开发环境:application-dev.properties 测试环境:application-test.properties 生产环境:application-online.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp #这个是关闭thymeleaf缓存,不是很必要 spring.thymeleaf.cache=false #关闭thymeleaf模板,不是很必要 spring.thymeleaf.enabled = false
springboot注解
- @SpringBootApplication
里面的@SpringBootApplication注解是springboot的核心注解,主要作用是开启spring自动配置。使用这个注解相当于加上了下面三个注解:- @Configuration 允许将其他@bean注解标识的类加入到spring容器中,相当于spring配置文件中的beans标签
- @EnableAutoConfiguration 启动自动配置
- @ComponentScan 会自动扫描当前包和子包下的标有@Component,@Service,@Repository,@Controller的类。相当于以前spring配置文件中的context:component-scan
- @RequestParam @RequestBody 总结 总结来源
- @RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
RequestParam可以接受简单类型的属性,也可以接受对象类型。
实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。 - @RequestBody
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
•GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
•POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。 - 总结
•在GET请求中,不能使用@RequestBody。
•在POST请求,可以使用@RequestBody和@RequestParam,但是如果使用@RequestBody,对于参数转化的配置必须统一。
- @RequestParam
- 读取配置文件自定义配置
2.1 自定义配置的数值可以用@value读取
2.2 读取配置文件@Component @ConfigurationProperties @PropertySource@Value("${author.name}") private String author;
如果配置信息在外置配置文件里则可以通过读取配置文件的方式读取信息@Component @PropertySource(value = "classpath:author.properties")//指定外部配置文件的名字 @ConfigurationProperties(prefix = "author")//前缀,对应的是配置文件中的author
- @RequestHeader(),获取请求信息头,在参数用String接受
整合mybatis
- 添加依赖
<!-- 加载mybatis整合springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- MySQL的jdbc驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- springboot 开发自动热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
- 让springboot编译mybatis文件
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
- 在application中配置信息
#指定mapper文件的位置 mybatis.mapper-locations=classpath:com/java/demo/dao/*.xml #指定bean的位置 mybatis.type-aliases-package=com.java.demo.bean #数据源 spring.datasource.username=root spring.datasource.password=passwd spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/sqldemo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone= Asia/Shanghai spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #查看sql语句日志 logging.level.com.java.demo.dao=debug
- 创建mapper和dao接口,dao接口类需要加上@Mapper注解,或者在main方法上加上@MapperScan注解,
4.1 书写xml
4.2 在dao方法上加上注解。 保存对象,获取数据库自增id @Options(useGeneratedKeys=true, keyProperty=“id”, keyColumn=“id”)<?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="com.java.demo.dao.StudentMapper"> <!--查询多条数据--> <select id="selectAllStudent" resultType="student"> SELECT id,name,age,score FROM t_student </select> </mapper>
@Select("SELECT * FROM user") @Results({ @Result