ibatis--dao的应用
举例说明:
1.来一个Person类
package com.Jdnis.ibatis.model;import java.io.Serializable;import java.sql.Date;public class Person implements Serializable {private static final long serialVersionUID = 1L;private int id ;private String firstName ;private String lastName ;private Date birthDate = new Date(1900,1,1);;private double weight ;private double height ;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Date getBirthDate() {return birthDate;}public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}}
package com.Jdnis.ibatis.interfaces;import java.util.List;import com.ibatis.dao.client.Dao;import com.Jdnis.ibatis.model.Person;public interface PersonInterface extends Dao{ public void insertPerson(Person p); public void updatePerson(Person p); public List selectAllPerson( );}
package com.Jdnis.ibatis.interfaces;import java.sql.SQLException;import java.util.List;import com.ibatis.dao.client.DaoManager;import com.ibatis.dao.client.template.SqlMapDaoTemplate;import com.Jdnis.ibatis.model.Person;public class PersonImpl extends SqlMapDaoTemplate implements PersonInterface {public PersonImpl(DaoManager daoManager) {super(daoManager);// TODO Auto-generated constructor stub}public void insertPerson(Person p) {// TODO Auto-generated method stubtry {this.getSqlMapExecutor().insert("insertPerson",p);System.out.println("数据插入成功!");} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("插入出错,主键冲突!");}}public void updatePerson(Person p) {// TODO Auto-generated method stubtry {this.getSqlMapExecutor().update("updatePerson",p);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public List selectAllPerson() {// TODO Auto-generated method stubreturn queryForList("selectAllPerson",null);}}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap namespace="Person"> <!-- 缓存 --> <typeAlias alias="person" type="com.Jdnis.ibatis.model.Person" /> <parameterMap id="insert_person" jdbcType="int" /> <parameter property="firstName" jdbcType="varchar"/> <parameter property="lastName" jdbcType="varchar"/> <parameter property="birthDate" jdbcType="Date"/> <parameter property="weight" jdbcType="double" /> <parameter property="height" jdbcType="double" /> </parameterMap> <resultMap id="allPersons" column="per_id" /> <result property="firstName" column="per_first_name" /> <result property="lastName" column="per_last_name" /> <result property="birthDate" column="per_birth_date" /> <result property="weight" column="weightInKilograms" /> <result property="height" column="heightInMeters" /> </resultMap> <statement id="insertPerson" parameterMap="insert_person"> INSERT INTO persons VALUES(?,?,?,?,?,?) </statement> <statement id="selectAllPerson" resultMap="allPersons"> <![CDATA[select * from persons]]> </statement> <statement id="updatePerson" parametername="code"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"><sqlMapConfig> <!-- 地方i --> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/person" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="root" /> <property name="Pool.MaximumActiveConnections" value="10" /> <property name="Pool.MaximumIdleConnections" value="5" /> <property name="Pool.MaximumCheckoutTime" value="120000" /> <property name="Pool.TimeToWait" value="500" /> <property name="Pool.PingQuery" value="select 1 from sample" /> <property name="Pool.PingEnabled" value="false" /> <property name="Pool.PingConnectionsOlderThan" value="1" /> <property name="Pool.PingConnectionsNotUsedFor" value="1" /> </dataSource> </transactionManager> <sqlMap resource="com/Jdnis/ibatis/map/person.xml" /></sqlMapConfig>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE daoConfig PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN" "http://ibatis.apache.org/dtd/dao-2.dtd"><daoConfig> <context> <transactionManager type="SQLMAP"> <property name="SqlMapConfigResource" value="com/neusoft/ibatis/map/SqlMapConfig.xml" /> </transactionManager> <dao interface="com.neusoft.ibatis.interfaces.PersonInterface" implementation="com.Jdnis.ibatis.interfaces.PersonImpl" /> </context></daoConfig>
package com.Jdnis.ibatis.dao;import java.io.IOException;import java.io.Reader;import java.util.Properties;import com.ibatis.common.resources.Resources;import com.ibatis.dao.client.DaoManager;import com.ibatis.dao.client.DaoManagerBuilder;public class DAOConfig {private static final String resource="com/Jdnis/ibatis/map/dao.xml";private static final DaoManager daoManager;static{daoManager = newDaoManager(null);}public static DaoManager getDaoManager(){return daoManager;}public static DaoManager newDaoManager(Properties props){try {Reader reader = Resources.getResourceAsReader(resource);return DaoManagerBuilder.buildDaoManager(reader,props);} catch (Exception e) {// TODO Auto-generated catch blocknew RuntimeException("Could not initialize DaoConfig. Cause: " + e, e);}return null;}}
package com.Jdnis.ibatis.dao;import java.sql.Date;import com.ibatis.dao.client.DaoManager;import com.Jdnis.ibatis.interfaces.PersonInterface;import com.Jdnis.ibatis.model.Person;public class TestCase { DaoManager daoManager;protected void setUp()throws Exception{daoManager = DAOConfig.getDaoManager();}public void testInsertPerson(){PersonInterface pi = (PersonInterface)daoManager.getDao(PersonInterface.class);Person p = new Person();p.setId(2);p.setFirstName("firstName");p.setLastName("lastName");//p.setBirthDate(new Date(21,12,25));p.setHeight(172);p.setWeight(125);pi.insertPerson(p);}public static void main(String[] args){TestCase tc = new TestCase();try {tc.setUp();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}tc.testInsertPerson();}}
PersonInterface pi = (PersonInterface)daoManager.getDao(PersonInterface.class);