iBatis简单入门教程

iBatis 简介:

iBatis apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。

官网为:http://www.mybatis.org/

 

搭建iBatis 开发环境:

1 、导入相关的jar 包,ibatis-2.3.0.677.jar mysql-connector-java-5.1.6-bin.jar

2 、编写配置文件:

Jdbc 连接的属性文件

总配置文件, SqlMapConfig.xml

关于每个实体的映射文件(Map 文件)

 

Demo 

Student.java:

Java代码  
  1. package com.iflytek.entity;
  2. import java.sql.Date;
  3. public class Student {
  4. // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
  5. private int id;
  6. private String name;
  7. private Date birth;
  8. private float score;
  9. public int getId() {
  10. return id;
  11. }
  12. public void setId(int id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public Date getBirth() {
  22. return birth;
  23. }
  24. public void setBirth(Date birth) {
  25. this.birth = birth;
  26. }
  27. public float getScore() {
  28. return score;
  29. }
  30. public void setScore(float score) {
  31. this.score = score;
  32. }
  33. @Override
  34. public String toString() {
  35. return "id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
  36. + score + "\n";
  37. }
  38. }

 

SqlMap.properties 

Properties代码  
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/ibatis
  3. username=root
  4. password=123

 

Student.xml 

Xml代码  

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-03-24 13:08  JamesFan  阅读(114)  评论(0编辑  收藏  举报