昨天晚上看了一下关于iBatis的一个讲解的视频,讲的和我的这个简单的总结差不多....
思考了一下还是把主要操作都总结一下吧,当然这里也不是全的,知识简单的CRUD。。。
首先我觉得持久层的操作主要就是这几个:
public interface IPersonDao { //添加 public boolean addPerson(Person person); //更新 public boolean updatePerson(Person person); //删除 根据ID删除, 批量删除 public boolean deletePersonById(int id); public boolean deleteAll(List<Integer> ids); //查询,根据ID, name模糊查询, 查询所有, 多条件查询,分页查询 public Person queryById(int id); public List<Person> queryByName(String name); public List<Person> queryAllPerson(); public List<Person> queryPersons(Person person); public List<Person> queryPage(Map<String, Object> info); }
然后我们就一一实现吧。
Person类:
public class Person { private int id; private String name; private int age; //省去了getter setter }
Person.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Person"> <!-- 实体类路径和名 这里得到person是大小写不敏感的 --> <typeAlias alias="person" type="com.gbx.po.Person" /> <!-- SQL语句 --> <insert id="addPerson" parameterClass="person" > <!-- 利用自增字段来填充主键 --> <selectKey resultClass="int" keyProperty="id"> select LAST_INSERT_ID() as id </selectKey> insert into t_person(id, name, age) values(#id#, #name#, #age#) </insert> <update id="updatePerson" parameterClass="person"> update t_person set id=#id#, name=#name#, age=#age# where id=#id# </update> <delete id="deletePerson" parameterClass="int"> delete from t_person where id=#id# </delete> <delete id="deleteAllPerson" parameterClass="List"> delete from t_person where id in (<iterate conjunction=","> #[]# </iterate>) </delete> <select id="queryPersonById" parameterClass="int" resultClass="person"> select id, name, age from t_person where id=#id# </select> <select id="queryPersonByName" parameterClass="String" resultClass="person"> select id, name, age from t_person where name like '%$name$%' </select> <select id="queryAllPerson" resultClass="person" > select id, name, age from t_person </select> <select id="queryPersons" resultClass="person" parameterClass="person" > select id, name, age from t_person <dynamic prepend="where"> <isNotEqual prepend="and" property="id" compareValue="0"> id=#id# </isNotEqual> <isNotNull prepend="and" property="name"> name like '%$name$%' </isNotNull> <isGreaterEqual prepend="and" property="age" compareValue="0"> age>#age# </isGreaterEqual> </dynamic> </select> <select id="queryPage" parameterClass="java.util.Map" resultClass="person"> select id, name, age from t_person <dynamic prepend="where"> <isNotEqual prepend="and" property="id" compareValue="0"> id=#id# </isNotEqual> <isNotNull prepend="and" property="name"> name like '%$name$%' </isNotNull> <isNotEqual prepend="and" property="age" compareValue="0"> age>=#age# </isNotEqual> </dynamic> limit #begin#, #pageSize# </select> </sqlMap>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <!-- 引入资源 --> <properties resource="SqlMap.properties"/> <!-- 配置数据库连接信息 --> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}" /> <property name="JDBC.ConnectionURL" value="${url}" /> <property name="JDBC.Username" value="${username}" /> <property name="JDBC.Password" value="${password}" /> </dataSource> </transactionManager> <sqlMap resource="com/gbx/po/Person.xml"/> </sqlMapConfig>
PersonDaoImp
package com.gbx.dao; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.gbx.po.Person; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class PersonDaoImp implements IPersonDao{ private static SqlMapClient client = null; static{ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); client = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean addPerson(Person person) { System.out.println("添加前 " + person.getId()); try { client.insert("addPerson", person); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("添加后: " + person.getId()); return true; } public boolean updatePerson(Person person) { try { client.update("updatePerson", person); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("更新成功...."); return true; } public boolean deletePersonById(int id) { try { client.delete("deletePerson", id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("删除成功...."); return true; } public boolean deleteAll(List<Integer> ids) { try { client.delete("deleteAllPerson", ids); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("删除成功...."); return true; } public Person queryById(int id) { Person p = null; try { p = (Person) client.queryForObject("queryPersonById", id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return p; } @SuppressWarnings("unchecked") public List<Person> queryByName(String name) { List<Person> persons = null; try { persons = client.queryForList("queryPersonByName", name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return persons; } @SuppressWarnings({ "unchecked", "deprecation" }) public List<Person> queryAllPerson() { List<Person> persons = null; try { persons = client.queryForList("queryAllPerson"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return persons; } @SuppressWarnings("unchecked") public List<Person> queryPersons(Person person) { List<Person> persons = null; try { persons = client.queryForList("queryPersons", person); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return persons; } @SuppressWarnings("unchecked") public List<Person> queryPage(Map<String, Object> info) { List<Person> persons = null; try { persons = client.queryForList("queryPage", info); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return persons; } public static void main(String[] args) { IPersonDao dao = new PersonDaoImp(); //添加 // Person person = new Person(); // person.setName("小明"); // person.setAge(1); // dao.addPerson(person); //更新 // Person person = new Person(); // person.setId(18); // person.setName("小明123"); // person.setAge(1); // dao.updatePerson(person); //删除 // dao.deletePersonById(18); //批量删除 // List<Integer> ids = new ArrayList<Integer>(); // ids.add(3); ids.add(4); ids.add(5); // dao.deleteAll(ids); // System.out.println(ids); //ID查询 // Person person = new Person(); // person = dao.queryById(19); // System.out.println("查询到的个人信息为:" + person.getId() + " " + person.getAge() + " " + person.getName()); //姓名模糊查询 // List<Person> persons = dao.queryByName("小米"); // for (Person p : persons) { // System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName()); // } //查询所有 // List<Person> persons = dao.queryAllPerson(); // for (Person p : persons) { // System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName()); // } //多条件查询 // Person person = new Person(); // person.setId(0); // person.setName("小"); // person.setAge(20); // List<Person> persons = dao.queryPersons(person); // for (Person p : persons) { // System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName()); // } //利用Map实现模糊的分页查询 Map<String, Object> info = new HashMap<String, Object>(); info.put("begin", 0); info.put("pageSize", 100); info.put("id", 0); info.put("name", "小"); info.put("age", 20); List<Person> persons = dao.queryPage(info); for (Person p : persons) { System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName()); } } }