반응형
# 스프링 부트2
## 스프링 부트, MySQL, MyBatis 게시물 리스트, 상세보기
### MyBatis, MySQL 연결
- pom.xml에 직접 찾아서 넣는 방법도 있지만, 기존에 있는 것 사용 예정
- 프로젝트 폴더 우클릭 > Spring > Edit Starters 들어가서 MyBatis Framework, MySQL Driver 클릭 후 추가
- application.yml 문서에 Mysql, Mybatis 관련 설정 입력
### application.yml 문서
server:
port: 8011
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/at?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul&useOldAliasMetadataBehavior=true&zeroDateTimeNehavior=convertToNull
username: MySQL 아이디
password: MySQL 비밀번호
mybatis:
type-aliases-package: com.sbs.jhs.at.dto
### ArticleController.java
package com.sbs.jhs.at.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sbs.jhs.at.dto.Article;
import com.sbs.jhs.at.service.ArticleService;
@Controller
public class ArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping("/article/list")
public String showList(Model model) {
int count = articleService.getCount();
List<Article> articles = articleService.getForPrintArticles();
model.addAttribute("count", count);
model.addAttribute("articles", articles);
return "article/list";
}
@RequestMapping("/article/detail")
public String showDetail(Model model, long id) {
Article article = articleService.getOne(id);
System.out.println(article);
model.addAttribute("article", article);
return "article/detail";
}
}
### ArticleService.java
package com.sbs.jhs.at.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sbs.jhs.at.dao.ArticleDao;
import com.sbs.jhs.at.dto.Article;
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
public int getCount() {
return 2;
}
public List<Article> getForPrintArticles() {
List<Article> articles = articleDao.getForPrintArticles();
return articles;
}
public Article getOne(long id) {
Article article = articleDao.getOne(id);
return article;
}
public void hitUp(long id) {
// TODO Auto-generated method stub
}
}
### ArticleDao.java
package com.sbs.jhs.at.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.sbs.jhs.at.dto.Article;
@Mapper
public interface ArticleDao {
List<Article> getForPrintArticles();
Article getOne(@Param("id") long id);
}
### ArticleDao.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.sbs.jhs.at.dao.ArticleDao">
<select id="getForPrintArticles" resultType="Article">
SELECT *
FROM article
WHERE displayStatus = 1
ORDER BY id DESC
</select>
<select id="getOne" resultType="Article">
SELECT *
FROM article
WHERE displayStatus = 1
AND id = #{id}
</select>
</mapper>
반응형
'프로그래밍 > 자바, JDBC' 카테고리의 다른 글
lodash (throttle, debounce) 개념 (0) | 2020.08.26 |
---|---|
cannot be cast to java.lang.String 해결방법 (0) | 2020.08.09 |
자바 현재 시간, 날짜 구하기 (0) | 2020.07.31 |
스프링 부트1 (0) | 2020.07.28 |
배경 이미지 넣기, 배경 이미지 크기 조절하기(background: url) (0) | 2020.07.27 |