和学习所有框架API一样,有个HelloWorld的例子
本着1+1=2学习的逻辑,以下继续对iBatis3做一些简单实现
熟悉Hibernate的都知道如下几步
/**
* 1. create Configuration
* 2. build SessionFactory
* 3. open Session
* 4. begin Transaction
* 5. do crud operation
* flush commit rollback
* 6. close resource
*/
而iBatis3会有如下几步
/**
* 1. create SqlSessionFactoryBuilder
* 2. read config file
* 3. build Factory
* 4. open session
* 5. do crud operations
* 6. close session
*/
第一个实现Hello World,
先看下文件结构
既然是iBatis,首先创建脚本,用的是mysql
DROP TABLE IF EXISTS `ibatis`.`t_helloworld`;
CREATE TABLE `ibatis`.`t_helloworld` (
`hw_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` int(2) unsigned NOT NULL,
PRIMARY KEY (`hw_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
数据如下
同时关注几个文件
- ibatis-config.xml
- config.properties
- helloworld.map.xml
- ibatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<!-- 数据库配置文件 -->
<properties resource="basic/config.properties"/>
<!-- default属性一定要指定为其中某一environment的id -->
<environments default="helloworld">
<environment id="helloworld">
<!-- type暂时用最简单的JDBC,其他可选项MANAGED -->
<transactionManager type="JDBC" />
<!-- type暂时用最简单的POOLED,其他可选项UNPOOLED,JNDI -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ibatis"/>
<property name="username" value="root"/>
<property name="password" value="abcdef"/ -->
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="basic/helloworld.map.xml"/>
</mappers>
</configuration>
2. config.properties
driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/ibatis username=root password=abcdef
3. helloworld.map.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="basic.HelloWorld">
<select id="getHelloworlds" resultType="java.util.List">
select * from t_helloworld
</select>
<select id="getHelloworldsByHwId" resultType="java.util.List" parameterType="int">
select * from t_helloworld where hw_id = #{hw_id}
</select>
</mapper>
FirstTest.java
package basic;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class FirstTest {
public static void main(String[] args) throws IOException {
String configXml = "basic/ibatis-config.xml";
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
Reader config = Resources.getResourceAsReader(configXml);
SqlSessionFactory factory = builder.build(config);
SqlSession session = factory.openSession();
List<HelloWorld> list = null;
try {
list = session.selectList("getHelloworlds");
System.out.println(list.size());
list = session.selectList("getHelloworldsByHwId", 1);
System.out.println(list.size());
} finally {
session.close();
}
}
}