使用注解实现增,删,改,查。
public interface DeptMapper02 {
@Select("select * from dept")
List<Dept> selectAll();
@Select("select * from dept where deptno=#{deptno}")
Dept selectOne(int deptNo);
@Insert("insert into dept values (#{deptno},#{dname},#{loc})")
int insert(Dept dept);
@Update("update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}")
int update(Dept dept);
@Delete("delete from dept where deptno=#{0}")
int delete(int deptNo);
}
public class Test4 {
public static void main(String[] args) throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis05.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession session = factory.openSession();
DeptMapper02 mapper02=session.getMapper(DeptMapper02.class);
List<Dept> list = mapper02.selectAll();
for(Dept dept:list){
System.out.println(dept);
}
Dept list1=mapper02.selectOne(30);
System.out.println(list1);
Dept dept=new Dept(30,"卫生部","青岛");
int up=mapper02.update(dept);
System.out.println(up);
/*Dept dept1=new Dept(50,"体育部","深圳");
int insert=mapper02.insert(dept1);
System.out.println(insert);*/
int del=mapper02.delete(50);
System.out.println(del);
session.commit();
session.close();
}
}