jar包:
下载ibatis-2.3.0.677.jar
mysql驱动包
配置文件:
jdbc配置文件:db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
数据库表:
DROP TABLE IF EXISTS `test`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
主配置:SqlMapConfig.xml (像hibernate中的hibernate.cfg.xml)
<?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> <properties resource="db.properties"/> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property value="${driver}" name="JDBC.Driver"/> <property value="${url}" name="JDBC.ConnectionURL"/> <property value="${username}" name="JDBC.Username"/> <property value="${password}" name="JDBC.Password"/> </dataSource> </transactionManager> <!--如果有包的话用'/'隔开--> <sqlMap resource="Test.xml"/> </sqlMapConfig>
类配置:Test.xml(像hibernate中的类名.hbm.xml不过那是对象映射,而这里配置的是所使用的sql)
<?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> <!--alias用于起别名 如果type中有包名的话 alias可简化代码--> <typeAlias alias="Test" type="Test"/> <!-- id是个标识,不能重复,用于读取 就像spring里配置文件中bean中的id resultClass是返回对象的类型 如果是list返回list中元素的类型--> <select id="selectAll" resultClass="Test"> select * from test </select> <!-- parameterClass是参数的类型 参数用#param#表示--> <select parameterClass="int" id="selectTestById" resultClass="Test"> select * from test where id=#id# </select> <!-- parameterClass不区分大小写 --> <insert id="add" parameterClass="test"> insert into test(id,name) values(#id#,#name#) </insert> <!-- set多 用,隔开 --> <update id="update" parameterClass="Test"> update test set name=#name# where id=#id# </update> <!-- 模糊查询必须加单引号或者双引号并且参数用$param$表示,%放到引号内参数外--> <select id="fuzzyRetrieve" parameterClass="String" resultClass="Test"> select * from test where name like '%$name$%' </select> <!-- 也可以在外部传参用%param%,配置用'$param$'表示 <select id="fuzzyRetrieve" parameterClass="String" resultClass="Test"> select * from test where name like '$name$' </select> --> <delete id="delete" parameterClass="int"> delete from test where id=#id# </delete> </sqlMap>
javabean:
public class Test {
private int id = 0;
private String name = null;
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;
}
}
接口:
import java.util.List;
public interface TestDao {
public void add(Test test) throws Exception;
public void delete(int id) throws Exception;
public void update(Test test) throws Exception;
public List<Test>query() throws Exception;
public List<Test> query(String name) throws Exception;
public Test query(int id) throws Exception;
public void addBySequence(Test test) throws Exception;
}
接口实现:
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class ITestDaoImpl implements TestDao {
private static SqlMapClient sqlMapClient = null;
static{
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(Test test) throws Exception {
sqlMapClient.insert("add",test);
}
public void delete(int id) throws Exception {
sqlMapClient.delete("delete",id);
}
@SuppressWarnings("unchecked")
public List<Test> query() throws Exception {
List<Test>list = sqlMapClient.queryForList("selectAll");
return list;
}
@SuppressWarnings("unchecked")
public List<Test> query(String name) throws Exception {
List<Test>list = sqlMapClient.queryForList("fuzzyRetrieve",name);
return list;
}
public Test query(int id) throws Exception {
Test test = null;
test = (Test)sqlMapClient.queryForObject("selectTestById",id);
return test;
}
public void update(Test test) throws Exception {
sqlMapClient.update("update", test);
}
以上即配置完成,直接使用mian函数测试,也可使用junit测试
ibatis缺点
sql需要自己写
参数数量只能一个
ibatis优点
减少代码
简单
sql语句与程序代码分离
简化项目中的分工
增强了移植性