SpringBoot-Common Annotations-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ファクトリの下の構成クラス(開いた)を、springfactoriesLoaderのloadメソッドを介してロードし、forループで1つずつロードします
利点:大量の統合情報を順不同で書き込むには、統合するために、Springbootが提供する適切な規則に従うだけで済みます。
欠点:スターター依存関係が追加され、必要な構成情報を入力する必要があります
@SpringBootApplication(exclude = QuartzAutoConfiguration.class)を手動で閉じます
@ComponentScan
<context:component-scan basePackage = "package name" />と同等
構成ファイルの形式
プロパティとyml、jsonもサポート
ymlをお勧めします
- 改行とインデントに基づいて位置を管理する
- 軽量
短所:
- 改行とインデントに厳密に従う
- 値を入力するときは、コロンの後にスペースを追加してください
マルチ環境構成
application.ymlファイルに構成アイテムを追加する
spring:
profiles:
active: dev
リソースディレクトリに複数のアプリケーション環境名の.ymlファイルを作成する
プロジェクトをデプロイするとき、java -jar jarファイル-spring.profiles.active = environmentを使用
外部構成ファイル情報をインポートする
従来のSSMメソッドと同様に、properties.ymlファイルのコンテンツは@Valueのアノテーションを介して取得されます
統一された接頭辞を付けて、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>
設定の構成を変更します
[外部リンク画像の転送に失敗しました。ソースサイトにアンチリーチリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-1uCGZ43D-1600395426952)(/ Users / chenxiwen / Library / Application Support / typora-user-images / image-20200911231843480 .png)]
コンテンツを変更した後、ビルドを通じてプロジェクトを再ビルドできます
SpringBootはMybatisを統合します
-
依存関係のインポート
-
設定ファイルを書き込む
2.1エンティティークラスを準備する
@Data
public class Subject implements Serializable {
private long id;
private long subjectNo;
private String subjectName;
}
2.2マッパーインターフェイスの準備
public interface ResultMapper {
List<Result> findAll();
}
スタートアップクラスに注釈を追加し、マッパーインターフェイスが配置されているパッケージをスキャンします。
@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.テスト
マッパーインターフェースの右クリック-「goto」テスト
テストディレクトリに、インターフェイスのテストクラスを作成します。
現在のテストクラスにFirstSpringbootApplicationTestsテストクラス(およびパブリック)を継承させます。
ドライバーのバージョンが高い場合、タイムゾーンを追加する必要があります
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>
jspページを格納するためのwebapおよびWEB-INFストレージディレクトリを作成する
[外部リンク画像の転送に失敗しました。ソースサイトにアンチリーチリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-VFjbMFHC-1600395426954)(/ Users / chenxiwen / Library / Application Support / typora-user-images / image-20200917212024309) .png)]
コントローラを作成し、ビューのプレフィックスとサフィックスを指定します
@Controller
public class JspController {
@GetMapping("/index")
public String index(Model model) {
model.addAttribute("name", "荣荣");
return "index";
}
}
#连接数据库的信息
spring:
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp