μ΄μ Student λΌλ μ΄λ¦μ DAO λ₯Ό νλ μμ±ν΄λ³΄μ. μμλ λ€μκ³Ό κ°λ€.
- DAO Interface λ₯Ό define
- DAO Implement μ define (+inject entity manager)
- main app μ update
Step 1: Define DAO Interface
import com.lucvs.cruddemo.entity.Student;
public interface StudentDAO {
void save(Student theStudent);
}μμ κ°μ΄ Interface ννλ‘ StudentDAO λ₯Ό define νκ³ , save method λ₯Ό declare νλ€. save method λ Implementation μμ overriding μ ν΅ν΄ implement νμ¬ μ¬μ©νλ©΄ λ κ²μ΄λ€.
Step 2: Define DAO Implementation
public class StudentDAOImpl implements StudentDAO {
private EntityManager entityManager;
@Autowired
public StudentDAOImpl(EntityManager theEntityManager) {
entityManager = theEntityManager;
}
@Override
@Transactional
public void save(Student theStudent) {
entityManager.persist(theStudent);
}
}Constructor λΆλΆμμ, JPA Entity Manager μμ μΈκΈνλλ‘ Spring Boot κ° Entity Manager λ₯Ό μλμΌλ‘ μμ±ν΄μ£ΌκΈ° λλ¬Έμ @Autowired λ₯Ό ν΅νμ¬ EntityManager λ₯Ό injection ν΄μ£Όλ©΄ λλ€.
κ·Έλ¦¬κ³ StudentDAO Interface μμ declare ν save method λ₯Ό overriding νμ¬ implement νλ€. inject λ°μ EntityManger λ₯Ό persist method λ₯Ό ν΅νμ¬ Student object λ₯Ό database μ μ μ₯νλ€.
@Transactional annotation μ method λ class μ Transaction μ μ μ©ν μ μλλ‘ ν΄μ€λ€.
Transaction
Transaction μ Database μμ μ νλμ logic λ¨μλ‘ λ¬Άμ΄, λͺ¨λ μμ μ΄ μ±κ³΅μ μΌλ‘ μλ£λκ±°λ μ€ν¨ μ λͺ¨λ μ·¨μλλλ‘ λ³΄μ₯νλ κΈ°μ μ΄λ€.
Specialized Annotation for DAOs

