各路英雄好汉,江湖救急:刚接触iBatis(工程导入了mybatis-2.3.5.jar,mySQL 驱动包)
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>
<properties resource="SqlMap.properties"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property value="${driver}" name="JDBC.Driver"/>
<property value="${url}" name="JDBC.ConnectionURL"/>
<property value="${username}" name="JDBC.Username"/>
<property value="${password}" name="JDBC.Password"/>
</dataSource>
</transactionManager>
<SqlMap resource="Student.xml"/>
</SqlMapConfig>
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>
<typeAlias alias="Student" type="myIbatis.Student"/>
<resultMap id="StudentResult" class="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="major" column="major"/>
<result property="birthday" column="birthday"/>
</resultMap>
<select id="selectAllStudent" resultClass="Student">
select * from student_table
</select>
</sqlMap>
SqlMap.properties 文件内容如下 :
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/myIbatisDB"
username="root"
password="fhlu18"
写了一个方法 studentDaoImpl.jva
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import myIbatis.Student;
import com.ibatis.sqlmap.client.SqlMapClient;
public class StudentDaoImpl implements StudentDao {
private static SqlMapClient sqlMapClient = null;
static {
try {
Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("myIbatis/SqlMapConfig.xml");
sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Student> queryAllStudent() {
List<Student> studentList = null;
try {
studentList = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return studentList ;