src/main/java/com.example.demo.dto
Customer.java
package com.example.demo.dto;
import javax.validation.constraints.NotNull;
public class Customer {
@NotNull
private String id;
@NotNull
private String username;
@NotNull
private String email;
@NotNull
private String phoneNumber;
@NotNull
private String postCode;
}
src/main/java/com.example.demo.repository
CustomerMapper.java
L @Mapperアノテーションを作る
L intは件数、insertはcreate処理
L customerは実際にinsertするobject
package com.example.demo.repository;
import com.example.demo.dto.Customer;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CustomerMapper {
int insert(Customer customer);
}
src/main/java/com.example.demo.repository
CustomerMapper.xml
L mapper namespaceでMapperを宣言
L #{fieldName} でアクセスすることができる、jdbcType= で型を指定する
<?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.exmaple.demo.repository.CustomerMapper">
<insert id="insert" parameterType="com.exmaple.demo.dto.Customer">
INSERT INTO customer VALUES (
#{id, jdbcType=VARCHAR},
#{username, jdbcType=VARCHAR},
#{email, jdbcType=VARCHAR},
#{phoneNumber, jdbcType=VARCHAR},
#{postCode, jdbcType=VARCHAR}
)
</insert>
</mapper>
src/test/java/com.example.demo.config
DbConfig.java
package com.example.demo.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
public class DbConfig {
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.driverClassName}")
private String jdbcDriver;
@Bean
public DataSource dataSource() {
return new TransactionAwareDataSourceProxy(
DataSourceBuilder.create()
.username(this.username)
.password(this.password)
.url(this.url)
.driverClassName(this.jdbcDriver)
.build());
}
}
src/test/java/com.example.demo.service
CustomerService.java
import com.example.demo.dto.Customer;
public interface CustomerService {
Customer register(Customer customer);
}
src/test/java/com.example.demo.service.impl
package com.example.demo.service.impl;
import com.example.demo.dto.Customer;
import com.example.demo.repository.CustomerMapper;
import com.example.demo.service.CustomerService;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService {
private CustomerMapper mapper;
public CustomerServiceImpl(CustomerMapper mapper) {
this.mapper = mapper;
}
@Override
public Customer register(Customer customer) {
String formattedEmail = formatEmail(customer.getEmail());
customer.setEmail(formattedEmail);
mapper.insert(customer);
return customer;
}
private String formatEmail(String email) {
String[] separatedEmail = email.split("@");
return separatedEmail[0] + "@" + separatedEmail[1].toLowerCase();
}
}
src/test/java/com.example.demo.controller
package com.example.demo.controller;
import com.example.demo.dto.Customer;
import com.example.demo.service.CustomerService;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/customers")
public class CustomerController {
private CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@PostMapping
public Customer post(@Validated @RequestBody Customer customer, Errors errors) {
if (errors.hasErrors()) {
throw new RuntimeException((Throwable) errors);
}
return customerService.register(customer);
}
}
POST man
body -> json
{
"id": "011",
"username": "user011",
"email": "test.user.011@EXAMPLE.com",
"phoneNumber": "12345678901",
"postCode": "4567123"
}

なんやろ、上手く動作しないな。全体の流れは何となく理解したが、🤮🤮🤮
org.springframework.validation.BeanPropertyBindingResult cannot be cast to class java.lang.Throwable (org.springframework.validation.BeanPropertyBindingResult is in unnamed module of loader ‘app’