昨天网络上看了一些关于IBATIS的内容,今天开始进入具体的学习期.
1. 下载IBATIS 2.3.4的软件包(http://ibatis.apache.org)。
2. 在Mysql里建 数据库:ibatis_db和表t_user
Int id , varchar name, int sex.
操作如下:
create database ibatis_db;
show databases;
create table ibatis_db.t_user(id int(6) not null,
name varchar(15) not null, sex int(1));
3. 在eclipse里建工程ibatisItem
(1) 导入lib包
ibatis-2.3.4.726.jar
mysql-connector-java-5.0.8-bin.jar
(2) sqlMap的配置文件
(3) sqlMapConfig.properties
2 #of the SQL Maps configuration file (e.g. by Ant builders or continuous
3 #Integration tools for different environments
4 #These values can be used in any property value in the file above(e.g "${driver}")
5 #Using a properties file such as this is completely optional.
6 driver=com.mysql.jdbc.Driver
7 url=jdbc:mysql://localhost:3306/ibatis_db?userUnicode=true&characterEncoding=UTF-8
8 username=root
9 password=root
10
(4) 建一个对应数据库表的User Object:
(5) Sql Map 的映射文件
(6) 设计一个类MyAppSqlconfig.java
(7) 设计Dao
最后写一个junit测试类
顺利完成!
总结一下,在今天的学习过程中.容易出错的在Sql Map 的映射文件.
1) 在parameterMap 和resultMap中,字段数据类型是java.sqlTypes类定义的常量名称.BLOB,CHAR,CLOB,DATE,LONGVARBINARY,INTEGER,NULL,NUMBRIC,TIME
TIMESTAMP和VARCHAR等.
2)对于数据表中的NULLBALE的字段,必须在parameterMap 和resultMap中指定字段的数据类型.