์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ใ ใ
- ใ
- ๋ณ์
- ๋ก๊ทธ์ธํ๋ฉด #๋ก๊ทธ์ธ #JSP
- ์๋ฐ์ ์ ์์ ์ฐธ๊ณ ํ์์ต๋๋ค.
- ใฑ
- JSP #๊ฒ์ํ #ํ์ด์ง์ฒ๋ฆฌ #ํ์ด์ง
- ajax
- Today
- Total
Jam's story
[Spring] ํธ๋์ญ์ ๋ณธ๋ฌธ
๐์คํ๋ง ์ปจํ ์ด๋?
dispatcher-servlet.xml ์ค์ ํ์ผ์ ๋ณด๊ณ ์คํ๋ง ๋น ๊ฐ์ฒด ์์ฑ + ์กฐ๋ฆฝํ๋ ๊ณต์ฅ
๐ web.xml์ ๋ณด๋ฉด ํ๋ก ํธ์ปจํธ๋กค๋ฌ๊ฐ ๋ฑ๋ก๋์ด์๋ค. (๋ชจ๋ ์์ฒญ์ ๋ฐ๋ ์ปจํธ๋กค๋ฌ )
<!-- ์คํ๋ง front-controller๋ฑ๋ก -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup> <!-- ํฐ์บฃ์ด ์์๋ ๋ ์๋ธ๋ฆฟ๊ฐ์ฒด ์คํ -->
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ ์ฌ๋ฌ๊ฐ ์์ ์ ์๋ค. (๊ด๋ฆฌ๋ฅผ ๋ชฉ์ )
์)
๊ด๋ฆฌ์์์ฒญ ์คํ๋ง ์ปจํธ๋กค๋ฌ ,
ajax ์์ฒญ ์คํ๋ง ์ปจํธ๋กค๋ฌ ๋ฑ๋ฑ...
๐ ์คํ๋ง ์ปจํ ์ด๋ ๊ณ์ธต๊ตฌ์กฐ (๋ถ๋ชจ-์์ ๊ณ์ธต )
์คํ๋ง ์ปจํ ์ด๋ ๊ณ์ธต๊ตฌ์กฐ(๋ถ๋ชจ- ์์ ๊ณ์ธต )
๐ ์ง๊ธ์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ํ๋์ด๋ค (์์ ๊ณต์ฅ์ด ํ๋ )
ํ์ง๋ง ,์ง๊ธ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์ ์์๊ณต์ฅ์ ๋ ์ถ๊ฐํ๊ฒ ๋ค๋ ๊ฒ!
dispatcher-servlet.xml => ์ ์ธ๋ ๋น ๊ฐ์ฒด๋ค ์ค์ DB ์ฐ๋ํ๋ ์คํ๋ง ๋น์ ๋ถ๋ชจ๊ณต์ฅ(Application Context) ๊ทธ ์์์ ์์ฑํ๋๋ก ์ฝ๋ฉ
1๏ธโฃdispatcher-service.xml ํ์ผ ์์ฑ -์คํ๋ง ๋น์ ๊ด๋ฆฌ๋ฅผ ๋ชฉ์ ์ผ๋ก ์ค์ ํ์ผ ๋ถ๋ฆฌ
dispatcher-servlet.xml ( ์ด ์์ฒด๊ฐ ์๋ธ๋ฆฟ์ด๊ธฐ ๋๋ฌธ์ ํ ๊ฐ ์ด์์ dispatcher-servlet์ ์ค์ ํ ์ ์๋ค. )
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="newlecture.dao, controllers"></context:component-scan>
<!-- ํ์ผ์
๋ก๋๋ฅผ ์ํ -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="-1"></property>
</bean>
</beans>
dispatcher-service.xml (๋๋์ด์ฃผ์๋ค. )
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<!-- NamedParameterJddbcTemplate ํ
ํ๋ฆฟ ํด๋์ค ์ฌ์ฉ -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
</beans>
๐์ด๋ ๊ฒํ๋ฉด dispatcher-servlet.xml(์์ ๋) ๋ง ์คํ์ด ๋๋ฏ๋ก ๋ฐ์ดํฐ๊ด๋ จ ๋น ๊ฐ์ฒด๋ค์ ์์ฑ๋์ง ์๋๋ค.
์๋น์ค์ ์ค์ ๋ ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋๋ก web.xml์ ์์ ํด์ฃผ์ด์ผํ๋ค.
p373- ContextLoaderListener๋ฅผ ๋ฑ๋กํ์ฌ ๊ณตํต์ผ๋ก ์ฌ์ฉ๋ ๋น์ ์ค์ ํด์ค๋ค.
web.xml - ๋์คํจ์ฒ ์๋ธ๋ฆฟ์ ์ฌ๋ฌ๊ฐ๋ง๋ค๊ธฐ ์ํด์,( ๋ถ๋ชจ๊ณต์ฅ๊ณผ ์์๊ณต์ฅ๋ค์ ๋ง๋๋๋ฐ,) ๋ถ๋ชจ๊ณต์ฅ์ ๋ง๋ค๊ฒ ๋ค๋ ์๋ฆฌ
<!-- p.371 WebApplicationContext ๊ณ์ธต -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
springJDBC02 ๋ณต์ฌํด์ -> springTransaction ํ๋ก์ ํธ ์์ฑ
์ปจํ ์คํธ root ๋ฐ๊พธ๊ณ , add and remove์์ springTransaction ์ด๊ฒ๋ง ์ฌ๋ฆฌ๊ณ , ์๋ฒ ์คํ, ํ ํด๋ฆฐ ํ ์ฌ์์
์ ์ฌ์๊ฐ๋์ p505~536 ์ฝ์ด๋ณด๊ธฐ
1๏ธโฃHomeController ์์ฑ
@Controller
@RequestMapping("/")
public class HomeController {
public HomeController() {
super();
// TODO Auto-generated constructor stub
}
}
index.htm ์์ฑ
index.htm ์์ฒญ์ด์์๋ ๋ฉ์ธํ์ด์ง๋ก ๋๋ ค์ฃผ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์
HomeController.java
@Controller
@RequestMapping("/")
public class HomeController {
public HomeController() {
super();
// TODO Auto-generated constructor stub
}
@RequestMapping("index.htm")
public String home() throws Exception{
return "index.jsp";
}
}
JoinController.java
@Controller
@RequestMapping("/joinus/*")
public class JoinController {
public JoinController() {
super();
// TODO Auto-generated constructor stub
}
private MemberDao memberDao=null;
@RequestMapping(value= {"join.htm"}, method=RequestMethod.GET)
public String join() throws Exception{
return "redirect:../index.htm";
//joinus๊ฐ ๋ถ์ด์๊ธฐ ๋๋ฌธ์ ../ ์ถ๊ฐ
}
@RequestMapping(value= {"login.htm"}, method=RequestMethod.POST)
public String login(Member member) throws Exception{
this.memberDao.insert(member); //ํ์ insert
return "login.jsp";
}
}
notice.jsp -htm ์ผ๋ก ์์
<ul id="loginmenu" class="block_hlist">
<li>
<a href="../index.htm">HOME</a>
</li>
<li>
<a href="../joinus/login..htm">๋ก๊ทธ์ธ</a>
</li>
<li>
<a href="../joinus/join.htm">ํ์๊ฐ์
</a>
</li>
</ul>
-507p
dispatcher-servlet.xml
<!-- ์คํ๋ง ํธ๋์ญ์
์ ์ฒ๋ฆฌํ ํธ๋์ญ์
๋ฉ๋์ ธ ๋ฑ๋ก -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
6. ์คํ๋ง ํธ๋์ญ์ ์ฒ๋ฆฌ 3๊ฐ์ง ๋ฐฉ๋ฒ
1)์ฝ๋ ๊ธฐ๋ฐ ์ฒ๋ฆฌ: ๊ฐ๋ฐ์ ์ง์ ์ฝ๋ฉ . ํธ๋์ญ์ ๋งค๋์ ธ, ํธ๋์ญ์ ํ ํ๋ฆฟ ํด๋์ค ์ด์ฉ
2)์ ์ธ์ ํธ๋์ญ์ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ xml์ค์
3)์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ @์ด๋ ธํ ์ด์
ํธ๋์ญ์ ์ค์ต
ํธ๋์ญ์ ์ฒ๋ฆฌ ์ค์ต
1)๊ณต์ง์ฌํญ ์๊ธ์์ฑ +
noticeํ ์ด๋ธ insert
2)point์นผ๋ผ๊ฐ์ <3 ์ฒดํฌ์ ์ฝ์กฐ๊ฑด ์ค์
3)noticeํ ์ด๋ธ pk ์ ์ฝ์กฐ๊ฑด ์ค์
4)notice ํ ์ด๋ธ์ title ์นผ๋ผ๊ฐ์ unique ์ ์ฝ์กฐ๊ฑด ์ค์
1๏ธโฃSQL
ALTER TABLE member
ADD ( point number(10) default(0) );
ALTER TABLE member
ADD CONSTRAINT ck_member_point CHECK( point < 3 );
ALTER TABLE notices
ADD CONSTRAINT pk_notice_seq PRIMARY KEY( seq );
ALTER TABLE notices
ADD CONSTRAINT uk_notice_title unique( title);
2๏ธโฃMember.java
//ํธ๋์ญ์
์ฒ๋ฆฌ ์ฒดํฌ์ํด์ point ์นผ๋ผ ์ถ๊ฐ
private int point;
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
3๏ธโฃjoin.jsp
// action="join.htm"
<div class="top-wrapper clear">
<div id="content">
<form action="join.htm" method="post">
<h2>ํ์๊ฐ์
</h2>
//form ํ๊ทธ ์์ ๊ฐ์
๋ฒํผ ๋ฃ๊ธฐ
<div id="buttonLine">
<input class="btn-okay button" type="submit" value="๊ฐ์
" />
</div>
</form>
4๏ธโฃ
์ฐ์ ์ฒ๋ฆฌํ ๊ฒ์ด ์๊ธ(insert)+ ์์ฑ์์ point ์นผ๋ผ1์ฆ๊ฐ(update) ํธ๋์ญ์ ์ฒ๋ฆฌ ์๋ ์ํ
interface NoticeDao์ ์ถ๊ฐ
//ํธ๋์ญ์
ํ
์คํธ ์ฉ๋์ ๋ฉ์๋ ์ถ๊ฐ
public void insertAndPointUpOfMember(Notice notice, String id) throws ClassNotFoundException, SQLException;
5๏ธโฃNLNoticeDao
@Override
public void insertAndPointUpOfMember(Notice notice, String id) throws ClassNotFoundException, SQLException {
//์ ๊ณต์ง์ฌํญ ์ฐ๊ธฐ
String sql = "INSERT INTO NOTICES"
+ "(SEQ, TITLE, CONTENT, WRITER, REGDATE, HIT, FILESRC) "
+ "VALUES"
+ "( (SELECT MAX(TO_NUMBER(SEQ))+1 FROM NOTICES), :title, :content, :writer, SYSDATE, 0, :filesrc)";
SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(notice);
this.jdbcTemplate.update(sql, parameterSource);
//ํฌ์ธํธ 1์ฆ๊ฐ
sql="UPDATE member"
+ "SET point =point+1"
+ "WHERE id=:id";
MapSqlParameterSource paramSource2 = new MapSqlParameterSource();
paramSource2.addValue("id",id);
this.jdbcTemplate.update(sql, paramSource2);
}
์ปจํธ๋กค๋ฌ์ noticeReg ๋ฉ์๋
๋ง์ง๋ง์ ์ด๋ถ๋ถ ์ถ๊ฐ
this.noticeDao.insertAndPointUpOfMember(notice, "kenik");
return "redirect:notice.htm";
//๊ธ์ฐ๊ธฐ+์ฒจ๋ถ๋ ํ์ผ
@RequestMapping( value = {"noticeReg.htm"}, method = RequestMethod.POST )
public String noticeReg(Notice notice ,HttpServletRequest request ) throws Exception {
//1 ์ฒจ๋ถ๋ ํ์ผ์ ์ ๋ฌด ํ์ธ ํ์ ํ์ผ์ ์ฅ
CommonsMultipartFile multipartFile= notice.getFile();
String uploadRealPath=null;
if(!multipartFile.isEmpty()) { //์ฒจ๋ถ๋ ํ์ผ์ด ์๋ค๋ฉด
uploadRealPath =request.getServletContext().getRealPath("/customer/upload");
System.out.println("uplaodRealPath:"+uploadRealPath);
String originalFilename=multipartFile.getOriginalFilename(); //์ค์ฒ "a.txt" ->"a_1.txt"
//์ด๋ ๊ฒ ์ธ๋ฑ์ค๋ฅผ ๋ถ์ด๋ ํจ์ (์ด๋ฏธ ์กด์ฌํ ์ ๋ ์์ผ๋๊น)
String filesystemName = getFileNameCheck(uploadRealPath, originalFilename);
File dest=new File(uploadRealPath,filesystemName);
multipartFile.transferTo(dest); //์ค์ง์ ์ผ๋ก ํ์ผ์ ์ ์ฅ
//originalFilename , filesystemName Notices ํ
์ด๋ธ์ ์นผ๋ผ์ด 2๊ฐ ์กด์ฌ
String filesrc=filesystemName; //์ ์ฅํ๊ณ ์ํ๋ ํ์ผ
notice.setFilesrc(filesrc);
}
//2.Notices ํ
์ด๋ธ์ ๊ณต์ง์ฌํญ INSERT
notice.setWriter("kenik");
/* int rowCount = this.noticeDao.insert(notice);
if (rowCount==1) {
return "redirect:notice.htm";
}else {
return "redirect:noticeReg.jsp?error";
}*/
this.noticeDao.insertAndPointUpOfMember(notice, "kenik");
return "redirect:notice.htm";
} // noticeReg
์ด์ ๋ถํฐ ์คํ๋ง์ผ๋ก ํธ๋์ญ์ ์ฒ๋ฆฌ -2)์ ์ธ์ ํธ๋์ญ์ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ xml์ค์
1๏ธโฃweb.xml์ ์ถ๊ฐ
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
์ ์ฒด์ฝ๋
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<!-- NamedParameterJddbcTemplate ํ
ํ๋ฆฟ ํด๋์ค ์ฌ์ฉ -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<!-- p507 ์คํ๋ง ํธ๋์ญ์
์ ์ฒ๋ฆฌํ ํธ๋์ญ์
๋ฉ๋์ ธ ๋ฑ๋ก -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
</beans>
2๏ธโฃNLNoticeDao.java์ ์ถ๊ฐ
@Autowired
private TransactionTemplate transactionTemplate;
3๏ธโฃp516
ํธ๋์ญ์ ์ฒ๋ฆฌ๊ฐ ํ์ํ ์ฅ์(๋ฉ์๋)
NLNoticeDao.java - insertAndPointUpOfMember()
//ํธ๋์ญ์
๋งค๋์ ๋ฅผ ์ฌ์ฉํด์ ํธ๋์ญ์
์ฒ๋ฆฌ
@Override
public void insertAndPointUpOfMember(Notice notice, String id) throws ClassNotFoundException, SQLException {
//์ ๊ณต์ง์ฌํญ ์ฐ๊ธฐ
String sql = " INSERT INTO NOTICES"
+ " (SEQ, TITLE, CONTENT, WRITER, REGDATE, HIT, FILESRC) "
+ " VALUES"
+ " ( (SELECT MAX(TO_NUMBER(SEQ))+1 FROM NOTICES), :title, :content, :writer, SYSDATE, 0, :filesrc)";
//ํฌ์ธํธ 1์ฆ๊ฐ
String sql2=" UPDATE member"
+ " SET point =point+1"
+ " WHERE id=:id";
/// TransactionCallback+ WithoutResult
//์ฒ๋ฆฌํ๊ณ ๋๋ด์ ๋ฐํํ ๊ฒฐ๊ณผ๊ฐ์ด ์์๊ฒฝ์ฐ
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus ts) {
SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(notice);
//์ด ์์์ this๋ผ๊ณ ์ฐ๋ฉด ์๋๋ค. TransactionCallbackWithoutResult() ์ this ๋ผ๋ ๋ป์ด์ด์
// this.jdbcTemplate.update(sql, parameterSource);
jdbcTemplate.update(sql, parameterSource);
MapSqlParameterSource paramSource2 = new MapSqlParameterSource();
paramSource2.addValue("id",id);
jdbcTemplate.update(sql2, paramSource2);
}
});
}
๊ธ์์ฐ๋
ํ์ง๋ง ๋ ํ๋ฒ ๊ธ์ ์ฐ๋ ค๊ณ ํ๋ ์๋ฌ๋ฐ์ - > ๊ทธ๋์ ํ๋ก์ ํธ ์๋ก ์์ฑ ํด์ ์ ์ธ์ ๋ฐฉ๋ฒ ์๋
springTransaction03
์ ์ธ์ ํธ๋์ญ์ ์ฒ๋ฆฌ
- ์ ์ธ์ ํธ๋์ญ์ ? ํธ๋์ญ์ ๋งค๋์ ๊ฐ์ฒด
- ํธ๋์ญ์ ํ ํ๋ฆฟ ํด๋์ค ๊ฐ์ฒด ์์ฑ springTransaction X
- ํธ๋์ญ์
์ฒ๋ฆฌ๋ฅผ ์ฝ๋์์ ์ง์ ์ํํ์ง ์๊ณ
- xml ์ค์ ํ์ผ - springTransaction02
- <tx:advice> ํ๊ทธ์ด์ฉ - ํธ๋์ญ์ ์์ฑ ์ ์ํ๋ ํ๊ทธ
- <aop:config> ํ๊ทธ - ํธ๋์ญ์ ๋ ๋ด๋ถ์ ์ผ๋ก AOP ๋ฅผ ์ฌ์ฉํ๋ค.
- @Transaction ์ด๋ ธํ ์ด์ - springTransaction03
- ํธ๋์ญ์ ์ฒ๋ฆฌ ๋ฒ์, ๋กค๋ฐฑ ๊ท์น ๋ฑ๋ฑ ์ ์ํด์ ์ฒ๋ฆฌ
- xml ์ค์ ํ์ผ - springTransaction02
1๏ธโฃweb.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
2๏ธโฃNLNoticeDao.java
// [3]
// [tx:advice + aop:config] ์ ์ธ์ ํธ๋์ญ์
์ฒ๋ฆฌ.
@Override
public void insertAndPointUpOfMember(Notice notice, String id) throws ClassNotFoundException, SQLException {
// 1. ์ ๊ณต์ง์ฌํญ ์ฐ๊ธฐ
String sql = "INSERT INTO NOTICES"
+ " ( SEQ, TITLE, CONTENT, WRITER, REGDATE, HIT, FILESRC) "
+ " VALUES"
+ " ( (SELECT MAX(TO_NUMBER(SEQ))+1 FROM NOTICES), :title, :content, :writer, SYSDATE, 0, :filesrc )";
// 2. ํฌ์ธํธ 1์ฆ๊ฐ
String sql2 = "UPDATE member "
+ " SET point = point + 1 "
+ " WHERE id = :id ";
// 1.
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(notice);
jdbcTemplate.update(sql , paramSource );
// 2
MapSqlParameterSource paramSource2 = new MapSqlParameterSource();
paramSource2.addValue("id", id);
jdbcTemplate.update(sql2, paramSource2);
} // insertAndPointUpOfMember
์๋ฌ๋ฉ์ธ์ง
javax.servlet.ServletException: ์๋ธ๋ฆฟ [dispatcher]์(๋ฅผ) ์ํ Servlet.init() ํธ์ถ์ด ์์ธ๋ฅผ ๋ฐ์์์ผฐ์ต๋๋ค.
springTransaction04
์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ
@transactional ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด ์๋์ ์ผ๋ก ํธ๋์ญ์ ์ฒ๋ฆฌ๊ฐ ๋๋ค.
dispatcher-servlet.xml ์์ <tx:annotation> ์ถ๊ฐ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="newlecture.dao, controllers" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="-1"></property>
</bean>
<!-- ์ถ๊ฐ -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insertAndPointUpOfMember"
isolation="DEFAULT"
propagation="REQUIRED"
read-only="false"
timeout="-1"
/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public void insertAndPointUpOfMember(*, *))" id="insertOrM"/>
<aop:pointcut expression="within(newlecture.dao.*)" id="insertPackage"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="insertOrM"/>
</aop:config>
</beans>
NLNoticeDAO.java
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = -1)
public void insertAndPointUpOfMember(Notice notice, String id) throws ClassNotFoundException, SQLException {
// 1. ์ ๊ณต์ง์ฌํญ ์ฐ๊ธฐ
String sql = "INSERT INTO NOTICES"
+ " ( SEQ, TITLE, CONTENT, WRITER, REGDATE, HIT, FILESRC) "
+ " VALUES"
+ " ( (SELECT MAX(TO_NUMBER(SEQ))+1 FROM NOTICES), :title, :content, :writer, SYSDATE, 0, :filesrc )";
// 2. ํฌ์ธํธ 1์ฆ๊ฐ
String sql2 = "UPDATE member "
+ " SET point = point + 1 "
+ " WHERE id = :id ";
// 1.
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(notice);
jdbcTemplate.update(sql , paramSource );
// 2
MapSqlParameterSource paramSource2 = new MapSqlParameterSource();
paramSource2.addValue("id", id);
jdbcTemplate.update(sql2, paramSource2);
} // insertAndPointUpOfMember
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ์คํ๋ง ๋ ๊ฑฐ์ ํ๋ก์ ํธ 4.0๋ฒ์ (0) | 2022.07.20 |
---|---|
[Spring] ์คํ๋ง ํธ๋์ญ์ ๊ฒฉ๋ฆฌ์ฑ (0) | 2022.07.19 |
[Spring] NamedParameterJdbcTemplate (0) | 2022.07.18 |
[Spring] JDBC (0) | 2022.07.17 |
[Spring] ํ์ผ์ ๋ก๋ (0) | 2022.07.15 |