SpringBoot-常用注解-Spring03
@Configration和 @Bean
@Configration->beans标签
@Bean ->bean标签
id=方法名|注解中的name属性(指定后优先级更高)
class = “方法的返回结果
package com.example.firstspringboot.config;
import com.example.firstspringboot.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //代表当前类是个配置类
public class UserConfig {
@Bean //构建1个实例,放到spring容器中
public User user() {
User user = new User();
user.setId(1);
user.setName("荣荣");
return user;
}
/*
<beans> @Configuration
<bean id="user" class="com.example.firstspringboot.entity.User>
</beans>
*/
}
启动类注解@SpringBootApplication
@SpringBootApplication是个组合注解
@SpringConfigration就是@Configration注解 代表启动类就是一个配置类
@EnableAutoConfiguration 帮你实现自动装配,springbooot工程启动时,运行一个SpringFactoryLoader类,加载位于META-INF下/spring factories的配置类(已经开启的)通过springfactoriesLoader中的load方法,for循环一个个加载
好处:无序编写大量整合信息只需要按照springboot提供好的约定去整合
坏处:加入了starter依赖,需要填写必要配置信息
手动关闭@SpringBootApplication(exclude = QuartzAutoConfiguration.class)
@ComponentScan
相当于<context:component-scan basePackage=“包名”/>
配置文件格式
支持properties和yml,甚至支持json
推荐yml
- 会根据换行和缩进管理位置
- 轻量级
劣势:
- 严格遵循换行和缩进
- 填写value时,一定要在冒号后面加空格
多环境配置
application.yml文件中加一个配置项
spring:
profiles:
active: dev
在resource目录下,创建多个application-环境名.yml 文件
在部署工程时,通过java -jar jar文件-spring.profiles.active =环境
引入外部配置文件信息
和传统的SSM方式一样,通过@Value的注解去获取properties.yml的文件的内容
如果做yml文件中需要编写大量自定义配置,具有统一的前缀,
@ConfigurationProperties(prefix = "aliyun")
@Component
@Data
public class AliyunProperties {
private String xxx;
private String yyy;
private String zzz;
}
aliyun:
xxx: xxxxx
yyy: yyyyy
zzz: zzzzz
热加载
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
修改setting的配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1uCGZ43D-1600395426952)(/Users/chenxiwen/Library/Application Support/typora-user-images/image-20200911231843480.png)]
修改内容后,可以通过build重新构建工程
SpringBoot整合Mybatis
-
导入依赖
-
编写配置文件
2.1 准备实体类
@Data
public class Subject implements Serializable {
private long id;
private long subjectNo;
private String subjectName;
}
2.2 准备mapper接口
public interface ResultMapper {
List<Result> findAll();
}
在启动类中增加注解,扫描Mapper接口所在包
@SpringBootApplication(exclude = QuartzAutoConfiguration.class)
@MapperScan(basePackages = "com.example.firstspringboot.mapper")
public class FirstSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstSpringbootApplication.class, args);
}
}
2.3准备映射文件
<?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.example.firstspringboot.mapper.ResultMapper">
<!-- List<Result> findAll();-->
<select id="findAll" resultType="Result">
select * from result
</select>
</mapper>
2.4 添加yml文件配置信息
#mybatis配置
mybatis:
# 扫描映射文件
mapper-locations: classpath:mapper/*.xml
# 配置别名扫描的包
type-aliases-package: com.example.firstspringboot.entity
# 开启驼峰映射配置
configuration:
map-underscore-to-camel-case: true
2.5指定连接数据库的连接
#连接数据库的信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///result
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
3,测试
mapper接口右键-》goto〉Test
创建接口的测试类,在test目录下
让当前测试类,继承FirstSpringbootApplicationTests测试类(加上public)
驱动版本高的时候,需要加时区
注解方式整合mybatis
- 创建student的Mapper接口
public interface StudentMapper {
List<Student> findeAll();
}
-
添加mybatis注解(@insert @delete @update @select),启动类需要加MapperScan注解
public interface StudentMapper { @Select("select * from student") List<Student> findeAll(); @Select("select * fromstudent where id = #{id}") Student findOne(@Param("id") Integer id); }
-
测试,看到执行的sql语句
logging:
level:
com.example.firstspringboot.mapper: DEBUG
class StudentMapperTest extends FirstSpringbootApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
void findeAll() {
List<Student> studentList = studentMapper.findAll();
for(Student s:studentList){
System.out.println(s);
}
}
@Test
void findOne() {
Student student = studentMapper.findOne(1);
System.out.println(student);
}
}
springboot整合分页助手
-
导入依赖
-
<!-- pagehelper分页助手--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
-
测试使用
-
@Test public void findByPage() { PageHelper.startPage(1, 2); List<Student> list = studentMapper.findAll(); PageInfo<Student> pageInfo = new PageInfo<>(list); for (Student student : pageInfo.getList()) { System.out.println(student); } }
springboot 导入jsp
-
导入依赖
-
<!-- JSP核心引擎--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- JSTL--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
创建webap以及WEB-INF存放目录存放jsp页面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VFjbMFHC-1600395426954)(/Users/chenxiwen/Library/Application Support/typora-user-images/image-20200917212024309.png)]
创建controller并指定view的前后缀
@Controller
public class JspController {
@GetMapping("/index")
public String index(Model model) {
model.addAttribute("name", "荣荣");
return "index";
}
}
#连接数据库的信息
spring:
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp