搭建iBatis开发环境:
1、导入相关的jar包,ibatis-2.3.0.677.jar、mysql-connector-java-5.1.6-bin.jar
2、编写配置文件:
Jdbc连接的属性文件SqlMap.properties
总配置文件,SqlMapConfig.xml
关于每个实体的映射文件(Map文件)
Demo:
Student.java:
package com.iflytek.entity;import java.sql.Date;/** * @author xudongwang 2011-12-31 * *Email:xdwangiflytek@gmail.com * Student { id;private String name;private Date birth;private float score;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public float getScore() {return score;}public void setScore(float score) {this.score = score;}@Overridepublic String toString() {return “id=” + id + “\tname=” + name + “\tmajor=” + birth + “\tscore=”+ score + “\n”;}}
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ibatisusername=rootpassword=123
<?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><!– 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 –><typeAlias alias=”Student” type=”com.iflytek.entity.Student” /><!– 这样以后改了sql,就不需要去改java代码了 –><!– id表示select里的sql语句,美国空间,resultClass表示返回结果的类型 –><select resultClass=”Student”>select * fromtbl_student</select><!– parameterClass表示参数的内容 –><!– #表示这是一个外部调用的需要传进的参数,服务器空间,可以理解为占位符 –><select parameterClass=”int” resultClass=”Student”>select * from tbl_student where id=#id#</select><!– 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject –><select parameterClass=”String”resultClass=”Student”>select name,birth,score from tbl_student where name like’%$name$%'</select><insert parameterClass=”Student”>insert intotbl_student(name,birth,score) values(#name#,#birth#,#score#);<selectKey resultClass=”int” keyProperty=”id”>select @@identity as inserted<!– 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: –><!– mysql:SELECT LAST_INSERT_ID() AS VALUE –><!– mssql:select @@IDENTITY as value –><!– oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL –><!– 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 –></selectKey></insert><delete parameterClass=”int”><!– #id#里的id可以随意取,但是上面的insert则会有影响,香港虚拟主机,因为上面的name会从Student里的属性里去查找 –><!– 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 –>delete from tbl_student where id=#id#</delete><update parameterClass=”Student”>update tbl_student setname=#name#,birth=#birth#,score=#score# where id=#id#</update></sqlMap>
说明:
如果xml中没有ibatis的提示,则window –> Preference–> XML–>XML Catalog—>点击add
选择uriURI:请选择本地文件系统上
iBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd文件;
Key Type:选择Schema Location;
Key:需要联网的,不建议使用;
<?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><!– 引用JDBC属性的配置文件 –><properties resource=”com/iflytek/entity/SqlMap.properties” /><!– 使用JDBC的事务管理 –><transactionManager type=”JDBC”><!– 数据源 –><dataSource type=”SIMPLE”><property value=”${driver}” /><property value=”${url}” /><property value=”${username}” /><property value=”${password}” /></dataSource></transactionManager><!– 这里可以写多个实体的映射文件 –><sqlMap resource=”com/iflytek/entity/Student.xml” /></sqlMapConfig>
我要准备好行李启程了,谢谢关心我的家人和朋友,