[Day 2] SpringBoot + Vue.js์ MyBatis ๋ผ์น๊ธฐ
by ์ค๊ทคํฑ๊ทค์ด๋ ๊ฒ ์ธํ ๋ฐ๋ณด๊ฐ ๋์ด์์์ง ๋ฏธ์ฒ ์ ๋ ๋ชฐ๋์ต๋๋ค.
๊ณต๋ถ ์๊ธํด์ง... ์ค๋๋ถํฐ ์ต๊ฐ์ผ๊ตฌ ์ง์์ ๋ณด์ง ์๊ฒ ์ต๋๋ค.
1. pom.xml ์์
์ง๋๋ฒ์ SpringBoot Project ์์ฑ ์, MyBatis์ ๋ํ dependency๋ฅผ ์ถ๊ฐํ์ง ์์๊ธฐ ๋๋ฌธ์ ์ถ๊ฐํด์ค์ผ ํ๋ค.
ํ๋ก์ ํธ ์ฐํด๋ฆญ > Spring > Add Starters > MyBatis Framework ์ถ๊ฐ
๋ ์ด๋ฏธ ์ถ๊ฐํด๋ฒ๋ ค์ ์ฝ๋๊ฐ ๋์ผํ์ง๋ง, ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ์ ๋ง์ถฐ์ฃผ๋ฉด ๋๋ค.
2. application.properties ์์
src/main/resources ํ์ผ ๋ฐ์ ์๋ application.properties ํ์ผ์ ์์ ํ๋ค.
application.properties
#db connection(Oracle)
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=์ค๋ผํด db ์ฌ์ฉ์ id
spring.datasource.password=๋น๋ฐ๋ฒํธ
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.sql.init.encoding=UTF-8
#mapper.xml location
mybatis.mapper-locations:classpath*:com/insa/insaSystem/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.url
์ port์ sid๋ฅผ ์์ ํ์ง ์์๋ค๋ฉด ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ๋ ๋ฏ ์ถ๋ค.
mybatis.mapper-locations
๋ mapper xmlํ์ผ์ด ์๋ ๊ฒฝ๋ก๋ฅผ ์ ์ด์ฃผ๋ฉด ๋๋๋ฐ,
์ด ๋ classpath๋ src/main/resources ๋ฅผ ์๋ฏธํ๋ค.
mybatis.configuration.map-underscore-to-camel-case=true:
์ธ๋๋ฐ๋ฅผ camel case๋ก ๋ณํํ๋ ์ค์ ์ด๋ค.
DB๋ ๋์๋ฌธ์ ๊ตฌ๋ณ์ด ์์ผ๋ฏ๋ก ์ธ๋๋ฐ๋ฅผ ์ด์ฉํด ๋จ์ด์ ๊ฒฐํฉ์ ๊ตฌ๋ถ, java๋ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ฏ๋ก camel case๋ฐฉ์์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์๋ก ์ด๋ฆ์ด ๋ค๋ฅด๊ฒ ๋๋๋ฐ ์ด๋ฅผ ๊ฐ์ ์ด๋ฆ์ผ๋ก ์ธ์ํ์ฌ ๋งคํ๋๋๋ก ๋ณํํ๋ ์ค์ ์ด๋ค.
(์ถ์ฒ : https://velog.io/@0829kuj/%EC%8A%A4%ED%94%84%EB%A7%81%EC%88%98%EC%97%85-15%EC%9D%BC%EC%B0%A8)
spring.datasource.hikari.maximum-pool-size
๋ ๋ง๊ทธ๋๋ก hikari์ ์ต๋ ์ปค๋ฅ์
ํ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํด์ค๋ค. ์คํ๋ง๋ถํธ ๋ฒ์ ์ ๋ฐ๋ผ ์๋์ผ๋ก hikari๊ฐ ์ค์น๊ฐ ๋๋ค๊ณ ๋ณธ ๊ฒ ๊ฐ์๋ฐ, ์ฌ์ค ์๋ฌ ์์ ํ๋ค๊ฐ ๋
๋ค ์ถ๊ฐํ๊ฑฐ๋ผ ํ์ฌ์ํฉ์์๋ ํ์ ์๋ ๊ฒ ๊ฐ๊ธฐ๋...
โป classpath ๊ฒฝ๋ก ์ฐพ์๋ณด๊ธฐ (←์ด๊ฑฐ ๋๋ฌธ์ ์๋ฌ๋ ์ฌ๋ That's me)
ํ๋ก์ ํธ ์ฐํด๋ฆญ > Build Path > Configure Build Path ... > Source ํญ ํด๋ฆญ ![]() classpath๋ ์ java build path์ source ํด๋๋ฅผ ๋ชจ๋ ๊ฒ์?ํ๋ค. ํ๋ฐ ๋๋ resources ํ์ผ์ด ํด๋น ๊ฒฝ๋ก์ ํฌํจ๋์ด์์ง ์์์ xml ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํด์...์จ๊ฐ ์๋ฌ๋ฅผ ๋ฐ์์์ผฐ๋ค. resources ํ์ผ ์๋์ .xml ํ์ผ์ ์์น์ํฌ๊ฑฐ๋ผ๋ฉด! ๊ผญ ํ์ธํด๋ณผ ๊ฒ!!!! |
3. sql Bean ์ถ๊ฐ → ProjectApplication.java
ProjectApplication.java
@SpringBootApplication(exclude = {MybatisAutoConfiguration.class})
@MapperScan(basePackages="com.insa.insaSystem.mapper")
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(InsaSystemApplication.class, args);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:com/insa/insaSystem/mapper/*.xml")
);
return sessionFactory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@MapperScan basePackages
์๋ mapper ๋๋ dao์ ํด๋นํ๋ package๋ฅผ ์์ฑํ๋ค.
sessionFactory MapperLocations
์๋ ํ๋กํผํฐ์ ์์ฑํ .xml ๊ฒฝ๋ก๋ฅผ ์์ฑํด์ค๋ค.
4. Mapper interface, Mapper.xml ์์ฑ
TestMapper.java (
@Mapper
public interface TestMapper {
public String testDB(@Param("rank") int rank) throws Exception;
}
DB์ ์ฐ๊ฒฐ์ด ์ ๋๋์ง ํ์ธํ๊ธฐ ์ํด์ testDB๋ผ๋ interface๋ฅผ ๋ง๋ค์ด์ฃผ์๋ค.
์ฌ์ค, input Parameter ํ์์๋๋ฐ.... ์ฝ์งํ๋๋ผ ์ด๊ฒ์ ๊ฒ ์๋ํด๋ณด๋๋ผ ๋ฃ์ด์คฌ๋ค.
ํด๋น interface๊ฐ mapper๋ผ๋๊ฑธ ์๋ ค์ค์ผํ๊ธฐ ๋๋ฌธ์ @Mapper
์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ค.
testMapper.xml
<?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.insa.insaSystem.mapper.testDao">
<select id="testDB" parameterType="java.lang.Integer" resultType="java.lang.String">
SELECT TEST_COL
FROM TEST_TABLE
WHERE RANK_COL = #{rank}
</select>
</mapper>
.xml ํ์ผ ์ฟผ๋ฆฌ id์ interface์์ ์์ฑํ ๋ฉ์๋๋ช ์ด ๊ฐ์์ผํ๋ค.
↓ Error Log
@Select
์ด๋
ธํ
์ด์
์ฌ์ค ์์ ์ฝ๋์๋ ์๊ด ์์ง๋ง, ์ด๋ ธํ ์ด์ ์ ์ดํด ์์ด ๋ ๋ค ์๋ฌ ์์ ๊ฒ ๋ค๊ณ ์ผ๋ค๊ฐ
ORA-00900 : invalid SQL statement ์๋ฌ๋ฅผ ๋ง๋ณด์๊ธฐ ๋๋ฌธ์ ์ ๋ฆฌํด๋ณธ๋ค.
@Select
@Insert
@Update
์ ๊ฐ์ ์ด๋
ธํ
์ด์
์ xml ํ์ผ์ ๊ฑฐ์น์ง ์๊ณ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ์ง์ํ๋ค.
๋ด๊ฐ ๊ตฌ๊ธ๋ง ํ ๋์๋ ์ ์ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋ ์ด๋ค๊ณ ๋ดค๋๋ฐ, ์ง๊ธ ๋ ๊ตฌ๊ธ๋งํด๋ณด๋ ์๋ ๊ฒ ๊ฐ๋ค.
@Mapper
public interface TestMapper {
@Select("SELECT TEST_COL FROM TEST_TABLE WHERE RANK_COL = 1")
public String testDB(@Param("rank") int rank) throws Exception;
}
์ ๊ฐ์ ํํ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ด๋ ๋๋ resources ๊ฒฝ๋ก์ค์ ์ด ์ ๋๋ก ๋์ด์์ง ์์์ xml ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํ๋ ์ํ์๊ณ ,
ํด๋น ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ฉด ์๋ฌ๊ฐ ๋์ง ์๋๋ค๋ ๋ง๋ง ๋ฃ๊ณ , @Select("testDB")
์ฟผ๋ฆฌ ID๋ฅผ ๋ ๋ค ์จ๋ฒ๋ ธ๋ค. ์ผ๋จ SQL ์๋ฌ๊ฐ ๋๋, SQL ์ฐ๊ฒฐ์ ๋๊ฑฐ ์๋๊ฐ!!ํ๋ ๊ธฐ์จ๊ณผ ํจ๊ป ๋ง์ด๋ค.
์ด์ฉ์ง...์ด๋ฐ ์ด๋ ธํ ์ด์ ์์ด๋ ๋ง์ด๋ฐํฐ์ค๋ฅผ ์ธ ์ ์๋๋ฐ ์คํ๋ง๋ถํธ๋ผ ์ฐ๋๊ฑด๊ฐ ์ถ์๋ค.
๊ณต๋ถํ์...์ค๊ทค์...
4. service.java, serviceImpl.java ์์ฑ ํ, DB ์ฐ๋ test
ProjectService.java
public interface TestService {
public String test(int rank) throws Exception;
}
ProjectServiceImpl.java
Service
public class TestServiceImpl implements TestService{
@Autowired
testMapper testMapper;
@Override
public String test(int rank) throws Exception {
return testMapper.testDB(rank);
}
}
ProjectController.java
@Controller
public class ProejctController {
@Autowired
TestService testService;
@RequestMapping(value = "/", method=RequestMethod.GET)
public ModelAndView home() throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("index.html");
int rank = 1;
String test = testService.test(rank);
System.out.println("DB ์ฐ๋ ํ์ธ์ฉ");
System.out.println();
System.out.println(test);
return mv;
}
}
์์ฑ ํ, SpringBoot ๋ด์ฅ ํฐ์บฃ์ ์คํ์ํค๋ฉด
์ ์์ ์ผ๋ก ์๋ํจ์ ์ ์ ์๋ค.
'FRONTEND > VUE.JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day 1] SpringBoot + Vue.js ๊ฐ๋ฐํ๊ฒฝ ์ธํ (1) | 2023.03.12 |
---|
๋ธ๋ก๊ทธ์ ์ ๋ณด
๐๐๐ซ๐๐ง ๐ฟ๐๐
์ค๊ทคํฑ๊ทค