SpringWeb增删改查

模型类:

packagecom;publicclassModel{privateintid;privateString name;privateString dtype;privateString hospital;publicintgetId(){returnid;}publicvoidsetId(intid){this.id = id;}publicStringgetName(){returnname;}publicvoidsetName(String name){this.name = name;}publicStringgetDtype(){returndtype;}publicvoidsetDtype(String dtype){this.dtype = dtype;}publicStringgetHospital(){returnhospital;}publicvoidsetHospital(String hospital){this.hospital = hospital;}    }

连接类:

packageutil;importjava.io.IOException;importjava.io.Reader;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;publicclassMyBatisUtil{privatefinalstaticSqlSessionFactory sqlSessionFactory;//单例SqlSessionFactory对象static{String resource ="mybatis-config.xml";//MyBatis配置文件,位于src路径Reader reader =null;try{reader = Resources.getResourceAsReader(resource);}catch(IOException e) {System.out.println(e.getMessage());}sqlSessionFactory =newSqlSessionFactoryBuilder().build(reader);}publicstaticSqlSessionFactorygetSqlSessionFactory(){//返回单例SqlSessionFactory对象returnsqlSessionFactory;}}

接口类:

packagemapper;importjava.util.List;importcom.Model;publicinterfaceCrMapper{// 获取ID对象publicModelselectModel(intid);//插入一条记录publicvoidaddModel(Model model);//删除一条记录publicModeldeletModel(intid);//更新Model对象publicvoidupdateModel(Model model);//获取Model全部对象,并封装成一个集合publicListallModel();}

接口映射xml:

-insert into a(id,name,dtype,hospital) values(#{id},#{name},#{dtype},#{hospital})update a set name=#{name},dtype=#{dtype}, hospital=#{hospital} where id=#{id}delete from a where id=#{id}select * from a where id=#{id}select * from a

测试类:

packagetest;importjava.util.List;importmapper.CrMapper;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importutil.MyBatisUtil;importcom.Model;publicclassTest{publicstaticvoidmain(String[] args){SqlSessionFactory factory=MyBatisUtil.getSqlSessionFactory();    SqlSession session=factory.openSession();//实例化对象Model model=newModel(); model.setName("cc"); model.setDtype("33"); model.setHospital("33");    CrMapper crMapper = session.getMapper(CrMapper.class);    crMapper.addModel(model);//提交session.commit();//关闭session.close();  }//新增一条记录publicvoidtestCr(){    SqlSessionFactory factory=MyBatisUtil.getSqlSessionFactory();    SqlSession session=factory.openSession();//实例化对象Model model=newModel(); model.setName("cc"); model.setDtype("33"); model.setHospital("33");    CrMapper crMapper = session.getMapper(CrMapper.class);    crMapper.addModel(model);//提交session.commit();//关闭session.close();  }publicvoidtestAll(){//查询所有SqlSession session =MyBatisUtil.getSqlSessionFactory().openSession();    CrMapper crMapper=session.getMapper(CrMapper.class);    List list=crMapper.allModel();for(Model model:list){//显示model对象信息System.out.println("id:"+model.getId()+"\t姓名:\t"+model.getName()+"\t类型:\t"+model.getDtype()+"\t医生:\t"+model.getHospital());    }//关闭session.close();    }publicvoidtestuptate(){//修改SqlSession session=MyBatisUtil.getSqlSessionFactory().openSession();    CrMapper crMapper=session.getMapper(CrMapper.class);//根据id查询后修改字段Model model=newModel();    model=crMapper.selectModel(3);    model.setName("王菲");    model.setDtype("外科");    model.setHospital("1");    crMapper.updateModel(model);    session.commit();    session.close();    }publicvoidtestdelete(){    SqlSession session=MyBatisUtil.getSqlSessionFactory().openSession();    CrMapper crMapper=session.getMapper(CrMapper.class);//根据id删除crMapper.deletModel(2);//提交session.commit();//关闭session.close();    }}

你可能感兴趣的:(SpringWeb增删改查)