首页 > 代码库 > ibatis_001 入门
ibatis_001 入门
ibatis学习记录--001
本实例是JavaWeb项目,使用的是Mysql数据库;所需jar包:ibatis-2.3.0.677.jar;mysql-connector-java-5.1.7-bin.jar;
1.基本Java环境搭好后,Eclipse创建JavaWeb项目;导入上述两jar包;
2.创建实体类:com.lxt.entity.I_User;
package com.lxt.ibatis.entity;
public class I_User {
private Integer i_u_id;
private String i_u_name;
private String i_u_pwd;
public I_User() {
super();
}
public I_User(String i_u_name, String i_u_pwd) {
super();
this.i_u_name = i_u_name;
this.i_u_pwd = i_u_pwd;
}
public I_User(Integer i_u_id, String i_u_name, String i_u_pwd) {
super();
this.i_u_id = i_u_id;
this.i_u_name = i_u_name;
this.i_u_pwd = i_u_pwd;
}
public Integer getI_u_id() {
return i_u_id;
}
public void setI_u_id(Integer i_u_id) {
this.i_u_id = i_u_id;
}
public String getI_u_name() {
return i_u_name;
}
public void setI_u_name(String i_u_name) {
this.i_u_name = i_u_name;
}
public String getI_u_pwd() {
return i_u_pwd;
}
public void setI_u_pwd(String i_u_pwd) {
this.i_u_pwd = i_u_pwd;
}
@Override
public String toString() {
return "I_User [i_u_id=" + i_u_id + ", i_u_name=" + i_u_name
+ ", i_u_pwd=" + i_u_pwd + "]";
}
}
3.配置实体类文件i_user.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>
<typeAlias alias="I_User" type="com.lxt.ibatis.entity.I_User" />
<!-- 这样以后改了sql,就不需要去改java代码了 -->
<!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
<select id="selectAllStudent" resultClass="I_User">
select * from
i_user
</select>
</sqlMap>
4.编写配置文件SqlMap.properties,sqlMapConfig.xml
SqlMap.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis
username=root
password=root
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>
<!-- 引用JDBC属性的配置文件 -->
<properties resource="com/lxt/ibatis/config/SqlMap.properties" />
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC">
<!-- 数据源 -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="http://www.mamicode.com/${driver}" />
<property name="JDBC.ConnectionURL" value="http://www.mamicode.com/${url}" />
<property name="JDBC.Username" value="http://www.mamicode.com/${username}" />
<property name="JDBC.Password" value="http://www.mamicode.com/${password}" />
</dataSource>
</transactionManager>
<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="com/lxt/ibatis/entity/i_user.xml" />
</sqlMapConfig>
4.数据库里创建ibatis数据库和i_user表
5.编写Dao并测试
package com.lxt.ibatis.dao;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.lxt.ibatis.entity.I_User;
public class I_UserDao {
private static SqlMapClient sqlMapClient = null;
static {
try {
Reader reader = Resources.getResourceAsReader("com/lxt/ibatis/config/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public List<I_User> getI_Userlist (){
List<I_User> userlist = null;
try {
userlist = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userlist;
}
public static void main(String args[]){
I_UserDao iud = new I_UserDao();
List<I_User> userlist = iud.getI_Userlist();
for (I_User i_User : userlist) {
System.out.println(i_User.toString());
}
}
}