iBatis初级入门
一个简单的iBatis应用所需要的文件
1:POJO类 例:Person.java
public class Person { private int id; private String firstName; private String lastName; private Date birthDate; private double weightInKilograms; private double heightInMeters; // getter,setter省略 }
2:iBatis总的配置文件 例:SqlMapConfig.xml
<?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> <!-- 加载JDBC配置文件 --> <properties resource="examples/sqlmap/maps/SqlMapConfigExample.properties" /> <!-- 设置连接属性 --> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <!-- 别名 --> <typeAlias alias="order" type="testdomain.Order" /> <!-- transaction&dataSource配置 --> <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> <!-- 引入POJO类的配置文件 --> <sqlMap resource="examples/sqlmap/maps/Person.xml" /> </sqlMapConfig>
3:JDBC配置文件 SqlMapConfigExample.properties
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:oracle1 username=jsmith password=test
4:对应POJO类的配置文件 例:Person.xml
<?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"> <!-- 查询 --> <select id="getPerson" parameterClass="int" resultClass="examples.domain.Person"> SELECT PER_ID as id, PER_FIRST_NAME as firstName, PER_LAST_NAME as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as weightInKilograms, PER_HEIGHT_M as heightInMeters FROM PERSON WHERE PER_ID = #value# </select> <!-- 新增 --> <insert id="insertPerson" parameterClass="examples.domain.Person"> INSERT INTO PERSON (PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M) VALUES (#id#, #firstName#, #lastName#, #birthDate#,#weightInKilograms#, #heightInMeters#) </insert> <!-- 更新 --> <update id="updatePerson" parameterClass="examples.domain.Person"> UPDATE PERSON SET PER_FIRST_NAME = #firstName#, PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#, PER_WEIGHT_KG = #weightInKilograms#, PER_HEIGHT_M = #heightInMeters# WHERE PER_ID = #id# </update> <!-- 删除 --> <delete id="deletePerson" parameterClass="examples.domain.Person"> DELETE PERSON WHERE PER_ID = #id# </delete> </sqlMap>
5:对应表 Person.sql
CREATE TABLE PERSON( PER_ID NUMBER (5, 0) NOT NULL, PER_FIRST_NAME VARCHAR (40) NOT NULL, PER_LAST_NAME VARCHAR (40) NOT NULL, PER_BIRTH_DATE DATETIME , PER_WEIGHT_KG NUMBER (4, 2) NOT NULL, PER_HEIGHT_M NUMBER (4, 2) NOT NULL, PRIMARY KEY (PER_ID) )
6:封装
public class MyAppSqlConfig { private static final SqlMapClient sqlMapClient ; static { try { String resource = "com/ibatis/example/sql-map-config.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (Exception e) { e.printStackTrace(); } } public static SqlMapClient getSqlMapInstance() { return sqlMapClient; } }
7:应用
public class Person { public static void main(String[] args) { //---------查询-------------- SqlMapClient sqlMapClient = MyAppSqlMapConfig.getSqlMapInstance(); Integer personPk = new Integer(5); Person person = (Person) sqlMapClient.queryForObject ("getPerson", personPk); //---------更新------------- person.setHeightInMeters(1.83); person.setWeightInKilograms(86.36); sqlMapClient.update("updatePerson", person); //---------删除---------- sqlMapClient.delete("deletePerson", person); //---------插入---------- Person newPerson = new Person(); newPerson.setId(11); newPerson.setFirstName("Clinton"); newPerson.setLastName("Begin"); newPerson.setBirthDate (null); newPerson.setHeightInMeters(1.83); newPerson.setWeightInKilograms(86.36); sqlMapClient.insert ("insertPerson", newPerson); } }
相关推荐
Dullonjiang 2020-01-23
与卿画眉共浮生 2011-06-25
yierxiansheng 2014-06-18
登峰小蚁 2019-12-24
plane 2017-10-19
踩风火轮的乌龟 2019-10-23
pandapanda 2014-05-08
阿泰 2014-07-02
shouen 2016-04-26
whileinsist 2012-04-07
spprogrammer 2018-01-25
Coohx 2017-12-05
AndroidOliver 2012-05-14
BruceWayne 2014-12-02
liubang000 2014-05-30
KOJ 2013-11-26
CXC0 2013-08-28