1.jar包
2.mybatis-config.xm配置文件
3.dao
public interface IStudentDAO { public int addStu(Student stu) throws IOException; //删除 public int delStu(int id) throws IOException; //查询所有记录 public ListfindAll() throws IOException; //按照学生姓名查询学生集合 public List findStudntByName(Student stu) throws IOException; public List findStudntByName(String stuname) throws IOException; }
daoImpl
public class StudentDAOImpl implements IStudentDAO { SqlSession session ; public StudentDAOImpl() throws IOException { session= MybatisUtil.getSession(); } public ListfindStudntByName(String stuname) throws IOException { List list = session.selectList("findStudentByName",stuname); session.close(); return list; } /** * * 模糊查询 */ public java.util.List findStudntByName(Student stu) throws IOException { List list = session.selectList("findStudentByName",stu); session.close(); return list; } /** * 查询所有 */ public java.util.List findAll() throws IOException { List list = session.selectList("findAll"); session.close(); return list; } /** * 删除 */ public int delStu(int id) throws IOException { int result = session.delete("delStudent",id); session.commit(); session.close(); return result; } public int addStu(Student stu) throws IOException { int result = session.insert("insertStudent",stu); session.commit(); session.close(); return result; }}
dao.xml
insert into student(stuname,stuage,studate) values(#{stuname},#{stuage},#{studate}) select @@identity delete from student where stuno=#{xxx}
MyBatisUitl工具类
/** * 工具类 * @author Happy * */public class MybatisUtil { private static String config="mybatis-config.xml"; static Reader reader; static{ try { reader= Resources.getResourceAsReader(config); } catch (IOException e) { e.printStackTrace(); } } private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //提供一个可以获取到session的方法 public static SqlSession getSession() throws IOException{ System.out.println("22222"+factory); //弊病,就是工厂是 // 1.1 openSession到底做了什么 SqlSession session = factory.openSession(); System.out.println("3333"); return session; }}
单测
public class MyTest { IStudentDAO dao; @Before public void initData() throws IOException{ dao=new StudentDAOImpl(); } /** * 模糊查询 * @throws IOException */ @Test public void findStudentByName() throws IOException{ /*Student stu=new Student(); stu.setStuname("三");*/ Listlist = dao.findStudntByName("三"); for (Student student : list) { System.out.println(student.getStuname()); } } /** * selectALl学生 * @throws IOException */ @Test public void findAll() throws IOException{ List list = dao.findAll(); for (Student student : list) { System.out.println(student.getStuname()); } } /** * 删除学生 * @throws IOException */ @Test public void delStudent() throws IOException{ dao.delStu(2); System.out.println("ok"); } @Test public void testAdd() throws IOException{ Student stu=new Student(); stu.setStuname("张三"); stu.setStuage(21); stu.setStudate(new Date()); System.out.println("添加前"+stu); IStudentDAO dao=new StudentDAOImpl(); dao.addStu(stu); System.out.println("添加后"+stu); }}