在网上找到了一个搭建的ibatis教程,按照教程搭建了一下,中间遇到了一些问题,陆陆续续弄了一个小时,终于跑了起来,现在总结一下。
首先是需要下载ibatis所需要的包-----ibatis-2.3.0.677 和mysql-connector-java-5.1.6-bin;
接下来是创建一个java project。
不过我是创建了一个web project。
Student.java是一个简单的JAVABEAN。如下
package com.yunlong;
import java.util.Date;
/*The class of Student
* @author yunlong 2017/01/19
*/
public class Student {
private int id; //学生ID
private String name; //学生姓名
private String birth; //日升
private double score; //分数
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
sqlMap.properties文件里面是包含链接数据库的一些基本信息,如下:
整理完上面这些,我们需要写我们的student.xml 文件了,这里面主要是关于数据库表的一些sql语句操作。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
<typeAlias alias="Student" type="com.yunlong.Student"/>
<select id = "SelectAllStudent" resultClass = "Student">
SELECT * FROM Student
</select>
<!-- parameterClass表示参数的内容 -->
<!-- #表示这是一个外部调用的需要传进来的参数,可以理解为占位符 -->
<select id = "SelectStudentByName" parameterClass="String"
resultClass = "Student">
SELECT name,birth,score from Student where name like '%$name$%'
</select>
<insert id = "addStudent" parameterClass ="Student">
insert into Student(name,birth,score) values(#name#,#birth#,#score#)
<selectKey resultClass = "int" keyProperty="id">
select @@identity as inserted
</selectKey>
</insert>
<delete id ="deleteStudentById" parameterClass = "int">
delete from Student where id = #id#
</delete>
<update id = "updateStudent" parameterClass = "Student">
update Student set name=#name#,birth=#birth#,score=#score# where id=#id#
</update>
</sqlMap>
里面需要说明的就是,sql语句里面的参数是和student类里面的属性对应的,例如,insert into Student(name,birth,score) values(#name#,#birth#,#score#).
其中#name#、#birth#、#score#是student类里面的基本属性对应的。
至于<selectKey resultClass = "int" keyProperty="id">
select @@identity as inserted
</selectKey>
这个标签就是为了返回主键值。
下面把其他代码贴上来。
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 name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="Student.xml" />
</sqlMapConfig>
StudentDao 接口文件:
package com.yunlong;
import java.util.List;
import com.yunlong.Student;
public interface StudentDao {
//添加学生
public boolean addStudent(Student student);
//删除学生
public boolean delStudentById(int id);
//更新学生信息
public boolean updateStudent(Student student);
//查询全部学生信息
public List<Student> selectAllStudent();
//模糊查询学生信息
public List<Student> selectStudentByName(String name);
//根据学生ID查询学生信息
public Student selectStudentById(int id);
}
实现类文件:
package com.yunlong;
import java.io.IOException;
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;
public class StudentDaoIml implements StudentDao {
private static SqlMapClient sqlMapClient = null;
static{
try{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("读取配置文件出现错误!");
}
}
@Override
public boolean addStudent(Student student) {
// TODO Auto-generated method stub
Object object = null;
boolean flag = false;
try{
object = sqlMapClient.insert("addStudent",student);
System.out.println("添加学生信息的返回值:"+object);
}catch(SQLException e){
e.printStackTrace();
}
if(object != null ){
flag = true;
}
return flag;
}
@Override
public boolean delStudentById(int id) {
boolean flag = false;
Object object = null;
try{
object = sqlMapClient.delete("deleteStudentById",id);
System.out.println("删除学生信息的返回值:"+object);
}catch(SQLException e){
e.printStackTrace();
}
if(object !=null){
flag = true;
}
return flag;
}
@Override
public boolean updateStudent(Student student) {
boolean flag = false;
Object object = null;
try{
object = sqlMapClient.update("updateStudent",student);
System.out.println("更新学生信息的返回值:"+object+",返回影响的行数");
}catch(SQLException e){
e.printStackTrace();
}
if(object != null){
flag = true;
}
return flag;
}
@Override
public List<Student> selectAllStudent() {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Student> selectStudentByName(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Student selectStudentById(int id) {
// TODO Auto-generated method stub
return null;
}
}
备注:第一次调试的时候,一直提示sqlmap根元素需要sqlmapconfig ,一直搞了很久,最后是因为xml文件的头文件上面不对,我按照错误提示改了就可以了,搞不懂里面的原理,郁闷。
-----------------------------------------------ibatis的第一次爬坑总结结束---------------------------------------------