728x90
반응형
1. DB 접근 기술이란?
2. MyBatis란?
3. UserRepository - user.xml
package com.tenco.bank.repository.interfaces;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.tenco.bank.repository.model.User;
// MyBatis 설정 확인
// UserRepository 인터페이스와 user.xml 파일을 매칭시킨다.
@Mapper // 반드시 선언해야 동작한다.
public interface UserRepository {
public int insert(User user);
public int updateById(User user);
public int deleteById(Integer id);
public User findById(Integer id);
public List<User> findAll();
}
<?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.tenco.bank.repository.interfaces.UserRepository">
<!-- 반드시! 세미콜론을 제거해야 한다. -->
<!-- User-insert() 기능 구현하기 -->
<!-- id는 매칭되어있는 인터페이스의 메서드 명과 같아야 한다. -->
<insert id="insert">
insert into user_tb(username,password,fullname,created_at)
values( #{name}, #{password}, #{fullname}, #{createdAt} )
</insert>
<update id="updateById">
update user_tb set username = #{username},
password = #{password},
fullname = #{fullname}
where id = #{id}
</update>
<delete id="deleteById">
delete from user_tb where id = #{id}
</delete>
<select id="findById" resultType="com.tenco.bank.repository.model.User">
select * from user_tb where id = #{id}
</select>
<select id="findAll" resultType="com.tenco.bank.repository.model.User">
select * from user_tb
</select>
</mapper>
4. AccountRepository - account.xml
package com.tenco.bank.repository.interfaces;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.tenco.bank.repository.model.Account;
// AccountRepository 인터페이스와 account.xml 파일을 매칭시킨다.
@Mapper
public interface AccountRepository {
public int insert(Account account);
public int updateById(Account account);
public int deleteById(Integer id);
// 고민! - 계좌 조회
// -> 한 사람의 유저는 여러 개의 계좌 번호를 가질 수 있다.
// 유저:계좌 -->1:N
// 두 개 이상의 파라미터 값을 받을 경우 @Param이 필수로 쓰인다.
public List<Account> findByUserId(@Param("userId") Integer principalId);
// -> 하나의 account id 값으로 계좌 정보 조회 기능
public Account findByNumber(@Param("number") String id);
}
<?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.tenco.bank.repository.interfaces.AccountRepository">
<!-- 반드시! 세미콜론을 제거해야 한다. -->
<!-- User-insert() 기능 구현하기 -->
<!-- id는 매칭되어있는 인터페이스의 메서드 명과 같아야 한다. -->
<insert id="insert">
insert into account_tb(number, password,balance,user_id,created_at)
values ( #{number}, #{password}, #{balance}, #{userId}, #{createdAt} )
</insert>
<update id="updateById">
update account_tb set password = #{password},
balance = #{balance},
user_id = #{userId},
created_a = #{createdAt}
</update>
<delete id="deleteById">
delete account_tb where id = #{id}
</delete>
<select id="findByUserId" resultType="com.tenco.bank.repository.model.Account">
select account_tb where user_id = #{userId}
</select>
<select id="findByNumber" resultType="com.tenco.bank.repository.model.Account">
select account_tb where number = #{number}
</select>
</mapper>
5. HistoryRepository - history.xml
package com.tenco.bank.repository.interfaces;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.tenco.bank.repository.model.History;
// HistoryRepository, history.xml 파일을 매칭시킨다.
@Mapper
public interface HistoryRepository {
public int insert(History history);
public int updatebyid(History history);
public int deleteById(Integer id);
// 거래 내역 조회
public History findById(Integer id);
public List<History> findAll();
// 코드 추가 예정 - 모델을 반드시 1: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="com.tenco.bank.repository.interfaces.HistoryRepository">
<insert id="insert">
insert into history_tb(amount, w_account_id, d_account_id,w_balance,d_balance)
values(#{amount}, #{wAccountId}, #{dAccountId}, #{wBalance}, #{dBalance})
</insert>
<update id="updatebyid">
update history_tb set amount = #{amount},
w_account_id = #{wAccountId},
d_account_id = #{dAccountId},
w_balance = #{wBalance},
d_balance = #{dBalance}
where id = #{id}
</update>
<delete id="deleteById">
delete history_tb where id = #{id}
</delete>
<select id="findById" resultType="com.tenco.bank.repository.model.history">
select * from history_tb where id = #{id}
</select>
<select id="findAll" resultType="com.tenco.bank.repository.model.history">
select * from history_tb
</select>
</mapper>
728x90
반응형
'Springboot' 카테고리의 다른 글
[SpringBoot] 12.코드 정리 (0) | 2024.08.06 |
---|---|
[SpringBoot] 11. 회원가입 - 트랜잭션, 예외 처리,H2 테이블 (2) | 2024.08.06 |
[SpringBoot] 9. 어노테이션 정리 (0) | 2024.08.06 |
[SpringBoot] 8. 에러 페이지 생성 (0) | 2024.08.06 |
[SpringBoot] 7. Exception Handler 처리 (0) | 2024.08.05 |