Mybatis---注解

使用注解实现增,删,改,查。

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值