경로가 겹칠 수 있음으로 @PutMapping("/board/{num}")식별자 추가함
업데이트시 제목과 내용은 JSON 데이터 임으로 어노테이션 @RequestBody을 추가해서 전용 데이터 받아줌
업데이트 원리
단 에러가 생김으로 코드를 다음과 같이 변경
// boardUpdate(Long num, Board board)
@PutMapping("/board/{num}")
public ResponseEntity<?> boardUpdate(@PathVariable Long num, @RequestBody Board board){
board.setNum(num); //
return new ResponseEntity<>(mapper.boardUpdate(board),HttpStatus.OK);
}
https://gmlwjd9405.github.io/2019/01/01/spring-utf8.html
web.xml의 맨위에 복붙
${fn:replace(board.content,newLine,"<br>")}
데이터 전송방법
form에서 받은 데이터를 맵퍼에 전달함 > sql 실행
패키지 구성요소
config 패키지
package org.example.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"}; // -> DispatcherServlet
}
// 필터
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
return new Filter[]{encodingFilter};
}
}
package org.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//servlet-context.xml 대체기능
@Configuration
@EnableWebMvc // <annotation-driven/> 대체
@ComponentScan(basePackages = "org.example.controller") // POJO 대체
public class ServletConfig implements WebMvcConfigurer {
@Override // 재정의 됨
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable(); // 핸들러 맵핑을 활성화
}
@Bean // 스프링 컨테이너에 객체가 생성(메소드가 호출됨)
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
package org.example.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
//root-context.xml 을 대신할 파일
@Configuration
@MapperScan(basePackages = "org.example.repersitoty") // 여러개는 스캔s 사용
public class RootConfig {
@Bean //CP : HikariCP
public HikariConfig hikariConfig(){
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://localhost:3306/ai");
config.setUsername("root");
config.setPassword("12345");
return config;
}
@Bean // Hikari 데이터 소스
public DataSource dataSource(){
// hikariConfig()를 호출해서 사용
return new HikariDataSource( hikariConfig() ); //Connection Pool
}
@Bean // mapper.xml 찾기
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mybatis-config/mapper/**.xml"));
return sqlSessionFactoryBean.getObject(); // SqlSessionFactory
}
}