728x90
반응형
◾ SVN 연결하기 - Get Connectors... 클릭
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
◾ pom.xml 선택 후 마우스 우클릭 > Maven > Update Project
#애플리케이션 포트 설정11
server.port = 80
#MySQL 연결 설정
spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/world?serverTimez
one=UTC
spring.datasource.username=root
spring.datasource.password=1234
# Mybatis 환경설정
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# mybatis Query Xml 경로 설정
mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
#Thymeleaf cache 사용 중지
spring.thymeleaf.cache = false
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 카멜 케이스 VO 매핑 --> 12
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 쿼리 결과 필드가 null인 경우, 누락이 되서 나오지 않게 설정-->
<!-- <setting name="callSettersOnNulls" value="true"/> -->
<!-- 쿼리에 보내는 파라미터가 null인 경우, 오류가 발생하는 것 방지 -->
<!-- <setting name="jdbcTypeForNull" value="NULL"/> -->
</settings>
</configuration>
package com.example.demo.base.vo;
import lombok.Data;
@Data
public class CityVo {
private String name;
private String countrycode;
private String regTime;
}
ICityDao.java
package com.example.demo.base.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.example.demo.base.vo.CityVo;
@Mapper
public interface ICityDao {
public List<CityVo> listDao();
public CityVo viewDao(String name);
public int writeDao(Map<String, String> map);
public int deleteDao(@Param("_name") String name);
public int articleCount();
}
ICityDao.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.example.demo.base.dao.ICityDao">
<select id="listDao"
resultType="com.example.demo.base.vo.CityVo">
select * from city order by id desc
</select>
<select id="viewDao"
resultType="com.example.demo.base.vo.CityVo">
select * from city where name = #{0}
</select>
<insert id="writeDao" parameterType="java.util.HashMap">
insert into city (id, name, coutrycode)
values (sample_seq.nextval, #{item1}, #{item2})
</insert>
<delete id="deleteDao">
delete from city where name = #{_name}
</delete>
<select id="articleCount" resultType="_int">
select count(*) from city
</select>
</mapper>
◾ resultType가 _를 작성하지 않으면 Integer 클래스 객체로 보내주고, _int 처럼 작성을 하면 int 형으로 보내준다.
Service안에는 DB호출도 있지만, 다른 로직도 있다.
package com.example.demo.base.service;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.example.demo.base.vo.CityVo;
public interface ICityService {
public List<CityVo> list();
public CityVo view(String name);
public int write(Map<String, String> map);
public int delete(String name);
public int count();
}
package com.example.demo.base.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.base.dao.ICityDao;
import com.example.demo.base.vo.CityVo;
@Service
public class CityServiceImpl implements ICityService {
@Autowired
ICityDao dao;
@Override
public List<CityVo> list() {
return dao.listDao();
}
@Override
public CityVo view(String name) {
return dao.viewDao(name);
}
@Override
public int write(Map<String, String> map) {
int nResult = dao.writeDao(map);
return nResult;
}
@Override
public int delete(String name) {
int nResult = dao.deleteDao(name);
return nResult;
}
@Override
public int count() {
int nTotalCount = dao.articleCount();
return nTotalCount;
}
}
package com.example.demo.base.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.base.service.ICityService;
import com.example.demo.base.vo.CityVo;
@RestController
public class BaseWebController {
@Autowired
ICityService cityService;
@RequestMapping("/")
public String root() {
return "문자열 데이터 전달";
}
@RequestMapping("/test")
public CityVo test(HttpServletRequest httpServletRequest, Model model) {
CityVo city = new CityVo();
city.setName("Test");
int count = cityService.count();
city.setCountrycode("Code" + count);
return city;
}
@RequestMapping("/list")
public List<CityVo> list(HttpServletRequest httpServletRequest, Model model) {
List<CityVo> list1 = cityService.list();
return list1;
}
@RequestMapping("/view")
public CityVo view(CityVo city, Model model) {
return city;
}
@RequestMapping("/detail/{name}/{code}")
public String detail(@PathVariable String code, @PathVariable String name, Model model) {
return name + "/" + code;
}
}
728x90
반응형
'Back-End > Spring Boot' 카테고리의 다른 글
JPA를 사용하는 이유 (0) | 2024.04.16 |
---|---|
Spring Boot - 서버 (0) | 2022.08.31 |
Spring Boot - BootStrap 이용하기 (0) | 2022.07.28 |
Spring Boot - 실습 test파일 수정 (0) | 2022.07.27 |
Spring Boot - 상품 등록하기 (0) | 2022.07.27 |
댓글