sql>tbl_bank 테이블 만들기>은행 테이블만들기
create table tbl_bank(
bno char(4) primary key,/* 계좌번호 */
bname varchar(20), /* 계좌주 이름 */
bdate datetime default now(), /* 계좌만든날짜 */
balance double /* 계좌에 있는 금액 */
);
sql>tbl_account 테이블 만들기>계좌목록 테이블만들기
create table tbl_account(
aid int auto_increment primary key, /* 계좌목록아이디 */
bno char(4), /* 계좌번호 */
type char(1), /* 입금(1)/출금(2) */
tno char(4), /* 받는사람 계좌번호 */
amount double, /* 보낸금액 */
tdate datetime default now(), /* 보낸날짜 */
foreign key(bno) references tbl_bank(bno),
foreign key(tno) references tbl_bank(bno)
);
BankVO
package com.example.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class BankVO {
private String bno;
private String bname;
private Date bdate;
@JsonFormat(pattern="#,###")
private double balance;
public String getBno() {
return bno;
}
public void setBno(String bno) {
this.bno = bno;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public Date getBdate() {
return bdate;
}
public void setBdate(Date bdate) {
this.bdate = bdate;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "BankVO [bno=" + bno + ", bname=" + bname + ", bdate=" + bdate + ", balance=" + balance + "]";
}
}
BankDAO
package com.example.persistence;
import java.util.List;
import com.example.domain.BankVO;
public interface BankDAO {
public List<BankVO> list();
public BankVO read(String bno);
public void update(String bno,double amount);
}
BankMapper
<?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="BankMapper">
<select id="list" resultType="com.example.domain.BankVO">
select * from tbl_bank
</select>
<select id="read" resultType="com.example.domain.BankVO">
select * from tbl_bank
where bno=#{bno}
</select>
<update id="update">
update tbl_bank set balance= balance + #{amount}
where bno=#{bno}
</update>
</mapper>
BankDAOImpl
package com.example.persistence;
import java.util.HashMap;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.BankVO;
@Repository
public class BankDAOImpl implements BankDAO{
@Inject
SqlSession session;
String namespace="BankMapper";
@Override
public List<BankVO> list() {
// TODO Auto-generated method stub
return session.selectList(namespace +".list");
}
@Override
public BankVO read(String bno) {
// TODO Auto-generated method stub
return session.selectOne(namespace+".read",bno);
}
@Override
public void update(String bno, double amount) {
// TODO Auto-generated method stub
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("bno", bno);
map.put("amount", amount);
session.update(namespace+".update",map);
}
}
BankController
package com.example.web;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.domain.AccountVO;
import com.example.domain.BankVO;
import com.example.persistence.AccountDAO;
import com.example.persistence.BankDAO;
import com.example.service.AccountService;
@Controller
public class BankController {
@Inject
BankDAO dao;
@Inject
AccountDAO adao;
@Inject
AccountService service;
@ResponseBody
@RequestMapping("alist")
public List<AccountVO> alist(String bno){
return adao.list(bno);
}
@ResponseBody
@RequestMapping("ainsert")
public void ainsert(AccountVO vo){
service.insert(vo);
}
@RequestMapping("blist")
public String blist(Model model){
model.addAttribute("list",dao.list());
return "blist";
}
@RequestMapping("bread")
public String bread(Model model){
model.addAttribute("list",dao.list());
return "bread";
}
@ResponseBody
@RequestMapping("read.json")
public BankVO breadJson(String bno){
return dao.read(bno);
}
}
AccountVO
package com.example.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class AccountVO {
private int aid;
private String bname;
private String bno;
private String type; //1입급 2출금
private String tno;
private double amount;
@JsonFormat(pattern="yyyy-MM-dd",timezone="Asia/Seoul")
private Date tdate;
public int getAid() {
return aid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getBno() {
return bno;
}
public void setBno(String bno) {
this.bno = bno;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Date getTdate() {
return tdate;
}
public void setTdate(Date tdate) {
this.tdate = tdate;
}
@Override
public String toString() {
return "AccountVO [aid=" + aid + ", bno=" + bno + ", type=" + type + ", tno=" + tno + ", amount=" + amount
+ ", tdate=" + tdate + "]";
}
}
AccountDAO
package com.example.persistence;
import java.util.List;
import com.example.domain.AccountVO;
public interface AccountDAO {
public void insert(AccountVO vo);
public List<AccountVO> list(String bno);
}
AccountMapper
<?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="AccountMapper">
<insert id="insert">
insert into tbl_account(bno,type,tno,amount)
values(#{bno},#{type},#{tno},#{amount})
</insert>
<select id="list" resultType="com.example.domain.AccountVO">
select a.*,b.bname from tbl_account a,tbl_bank b
where a.bno=#{bno} and a.tno=b.bno
order by tdate desc;
</select>
</mapper>
AccountDAOImpl
package com.example.persistence;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.AccountVO;
@Repository
public class AccountDAOImpl implements AccountDAO{
@Inject
SqlSession session;
String namespace="AccountMapper";
@Override
public void insert(AccountVO vo) {
// TODO Auto-generated method stub
session.insert(namespace+".insert",vo);
}
@Override
public List<AccountVO> list(String bno) {
// TODO Auto-generated method stub
return session.selectList(namespace+".list",bno);
}
}
AccountService
package com.example.service;
import com.example.domain.AccountVO;
public interface AccountService {
public void insert(AccountVO vo);
}
AccountServiceImpl
package com.example.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.AccountVO;
import com.example.persistence.AccountDAO;
import com.example.persistence.BankDAO;
@Service
public class AccountServiceImpl implements AccountService{
@Inject
AccountDAO dao;
@Inject
BankDAO bdao;
@Transactional
@Override
public void insert(AccountVO vo) { //이체를 했을 때 발생
String bno=vo.getBno();
String tno=vo.getTno();
vo.setType("2");
dao.insert(vo);// 출금
//입금
String temp=vo.getBno();
vo.setBno(vo.getTno());
vo.setTno(temp);
vo.setType("1");
dao.insert(vo);
bdao.update(bno, vo.getAmount() * -1);
bdao.update(tno, vo.getAmount());
}
}
blist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table{
margin-top:20px;
border-top:1px solid gray;
}
td{
border-bottom:1px dotted gray;
}
</style>
</head>
<body>
<h1 style="margin:0px auto; text-align:center;margin-bottom:20px;">〓통장목록〓</h1>
<table border=0 width=700 style="margin:0px auto; text-align:center;">
<c:forEach items="${list}" var="vo">
<tr>
<td>${vo.bno}</td>
<td>${vo.bname}</td>
<td><fmt:formatDate value="${vo.bdate}" pattern="yyyy-MM-dd kk:mm:ss"/></td>
<td><fmt:formatNumber value="${vo.balance}" pattern="#,###만원"/></td>
<td><button onClick="location.href='bread?bno=${vo.bno}'">통장확인</button></td>
</tr>
</c:forEach>
</table>
</body>
</html>
bread
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table {
border-top: 1px solid gray;
}
td {
border-bottom: 1px dotted gray;
padding: 10px;
}
</style>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>
</head>
<body>
<h1 style="text-align: center;">▒통▒장▒내▒역▒</h1>
<table border=0 width=600 style="margin: 0px auto;">
<tr>
<td id="bno"></td>
<td id="bname"></td>
<td id="balance"></td>
</tr>
</table>
<div style="width: 600px; margin: 0px auto; margin-top: 20px;">
이체정보:
<select style="height: 22px;" id="tno">
<c:forEach items="${list}" var="vo">
<c:if test="${vo.bno != param.bno}">
<option value="${vo.bno}">${vo.bno}:${vo.bname}</option>
</c:if>
</c:forEach>
</select>
<input type="text" id="amount">
<input type="button" value="이체" id="btninsert">
<input type="button" value="목록" onClick="location.href='list'">
</div>
<table border=0 width=600 id="tbl"
style="margin: 0px auto; text-align: center; margin-top: 30px;"></table>
<script id="temp" type="text/x-handlebars-template">
<tr>
<td>날짜</td>
<td>타입</td>
<td>받는사람</td>
<td>금액</td>
</tr>
{{#each .}}
<tr>
<td>{{tdate}}</td>
<td>{{type}}</td>
<td>{{tno}}-{{bname}}</td>
<td>{{amount}}</td>
</tr>
{{/each}}
</script>
</body>
<script>
var bno = "${param.bno}";
getbank();
getlist();
function getlist() {
$.ajax({
type : "get",
url : "alist",
data : {
"bno" : bno
},
success : function(data) {
var temp = Handlebars.compile($("#temp").html());
$("#tbl").html(temp(data));
}
});
}
$("#btninsert").on("click", function() {
if (!confirm("이체하시겠습니까?"))
return;
var tno = $("#tno").val();
var amount = $("#amount").val();
var type = "2";
$.ajax({
type : "get",
url : "ainsert",
data : {
"bno" : bno,
"tno" : tno,
"amount" : amount,
"type" : type
},
success : function() {
alert("이체완료.....");
getlist();
getbank();
}
});
});
function getbank() {
$.ajax({
type : "get",
url : "read.json",
data : {
"bno" : bno
},
success : function(data) {
$("#bno").html(data.bno);
$("#bname").html(data.bname);
$("#balance").html(data.balance);
}
});
}
</script>
</html>
'spring' 카테고리의 다른 글
파일업로드 (0) | 2019.11.29 |
---|---|
게시판에 댓글을 달면 게시글의 댓글수가 추가됩니다. (0) | 2019.11.28 |
게시글 댓글 조회수를 추가한다. (0) | 2019.11.27 |
맛집목록 만들기 (0) | 2019.11.26 |
Transaction 처리 (0) | 2019.11.26 |