首先入门案例(并且拿到新增记录当前id)
1.创建mybatis-config.xml文件
2.创建分层搭建架构
3.创建StudentDAO.xml
insert into student(id,name,address,sex) values(seq_ssm.nextval,#{name},#{address},#{sex}) select seq_ssm.currval from dual delete student where id=#{id} update student set name=#{name},address=#{address},sex=#{sex} where id=#{id}
4.实现接口方法
@Override public Integer addStudent(Student stu) { SqlSession sqlSession = SessionUtil.getSqlSession(); int result = sqlSession.insert("insertStudent", stu); sqlSession.commit(); sqlSession.close(); System.out.println("保存结果" + result); return result; }
5.测试
@Test public void frist() { Student stu = new Student("BBB", "北京", "男"); StudentDaoImpl dao = new StudentDaoImpl(); dao.addStudent(stu); System.out.println("成功"); }
结果图
注意事项:
语句后面不能加';'(分号),会报sql异常