日期:2014-05-17 浏览次数:20686 次
public class Person implements Serializable { private static final long serialVersionUID = 9006203036989086304L; private Integer id; private String pName; private String sex; private Integer age;//省略setter getter方法}
<sqlMap namespace="Person"> <typeAlias alias="person" type="com.bean.Person"/> <select id="getPerson" resultClass="person" parameterClass="java.lang.String"> <![CDATA[ select * from tab_person where id=#value# ]]> </select> <insert id="insertPerson" parameterClass="person"> <![CDATA[ insert into tab_person(id,pname,sex,age) values(#id#,#pname#,#sex#,#age#) ]]> </insert> <update id="updatePerson" parameterClass="person"> <![CDATA[ update tab_person set pname=#pname#,sex=#sex#,age=#age# where id=#id# ]]> </update> <delete id="deletePerson" parameterClass="java.lang.String"> <![CDATA[ delete tab_person where id=#id# ]]> </delete> </sqlMap>
<sqlMapConfig> <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="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="JDBC.Username" value="system"/> <property name="JDBC.Password" value="oraclepass"/> </dataSource> </transactionManager> <sqlMap resource="com/bean/Person.xml"/> </sqlMapConfig>
public static void main(String[] args){ try { String resource="SqlMapConfigExample.xml"; Reader reader=Resources.getResourceAsReader(resource); XmlSqlMapClientBuilder xmlSqlMapClientBuilder=new XmlSqlMapClientBuilder(); SqlMapClient sqlMapClient=xmlSqlMapClientBuilder.buildSqlMap(reader); sqlMapClient.startTransaction(); Person person=new Person("gahd", "man", 21); sqlMapClient.insert("insertPerson",person); sqlMapClient.commitTransaction(); } catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } }