导包-->配sqlMapConfig.xml-->配sql-->调用API
1.引入jar包ibatis-2.3.jar
2.配置sqlMapConfig.xml,放在src下config文件夹中即可。
sqlMapConfig.xml如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <!--使用properties属性文件可以存储相应的属性值--> <properties resource="config/SqlMapConfigExample.properties " /> <!-- setting用于配置和优化sqlMapClient的各选项。 maxRequest是同时执行sql的最大线程数 。maxSession是同一时间最大的会话数。 减小数值类型的属性可以提高性能,降低消耗 --> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true" /> <!-- 配置事务管理服务 --> <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> <!--引入存储sql语句的配置文件--> <sqlMap resource="config/sql/student.xml" /> </sqlMapConfig>
<?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="student"> <resultMap class="earl.vo.Student" id="student"> <result property="id" column="ID"/> <result property="name" column="NAME"/> <result property="age" column="AGE"/> <result property="sex" column="SEX"/> <result property="major" column="MAJOR"/> </resultMap> <select id="queryAllStudent" resultMap="student"> SELECT * FROM STUDENT </select> <select id="queryStudentById" parameterClass="int" resultMap="student"> SELECT * FROM STUDENT WHERE ID=#id# </select> <select id="queryStudentById1" parameterClass="int" resultClass="earl.vo.Student"> SELECT * FROM STUDENT WHERE ID=#id# </select> <insert id="addStudent" parameterClass="earl.vo.Student"> INSERT INTO STUDENT(ID,NAME,AGE,SEX,MAJOR) VALUES(#id#,#name#,#age#,#sex#,#major#) </insert> <delete id="deleteStudent" parameterClass="int"> DELETE FROM STUDENT WHERE ID=#id# </delete> <update id="updateStudent" parameterClass="earl.vo.Student"> UPDATE STUDENT SET ID=#id#,NAME=#name#,AGE=#age#,SEX=#sex#,MAJOR=#major# </update> <!-- 以下是动态拼接sql --> <select id="queryStudentById2" parameterClass="java.util.HashMap" resultMap="student"> SELECT * FROM STUDENT <dynamic prepend="where"> <isNotEmpty prepend="" property="id">ID=#id#</isNotEmpty> </dynamic> </select> <!-- 大写的为数据库字段名,小写的为传入参数map的key值,上下的property为对应关系 --> <insert id="addStudent1" parameterClass="earl.vo.Student"> INSERT INTO STUDENT( <dynamic prepend=" "> <isNotEmpty prepend="," property="id">ID</isNotEmpty> <isNotEmpty prepend="," property="name">NAME</isNotEmpty> <isNotEmpty prepend="," property="age">AGE</isNotEmpty> <isNotEmpty prepend="," property="sex">SEX</isNotEmpty> <isNotEmpty prepend="," property="major">MAJOR</isNotEmpty> </dynamic> )VALUES( <dynamic prepend=" "> <isNotEmpty prepend="," property="id">#id#</isNotEmpty> <isNotEmpty prepend="," property="name">#name#</isNotEmpty> <isNotEmpty prepend="," property="age">#age#</isNotEmpty> <isNotEmpty prepend="," property="sex">#sex#</isNotEmpty> <isNotEmpty prepend="," property="major">#major#</isNotEmpty> </dynamic> ) </insert> <select id="selectUseIterator" parameterClass="java.util.List" resultMap="student"> SELECT * FROM STUDENT <dynamic prepend="WHERE ID IN"> <iterate open="(" close=")" conjunction="," >#[]#</iterate> </dynamic> </select> </sqlMap>
static{ try { Reader reader=Resources.getResourceAsReader("config/sqlMapConfig.xml"); sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } }