核心配置文件:
<?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>
<!-- ibatis 数据源配置信息 <settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="true"/> -->
<!-- ibaits 配置不同数据库的数据接入方式 利用 JDBC -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/student" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="admin" />
<!-- <property name="Pool.MaximumActiveConnections" value="10" /> <property
name="Pool.MaximumIdleConnections" value="5" /> <property name="Pool.MaximumCheckoutTime"
value="120000" /> <property name="Pool.TimeToWait" value="500" /> <property
name="Pool.PingEnabled" value="false" /> <property name="Pool.PingConnectionsOlderThan"
value="1" /> <property name="Pool.PingConnectionsNotUsedFor" value="1" /> -->
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/student.xml" />
</sqlMapConfig>
映射配置文件:
<?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="student" type="com.ibatis.Student"/>
<select id="selectAllStudent" resultClass="Student">
select * from student
</select>
</sqlMap>
model类:
package com.ibatis;
import java.util.Date;
public class Student {
private int sid=0;
private String sname=null ;
private String major=null;
private Date birth=null;
private float score=0;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
接口:
package com.ibatis;
import java.util.List;
public interface IStudentDao {
public void addStudent(Student student);
public void deleteStudent(int id);
public void updateStudent(Student student);
public List<Student> queryAllStudent();
public List<Student> querryStudentByName(String name);
//只查一个属性
public Student querryStudentById(int id);
}
继承接口类:
package com.ibatis;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
public class IStudentDAOImpl implements IStudentDao {
//得去配置文件
private static SqlMapClient sqlMapClient=null;
static {
try {
Reader reader=com.ibatis.common.resources.Resources.getResourceAsReader("com/ibatis/sqlMapConfig.xml");
sqlMapClient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void addStudent(Student student) {
// TODO Auto-generated method stub
}
@Override
public void deleteStudent(int id) {
// TODO Auto-generated method stub
}
@Override
public void updateStudent(Student student) {
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
@Override
public List<Student> queryAllStudent() {
List<Student> studentList=null;
try {
studentList=sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentList;
}
@Override
public List<Student> querryStudentByName(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Student querryStudentById(int id) {
return null;
}
}
主函数:
package com.ibatis;
public class Test {
public static void main(String[] args) {
IStudentDao dao=new IStudentDAOImpl();
for(Student student:dao.queryAllStudent()){
System.out.println("this is id:"+student);
}
}
}