iBATIS入门实例测试

1. 下载iBATIS相关jar包并导入MyEclipse工程;
2. 创建测试数据库Student,以Oracle为例(此项目使用Oracle自带序列DEPT_SEQ做测试);
id number(10),
name varchar2(20),
address varchar2(20),
3. iBATIS SqlMap 配置(SqlMapConfig.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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL"
value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="JDBC.Username" value="scott" />
<property name="JDBC.Password" value="tiger" />
</dataSource>
</transactionManager>
<sqlMap resource="com/mybatis/data/Student.xml" />
</sqlMapConfig>
4. POJO(Student)
private int id;
private String name;
private String address;
setters/getters省略
5. 映射文件(Student.xml)
<?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 namespace="Student">
<!-- 设置pojo类的别名(shortName) -->
<typeAlias alias="Student" type="com.mybatis.domain.Student" />
<!-- 输出映射,输入为parameterMap -->
<resultMap id="StudentResult" class="Student">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="address" column="address" />
</resultMap>
<!-- 查询所有 -->
<select id="selectAllStudents" resultMap="StudentResult">
select * from Student
</select>
<!-- 根据id查询 -->
<select id="selectStudentById" parameterClass="int" resultClass="Student">
select * from Student where ID = #id#
</select>
<!-- 插入,使用parameterClass -->
<insert id="insertStudent" parameterClass="Student">
<!-- 使用Oracle序列生成主键 -->
insert into Student values (dept_seq.nextval, #name#, #address#)
</insert>
<!-- 更新,使用parameterClass -->
<update id="updateStudent" parameterClass="Student">
update Student set name = #name#,address = #address# where ID = #id#
</update>
<!-- 删除,使用parameterClass -->
<delete id="deleteStudent" parameterClass="int">
delete from Student where ID = #id#
</delete>
</sqlMap>
6. 辅助类(IbatisUtil)
public class IBatisUtil {
private static SqlMapClient sqlMapper;
//静态执行块
static {
try {
Reader reader = Resources.getResourceAsReader("com/mybatis/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("Sorry,something bad happened while building the SqlMapClient instance!"+ e, e);
}
}
//查询所有
public static List selectAllStudents() throws Exception {
return sqlMapper.queryForList("selectAllStudents");
}
//根据id查询
public static Student selectStudentById(int id) throws Exception {
return (Student) sqlMapper.queryForObject("selectStudentById", id);
}
//插入
public static void insertStudent(Student student) throws Exception {
sqlMapper.insert("insertStudent", student);
}
//更新
public static void updateStudent(Student student) throws Exception {
sqlMapper.update("updateStudent", student);
}
//删除
public static void deleteStudent(int id) throws Exception {
sqlMapper.delete("deleteStudent", id);
}
}
7. 测试(Test.java)
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 查询所有
List<Student> studentList = IBatisUtil.selectAllStudents();
System.out.println("listSize=" + studentList.size());
// 根据id查询
try {
Student student = new Student();
int id = 97;
student = IBatisUtil.selectStudentById(id);
if (student != null) {
System.out.println("Result:" + student.getName() + "-"
+ student.getAddress());
} else {
System.out.println("Sorry,there is no result relates your given id!");
}
System.out.println(student.getId() + " " + student.getName() + " "
+ student.getAddress());
} catch (Exception e) {
e.printStackTrace();
}
// 插入
try {
Student student = new Student();
student.setName("zhang san");
student.setAddress("UPC");
IBatisUtil.insertStudent(student);
System.out.println("...insert successfully...");
} catch (Exception e) {
e.printStackTrace();
}
// 更新
try {
Student student = new Student();
student.setId(113);
student.setName("zhang san");
student.setAddress("CPU");
IBatisUtil.updateStudent(student);
System.out.println("...update successfully...");
} catch (Exception e) {
e.printStackTrace();
}
// 删除
try {
int id = 113;
IBatisUtil.deleteStudent(id);
System.out.println("...delete successfully...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值