select 컬럼들 from 테이블명 where 조건
select title, category, price, insert_date from book where book_id =1
<?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="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
<select id="select_detail" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
title,
category,
price,
insert_date
from
book
where
book_id = #{bookId}
]]>
</select>
</mapper>
package sample.spring.yse;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Autowired
SqlSessionTemplate sqlSessionTemplate;
public int insert(Map<String, Object> map) {
return this.sqlSessionTemplate.insert("book.insert", map);
}
public Map<String, Object> selectDetail(Map<String, Object> map) {
return this.sqlSessionTemplate.selectOne("book.select_detail", map);
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if(affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
@Override
public Map<String, Object> detail(Map<String, Object> map) {
return this.bookDao.selectDetail(map);
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?BookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>책 상세</title>
</head>
<body>
<h1>책 상세</h1>
<p>제목 : ${ data.title } </p>
<p>카테고리 ${data.category } </p>
<p>가격 : <fmt:formatNumber type="number" maxFractionDigits="3" value="${data.price }" /> </p>
<p>입력일 : <fmt:formatDate value="${data.insert_date }" pattern="yyyy.MM.dd HH:mm:ss"/> </p>
<p>
<a href="/update?bookId=${bookId }">수정</a>
</p>
<form method="post" action="/delete">
<input type="hidden" name="bookId" value="${bookId }" />
<input type="submit" value="삭제" />
</form>
<p>
<a href="/list">목록으로</a>
</p>
</body>
</html>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
mav.addObject("data",datilMap);
<p>가격 : <fmt:formatNumber type="number" maxFractionDigits="3" value="${data.price }" /> </p>
<p>입력일 : <fmt:formatDate value="${data.insert_date }" pattern="yyyy.MM.dd HH:mm:ss"/> </p>
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?BookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView update(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
mav.setViewName("/book/update");
return mav;
}
}
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>책 수정</title>
</head>
<body>
<h1>책 수정</h1>
<form method="POST">
<p>제목 : <input type="text" name="title" value="${data.title }"/> </p>
<p>카테고리 : <input type="text" name="category" value="${data.category }"/> </p>
<p>가격 : <input type="text" name="price" value="${data.price }"/> </p>
<p><input type="submit" value="저장" /> </p>
</form>
</body>
</html>
책 상세 화면을 수정하는 쿼리를 작성한다
UPDATE 테이블명 SET 컬럼들 where 조건
UPDATE book SET title='제목', category='IT', price=10000 where book_id = 1
수정 쿼리를 작성한다
<?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="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
<select id="select_detail" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
title,
category,
price,
insert_date
from
book
where
book_id = #{bookId}
]]>
</select>
<update id="update" parameterType="hashMap">
<![CDATA[
update book set
title = #{title},
category = #{category},
price = #{price}
where
book_id = #{bookId}
]]>
</update>
</mapper>
package sample.spring.yse;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Autowired
SqlSessionTemplate sqlSessionTemplate;
public int insert(Map<String, Object> map) {
return this.sqlSessionTemplate.insert("book.insert", map);
}
public Map<String, Object> selectDetail(Map<String, Object> map) {
return this.sqlSessionTemplate.selectOne("book.select_detail", map);
}
public int update(Map<String, Object> map) {
return this.sqlSessionTemplate.update("book.update", map);
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if(affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
@Override
public Map<String, Object> detail(Map<String, Object> map) {
return this.bookDao.selectDetail(map);
}
@Override
public boolean edit(Map<String, Object> map) {
int affectRowCount = this.bookDao.update(map);
return affectRowCount == 1;
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?BookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView update(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
mav.setViewName("/book/update");
return mav;
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public ModelAndView updatePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isUpdateSuccess = this.bookservice.edit(map);
if(isUpdateSuccess) {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}else {
mav = this.update(map);
}
return mav;
}
}
/update?bookId=1
{
"bookId" : 1,
"title" : "제목 수정",
"category" : "IT",
"price" : "10000"
}
책 정보를 삭제하는 쿼리를 작성한다
DELETE FROM 테이블명 WHERE 조건
DELETE FROM book WHERE book_id = 1
src/main/resouce/sqlmap/book_SQL.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="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
<select id="select_detail" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
title,
category,
price,
insert_date
from
book
where
book_id = #{bookId}
]]>
</select>
<update id="update" parameterType="hashMap">
<![CDATA[
update book set
title = #{title},
category = #{category},
price = #{price}
where
book_id = #{bookId}
]]>
</update>
<delete id="delete" parameterType="hashMap">
<![CDATA[
delete from book
where
book_id = #{bookId}
]]>
</delete>
</mapper>
package sample.spring.yse;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Autowired
SqlSessionTemplate sqlSessionTemplate;
public int insert(Map<String, Object> map) {
return this.sqlSessionTemplate.insert("book.insert", map);
}
public Map<String, Object> selectDetail(Map<String, Object> map) {
return this.sqlSessionTemplate.selectOne("book.select_detail", map);
}
public int update(Map<String, Object> map) {
return this.sqlSessionTemplate.update("book.update", map);
}
public int delete(Map<String, Object> map) {
return this.sqlSessionTemplate.delete("book.delete", map);
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if(affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
@Override
public Map<String, Object> detail(Map<String, Object> map) {
return this.bookDao.selectDetail(map);
}
@Override
public boolean edit(Map<String, Object> map) {
int affectRowCount = this.bookDao.update(map);
return affectRowCount == 1;
}
@Override
public boolean remove(Map<String, Object> map) {
int affectRowCount = this.bookDao.delete(map);
return affectRowCount == 1;
}
}
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?BookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView update(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
mav.setViewName("/book/update");
return mav;
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public ModelAndView updatePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isUpdateSuccess = this.bookservice.edit(map);
if(isUpdateSuccess) {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}else {
mav = this.update(map);
}
return mav;
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public ModelAndView deletePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isDeleteSuccess = this.bookservice.remove(map);
if(isDeleteSuccess) {
mav.setViewName("redirect:/list");
}else {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}
return mav;
}
}
<?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="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
<select id="select_detail" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
title,
category,
price,
insert_date
from
book
where
book_id = #{bookId}
]]>
</select>
<update id="update" parameterType="hashMap">
<![CDATA[
update book set
title = #{title},
category = #{category},
price = #{price}
where
book_id = #{bookId}
]]>
</update>
<delete id="delete" parameterType="hashMap">
<![CDATA[
delete from book
where
book_id = #{bookId}
]]>
</delete>
<select id="select_list" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
book_id,
title,
category,
price,
insert_date
from
book
order by insert_date desc
]]>
</select>
</mapper>
package sample.spring.yse;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Autowired
SqlSessionTemplate sqlSessionTemplate;
public int insert(Map<String, Object> map) {
return this.sqlSessionTemplate.insert("book.insert", map);
}
public Map<String, Object> selectDetail(Map<String, Object> map) {
return this.sqlSessionTemplate.selectOne("book.select_detail", map);
}
public int update(Map<String, Object> map) {
return this.sqlSessionTemplate.update("book.update", map);
}
public int delete(Map<String, Object> map) {
return this.sqlSessionTemplate.delete("book.delete", map);
}
public List<Map<String, Object>> selectList(Map<String, Object> map) {
return this.sqlSessionTemplate.selectList("book.select_list", map);
}
}
package sample.spring.yse;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if(affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
@Override
public Map<String, Object> detail(Map<String, Object> map) {
return this.bookDao.selectDetail(map);
}
@Override
public boolean edit(Map<String, Object> map) {
int affectRowCount = this.bookDao.update(map);
return affectRowCount == 1;
}
@Override
public boolean remove(Map<String, Object> map) {
int affectRowCount = this.bookDao.delete(map);
return affectRowCount == 1;
}
@Override
public List<Map<String, Object>> list(Map<String, Object> map) {
return this.bookDao.selectList(map);
}
}
package sample.spring.yse;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?bookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView update(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
mav.setViewName("/book/update");
return mav;
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public ModelAndView updatePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isUpdateSuccess = this.bookservice.edit(map);
if(isUpdateSuccess) {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}else {
mav = this.update(map);
}
return mav;
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public ModelAndView deletePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isDeleteSuccess = this.bookservice.remove(map);
if(isDeleteSuccess) {
mav.setViewName("redirect:/list");
}else {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}
return mav;
}
@RequestMapping(value = "list")
public ModelAndView list(@RequestParam Map<String, Object> map) {
List<Map<String, Object>> list = this.bookservice.list(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", list);
mav.setViewName("/book/list");
return mav;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>책 목록</title>
</head>
<body>
<h1>책 목록</h1>
<table>
<thead>
<tr>
<th>제목</th>
<th>카테고리</th>
<th>가격</th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${data}">
<tr>
<td>
<a href="/detail?bookId=${row.book_id}">${row.title}</a>
</td>
<td>${row.category }</td>
<td><fmt:formatNumber type="number" maxFractionDigits="3" value="${row.price}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="/create">생성</a>
</p>
</body>
</html>
<?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="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
<select id="select_detail" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
title,
category,
price,
insert_date
from
book
where
book_id = #{bookId}
]]>
</select>
<update id="update" parameterType="hashMap">
<![CDATA[
update book set
title = #{title},
category = #{category},
price = #{price}
where
book_id = #{bookId}
]]>
</update>
<delete id="delete" parameterType="hashMap">
<![CDATA[
delete from book
where
book_id = #{bookId}
]]>
</delete>
<select id="select_list" parameterType="hashMap" resultType="hashMap">
<![CDATA[
select
book_id,
title,
category,
price,
insert_date
from
book
where 1 = 1
]]>
<if test="keyword != null and keyword != ''">
and (title like CONCAT('%',#{keyword},'%') or category like CONCAT('%',#{keyword},'%'))
</if>
order by insert_date desc
</select>
</mapper>
order by insert_date desc
]]>
where 1 = 1
]]>
<if test="keyword != null and keyword != ''">
and (title like CONCAT('%',#{keyword},'%') or category like CONCAT('%',#{keyword},'%'))
</if>
order by insert_date desc
<if test="keyword != null and keyword != ''">
and (title like CONCAT('%',#{keyword},'%') or category like CONCAT('%',#{keyword},'%'))
and (title like '%키워드%' or category like '%키워드%')
package sample.spring.yse;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookservice;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
return new ModelAndView("book/create");
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookservice.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?bookId="+bookId);
}
return mav;
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public ModelAndView detail(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
String bookId = map.get("bookId").toString();
mav.addObject("bookId", bookId);
mav.setViewName("/book/detail");
return mav;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView update(@RequestParam Map<String, Object> map) {
Map<String, Object> detailMap = this.bookservice.detail(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", detailMap);
mav.setViewName("/book/update");
return mav;
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public ModelAndView updatePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isUpdateSuccess = this.bookservice.edit(map);
if(isUpdateSuccess) {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}else {
mav = this.update(map);
}
return mav;
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public ModelAndView deletePost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
boolean isDeleteSuccess = this.bookservice.remove(map);
if(isDeleteSuccess) {
mav.setViewName("redirect:/list");
}else {
String bookId = map.get("bookId").toString();
mav.setViewName("redirect:/detail?bookId="+bookId);
}
return mav;
}
@RequestMapping(value = "list")
public ModelAndView list(@RequestParam Map<String, Object> map) {
List<Map<String, Object>> list = this.bookservice.list(map);
ModelAndView mav = new ModelAndView();
mav.addObject("data", list);
if(map.containsKey("keyword")) {
mav.addObject("keyword", map.get("keyword"));
}
mav.setViewName("/book/list");
return mav;
}
}
if(map.containsKey("keyword")) {
mav.addObject("keyword", map.get("keyword"));
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>책 목록</title>
</head>
<body>
<h1>책 목록</h1>
<p>
<form>
<input type="text" placeholder="검색" name="keyword" value="${keyword}" />
<input type="submit" value="검색" />
</form>
</p>
<table>
<thead>
<tr>
<th>제목</th>
<th>카테고리</th>
<th>가격</th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${data}">
<tr>
<td>
<a href="/detail?bookId=${row.book_id}">${row.title}</a>
</td>
<td>${row.category }</td>
<td><fmt:formatNumber type="number" maxFractionDigits="3" value="${row.price}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="/create">생성</a>
</p>
</body>
</html>
<p>
<form>
<input type="text" placeholder="검색" name="keyword" value="${keyword}" />
<input type="submit" value="검색" />
</form>
</p>
정말 많은 도움이 돼요 감사합니다!