Mybatis 주석 기반 CRUD

2207 단어 Java
실체 류
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Component
@ToString
public class Student {
    private int id;
    private String name;
    private String number;
    private boolean gender;
}

인터페이스
@Repository
public interface StudentDao {

    @Insert("insert into tb_student values(#{id}, #{name}, #{number}, #{gender})")
    public int insert(Student student);

    @Update("update tb_student set name = #{name}, number = #{number}, gender = #{gender} where id = #{id}")
    public void update(Student student);

    @Delete("delete tb_student where id = #{id}")
    void delete(Integer id);

    @Select("select * from tb_student where id = #{id}")
    Student detail(Integer id);

    @Select("select *from tb_student")
    List list();

    @Select("select * from tb_student where name = #{name} and number = #{number}")
    Student detail2(@Param("name") String name, @Param("number") String number);

}

테스트
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:app.xml")
public class StudentDaoTest {
    @Autowired
    private StudentDao studentDao;
    @Test
    public void insert(){
        Student build = Student.builder().id(1).name("a").number("aa").gender(true).build();
        int insert = studentDao.insert(build);
        System.out.println("********* " + insert);
    }

    @Test
    public void list(){
        List list = studentDao.list();
        for(Student stu: list){
            System.out.println(stu);
        }
    }

    @Test
    public void update(){
        Student student = new Student(1, "bb", "bbb", true);
        studentDao.update(student);

    }
    @Test
    public void detail(){
        Student detail = studentDao.detail(1);
        System.out.println(detail);
    }

    @Test
    public void detail2(){
        Student student = studentDao.detail2("bb", "bbb");
        System.out.println(student);
    }

    @Test
    public void delete(){
        studentDao.delete(1);
    }
}

좋은 웹페이지 즐겨찾기