Spring μ @Repository λΌλ annotation μ μ 곡νλ€. @Repository annotation μ @Component μ νΉμνλ ννλ‘, component-scanning μ ν΅νμ¬ DAO Implementation μ Bean μ ννλ‘ μλ λ±λ‘νλ€.
λνDatabase μμ
μ€ λ°μνλ λ€μν κΈ°μ μ’
μμ μΈ JDBC Exception(ex.Β SQLException,Β HibernateException λ±)λ€μ Springμ΄ μ 곡νλΒ DataAccessExceptionΒ κ³μ΄μ Exception μΌλ‘ λ³νν΄μ€λ€.
@Repository
public class StudentDAOImpl implements StudentDAO {
...
}μμ κ°μ΄ DAO Implementation class μ λνμ¬ @Repository annotation μ μ¬μ©ν΄μ£Όλ©΄ λλ€.
Step 3. Update Main Application
@SpringBootApplication
public class CruddemoApplication {
public static void main(String[] args) {
SpringApplication.run(CruddemoApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(StudentDAO studentDAO) {
return runner -> {
createStudent(studentDAO);
}
}
private void createStudent(StudentDAO studentDAO) {
// create the student object
...
// save the student object
...
// display id of the saved student
...
}
}κ·Έλ¦¬κ³ λ§μ§λ§μΌλ‘, StudentDAO λ₯Ό Main Application μ Inject νμ¬ Database μ Interacting ν μ μλ€.
Student DAO - Test (singular saving)
private void createStudent(StudentDAO studentDAO) {
// create the student object
System.out.println("Creating new student object ...");
Student tempStudent = new Student("Arne", "Slot", "arne@liverpool.com");
// save the student object
System.out.println("Saving the student ...");
studentDAO.save(tempStudent);
// display id of the saved student
System.out.println("Saved a student. Generated id: " + tempStudent.getId());
}createStudent method λ μμ κ°μ΄ μμ±ν μ΄νμ Application μ μ€ννμ¬ tempStudent object μ λν data κ° Database μ μ μ μ₯λμμμ§ ν
μ€νΈν΄λ³΄μ.

Application μ μ€ννλ©΄ console νλ©΄μ μμ κ°μ΄ code μ μ€ν μμμλ λ¬Έμ κ° μλ κ²μΌλ‘ λμλ€. κ·ΈλΌ μ€μ Database μ theStudent obejct κ° μ μ μ₯λμλμ§ νμΈν΄λ³΄μ.
SELECT * FROM student_tracker.student;μμ κ°μ΄ SELECT λ₯Ό μ΄μ©νμ¬ query λ₯Ό λ λ €μ£Όλ©΄ μ΄μ μ λ§λ€μ΄λμ student table μ μ‘°νν μ μλ€.

μ±κ³΅μ μΌλ‘ Database μ μ§μ μμ±ν theStudent object μ data κ° μ μ₯λ κ²μ λ³Ό μ μλ€.
κ²°κ³Όμμ λ³Ό μ μλ―μ΄, theStudent object λ₯Ό λ§λ€ λ Contructor μ argument μ id κ°μ λ£μ΄μ initialize ν΄μ£Όμ§ μμμμλ λΆκ΅¬νκ³ 1 μ΄λΌλ κ°μ΄ λ€μ΄κ° μλ€.
μ΄λ‘μ¨ μ΄μ μ Student Entity Class λ₯Ό λ§λ€ λ id field μ λνμ¬ μ§μ ν ID Generation Strategy μμλ μ μμ μΌλ‘ λμν κ²μ μ μ μλ€.
Student DAO - Test (multiple saving)
μ΄λ²μλ μ¬λ¬ κ°μ Student object λ₯Ό μμ±νμ¬ Database μ μ μ₯ν μ΄ν, PK(Primary Key) λ‘ μ€μ ν id column μ κ°λ€μ΄ μ μ μ₯λλμ§ νμΈν΄λ³΄μ.
idμ λνμ¬GenerationType.IDENTITYstrategy λ₯Ό μ¬μ©νκΈ° λλ¬Έμ, DB λ΄λΆμμλAUTO_INCREMENT` λ‘ λμνλ€.
μΆκ°μ μΌλ‘, MySQL Workbench μμ νΉμ table μ μ°ν΄λ¦νλ©΄ μμ κ°μ λ©λ΄κ° λμ€λλ°, μ¬κΈ°μ Alter Table
Changing Index of MySQL Auto Increment
κΈ°λ³Έμ μΌλ‘ Auto-Increment λ₯Ό μ¬μ©νκ² λλ©΄ index κ° 1 λΆν° μμλλ κ²μ νμΈν μ μμλ€. κ·Έλ¬λ λ€μ SQL query λ₯Ό ν΅νμ¬ μμ index λ₯Ό μνλ μ«μλ‘ λ°κΏ μ μλ€.
ALTER TABLE student_tracker.student AUTO_INCREMENT=1000
μ΄νμ Application μ μ€ννλ©΄ μμ κ°μ΄ PK μ μμ index κ° ALTER query λ₯Ό ν΅νμ¬ μ€μ ν 1000 λΆν° μμνλ κ²μ μ μ μλ€.
TRUNCATE student_tracker.studentμ°Έκ³ λ‘ μμ κ°μ΄ TRUNCATE query λ₯Ό μ€ννλ©΄ ν΄λΉ table μ λͺ¨λ row λ₯Ό μμ νλ€. primary key μμ μμ index λ‘ λ€μ μ΄κΈ°νλλ€.