<select>, <insert>, <update>, <delete>
로 나누어 작성<?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.sample.dao.BookDao">
<insert id="insertBook" parameterType="com.sample.vo.Book">
insert into sample_spring_books
(book_no, book_title, book_author, book_publisher,
book_price, book_discount_price, book_pub_date, book_stock)
values
(books_seq.nextval, #{title}, #{author}, #{publisher}, #{price},
#{discountPrice}, #{pubDate}, #{stock})
</insert>
<select id="getAllBooks" resultType="com.sample.vo.Book">
select
book_no as no,
book_title as title,
book_author as author,
book_publisher as publisher,
book_price as price,
book_discount_price as discountPrice,
book_pub_date as pubDate,
book_stock as stock,
book_updated_date as updatedDate,
book_created_date as createdDate
from
sample_spring_books
order by
book_no desc
</select>
</mapper>
<!DOCTYPE mapper...>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace...>
<mapper namespace="com.경로.경로.Dao">
<insert>, <update>, <delete>
에서 사용 가능한 속성
java파일이 위치할 package 생성
@Mapper
public interface BookDao {
void insertBook(Book book);
void updateBook(Book book);
List<Book> getAllBooks();
}
@Service
public class BookService {
@Autowired
private BookDao bookDao;
public List<Book> getAllBookList(){
return bookDao.getAllBooks();
}
public void addNewBook(Book book) {
bookDao.insertBook(book);
}
public void updateBookPrice(int bookNo, int price, int discountPrice) {
Book book = bookDao.getBookByNo(bookNo);
book.setPrice(price);
book.setDiscountPrice(discountPrice);
bookDao.updateBook(book);
}
}
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/list.do")
public String list(Model model) {
List<Book> books = bookService.getAllBookList();
model.addAttribute("books", books);
return "book/list.jsp";
}
}
<c:choose>
<c:when test="${empty books }">
<tr>
<td class="text-center" colspan="5">등록된 도서 정보가 없습니다.</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach var="book" items="${books }" varStatus="loop">
<tr>
<td>${loop.count }</td>
<td><a href="detail.do?no=${book.no}" class="text-none">${book.title }</a></td>
<td>${book.author }</td>
<td class="text-end pe-3"><fmt:formatNumber value="${book.price }" pattern="##,###"/> 원</td>
<td class="text-end pe-3"><strong class="text-danger"><fmt:formatNumber value="${book.discountPrice }" pattern="##,###"/></strong> 원</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>