MemberController.java에 @RequestMapping 어노테이션으로 value=member_insert.me를 적고 method=RequestMethod.GET 혹은 method=RequestMethod.POST 방식으로 구분하여 단순히 폼으로 이동 memberForm 하는 건지 db에 회원정보가 입력 dao.insert(user)되도록 하는 건지 분리되어 있다.
package com.person.model;
import java.util.List;
public interface PersonDAO {
//추가
public void personInsert(Person p);
//전체보기
public List<Person> personList();
//수정
public void personUpdate(Person p);
//삭제
public void personDelete(String id);
//상세보기
public Person personView(String id);
}
Repository, Autowired 어노테이션을 통해 자동으로 springtest-servlet.xml과 연결된다.
💡
<지금까지의 스프링 컨테이너 이용 작업 순서> 1. springtest-servlet.xml에서 찾을 패키지 등록 2. com.person 하위에서 가장 먼저 controller이 찾아짐. PersonController의 상단에 있는 @Controller 어노테이션에 의해 스프링 컨테이너에 PersonController 객체가 들어감. 3. com.person 그 다음 하위인 model 패키지 내의 Person.java에는 @Component 어노테이션이 붙었음. (getter, setter는 어노테이션을 떼도 된다.) 4. Person.java의 객체들을 인식한뒤 PersonDAOImpl.java로 넘어가서 template을 자동으로 연결한다.
package com.person.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDAOImpl implements PersonDAO{
@Autowired
private JdbcTemplate template;
//추가
public void personInsert(Person p) {
String sql="insert into person values(?,?,?,?,?)";
Object[]param=new Object[] {
p.getId(),p.getName(),p.getPassword(),p.getGender(),p.getJob()
};
template.update(sql, param);
}
//전체보기
public List<Person> personList() {
String sql="select * from person";
List<Person> personlist=template.query(sql, new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int arg1) throws SQLException {
Person p=new Person();
p.setGender(rs.getString("gender"));
p.setId(rs.getString("id"));
p.setJob(rs.getString("job"));
p.setName(rs.getString("name"));
p.setPassword(rs.getString("password"));
return p;
}
});
return personlist;
}
//수정하기
public void personUpdate(Person p) {
String sql="update person set job=?, gender=?, name=?, password=? where id=?";
Object[] param=new Object[] {
p.getJob(),p.getGender(),p.getName(),p.getPassword(),p.getId()
};
template.update(sql, param);
}
//삭제하기
public void personDelete(String id) {
String sql="delete from person where id='"+id+"'";
template.update(sql);
}
//상세보기
public Person personView(String id) {
String sql="select * from person where id='"+id+"'";
Person p=template.queryForObject(sql, new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int arg1) throws SQLException {
Person user=new Person();
user.setGender(rs.getString("gender"));
user.setId(rs.getString("id"));
user.setJob(rs.getString("job"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
return user;
}
});
return p;
}
}
그 외 personForm.jsp, personList.jsp, personView.jsp, updateForm.jsp는 지난 시간과 거의 똑같다.
animals=["개", "고양이", "스컹크", "아나콘다", "코끼리", "하이에나"]
for animal in animals:
print(animal)
# range()
# (숫자) 0~숫자-1까지
for n in range(3):
print(n)
#구구단 2단 출력
for i in range(1,10):
print("{}X{}={}".format(2,i,2*i))
#구구단 2단~9단 출력
for i in range(2,10):
for j in range(1,10):
print('{}X{}={}'.format(i,j,i*j), end=" ")
print(end='\n')
연습문제
#연습문제 1
a="Life is too short, you need python"
if "wife" in a:
print("wife")
elif "python" in a and "you" not in a:
print("python")
elif "shirt" not in a:
print("shirt")
elif "need" in a:
print("need")
else:
print("none")
만약 elif 문에 Bob이 아니라 bob이었다면 '누구신가요?' 가 출력된다. 대소문자 구분.
#예제
name2=input("이름: ")
if name2:
print("당신의 이름은 {name2} 입니다.")
else:
print("이름을 입력하지 않았군요!")
while 반복문
#while 반복문
count=0
while count<3:
print('횟수: ',count)
count+=1
treeHit=0
while treeHit<10:
treeHit+=1
print(f"나무를 {treeHit}번 찍습니다")
if treeHit==10:
print("나무가 넘어갑니다.")
coffee=10
while True:
money=int(input("돈을 넣어 주세요: "))
if money==300:
print("커피를 줍니다.")
coffee=coffee-1
elif money>300:
print("거스름돈 %d를 주고 커피를 줍니다." %(money -300))
coffee=coffee-1
else:
print("돈을 다시 돌려주고 커피를 주지 않습니다.")
print("남은 커피의 양은 %d개 입니다." %coffee)
if coffee==0:
print("커피가 다 떨어졌습니다. 판매를 중지합니다.")
break
#bool(참, 거짓 형)
bool1=True
bool2=False
bool3=1<2
bool4=1==2
print(bool1,bool2,bool3,bool4)
x,y=1,2
print(x>y)
print(x==y)
print(x!=y)
불린타입 변환 bool()
#불린타입 변환 bool()
print(bool(1))
print(bool(0))
print(bool('True'))
print(bool('안녕'))
논리연산자
#논리연산자
#and: 둘다 true 일때
#or: 둘 중 하나가 true일때
#not: true면 false, false면 True
print(True and True)
print(True and False)
print(False and True)
print(False and False)
print("-------")
#or
print(True or True)
print(True or False)
print(False or True)
print(False or False)
print("-------")
#not
print(not True)
print(not False)
print("-------")