1.查询:
1)修改BeanPerson.xml:
2)修改接口:
package cn.cvu.service;import java.util.List;import cn.cvu.domain.BeanPerson;//接口,定义操作public interface IOperationBean { //查询全部的Person,方法名对应BeanPerson.xml中配置的select节点的id public ListselectPersons();}
3)测试:
/** * 一次查询全部数据 * @throws Exception */ @Test public void testQueryAll() throws Exception { Reader reader = Resources.getResourceAsReader("Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); IOperationBean operationBean = session.getMapper(IOperationBean.class); //查询全部数据。返回JavaBean=接口.使用ID查询(Integer) Listpersons = operationBean.selectPersons(); for (BeanPerson person:persons){ System.out.println("ID:" + person.getId()); System.out.println("姓名:" + person.getName()); System.out.println("国家:" + person.getCountry()); System.out.println("地址:" + person.getAddress()); } session.close(); }
2.添加:
1)修改接口:
//接口,定义操作public interface IOperationBean { //添加条目 public void addPerson(BeanPerson person);}
2)修改BeanPerson.xml:
insert into tb_person(id, name, country, address) values( #{id}, #{name}, #{country}, #{address} )
3)测试:
/** * 插入一条记录 * @throws Exception */ @Test public void testInsert() throws Exception { Reader reader = Resources.getResourceAsReader("Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); IOperationBean operationBean = session.getMapper(IOperationBean.class); //1.准备数据 BeanPerson person=new BeanPerson(3, "Eminem", "America", "Ditroit"); //2.执行插入 operationBean.addPerson(person); //3.提交事务 session.commit(); session.close(); }
3.更新:
1)修改IOperationPerson接口:
//接口,定义操作public interface IOperationBean { //根据id更新条目 public void updatePerson(BeanPerson person);}
2)修改BeanPerson.xml文件:
update tb_person set name=#{name},country=#{country},address=#{address} where id=#{id}
3)测试:
/** * 更新一条记录 * @throws Exception */ @Test public void testUpdateOne() throws Exception { Reader reader = Resources.getResourceAsReader("Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); IOperationBean operationBean = session.getMapper(IOperationBean.class); //1.准备数据(原ID,新name,新country,新address) BeanPerson person=new BeanPerson(2, "NewMichael", "America", "New York"); //2.执行更新 operationBean.updatePerson(person); //3.提交事务 session.commit(); session.close(); }
4.删除:
1)修改接口:
package cn.cvu.service;import java.util.List;import cn.cvu.domain.BeanPerson;//接口,定义操作public interface IOperationBean { //根据id更新条目 public void deletePerson(int id);}
2)修改BeanPerson.xml:
delete from tb_person where id=#{id}
3)测试:
/** * 删除一条记录 * @throws Exception */ @Test public void testDeleteOne() throws Exception { Reader reader = Resources.getResourceAsReader("Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); IOperationBean operationBean = session.getMapper(IOperationBean.class); //1.准备数据 Integer id = 2 ; //2.执行更新 operationBean.deletePerson(id); //3.提交事务 session.commit(); session.close(); }
- end