1. ローカル開発環境のホットリロード設定
開発中の自動リロード機能を有効化するための依存関係:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
設定プロパティの追加:
spring.devtools.add-properties=false
2. Eclipseのショートカット競合
コード整形ショートカット Ctrl+Shift+F が入力メソッドと競合する場合、Eclipse設定から解除してください。
3. Javaコード補完設定
Eclipse設定手順:
Window → Preferences → Java → Editor → Content Assist
自動補完トリガーを .abcdefghijklmnopqrstuvwxyz に変更
4. コントローラー注釈の選択
@RestController:REST API(JSON返却用)@Controller:ビュー返却用
5. MySQLドライバの非推奨問題
application.propertiesの設定:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6. クラス構成のベストプラクティス
@Service:ビジネスロジック層@Repository:DBアクセス層@Component:汎用コンポーネント
7. hh.exe不足の対応
Windowsシステムファイルのコピーと登録手順:
- hh.exeを
C:\Windowsに配置 - 関連DLLを
C:\Windows\System32に配置 - 管理者権限でコマンド実行:
regsvr32 hhctrl.ocx
regsvr32 itss.dll
regsvr32 itircl.dll
8. エンティティ生成ツール
JPA Tools推奨:
http://java.bejson.com/generator/ などのオンラインツール利用可能
11. マッピング注釈の使い分け
// GETリクエスト
@GetMapping("/example")
// POSTリクエスト
@PostMapping("/example")
12. REST APIの統一レスポンス
public class ApiResponse {
public static Map create(int code, String msg, Object data) {
Map res = new HashMap<>();
res.put("status", code);
res.put("message", msg);
res.put("payload", data);
return res;
}
}
設定:
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Tokyo
13. タイムゾーン不一致問題
MySQL設定:
[mysqld]
default-time-zone='+8:00'
14. 主キー生成戦略
MySQL推奨:
@GeneratedValue(strategy = GenerationType.IDENTITY)
15. 自動日時更新機能
// エンティティクラス
@EntityListeners(AuditingEntityListener.class)
public class SampleEntity {
@CreatedDate
@Column(updatable = false)
private Date createdAt;
@LastModifiedDate
private Date updatedAt;
}
// 設定クラス
@EnableJpaAuditing
@SpringBootApplication
public class Application { ... }
16. リクエストパラメータの送信方法
AJAX送信例:
contentType: "application/x-www-form-urlencoded"
data:$("#form").serialize()
17. @Componentと@Beanの違い
// クラス注釈
@Component
class ServiceClass { ... }
// 設定クラス内でのBean定義
@Configuration
class Config {
@Bean
public ServiceClass service() {
return new ServiceClass();
}
}
20. コレクション操作時の例外対策
Iterator<AdminPermission> itr = list.iterator();
while (itr.hasNext()) {
// 子要素の処理
process(itr.next().getChild());
}
21. LazyInitializationException対策
設定:
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
エンティティ注釈:
@Proxy(lazy = false)
24. JSON変換エラーの回避
エンティティ注釈:
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
25. データ変更履歴の自動記録
@CreatedDate
@Column(name = "create_time")
private Date createTime;
@LastModifiedDate
@Column(name = "modify_time")
private Date modifyTime;
26. Mavenレポジトリ高速化
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
29. データベース接続エラー
接続文字列例:
jdbc:mysql://localhost:3306/db?serverTimezone=UTC
30. JSON型データのマッピング
// 依存関係
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>2.4.3</version>
</dependency>
// エンティティ
@Type(type = "json")
@Column(columnDefinition = "json")
private List<String> metadata;
33. MyBatisのリレーションマッピング
<resultMap id="parentMap" type="Parent">
<collection property="children" ofType="Child">
<result property="name" column="child_name"/>
</collection>
</resultMap>
34. 複数パラメータの処理方法
// パラメータ注釈使用例
List<User> findUsers(@Param("ids") List<Integer> ids, @Param("type") String type);
// XMLクエリ
<select id="findUsers" resultType="User">
SELECT * FROM users
WHERE type = #{type}
AND id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>