iBatis学习

1.iBatis是apache的一个开源项目,一个O/R Mapping解决方案。

2.搭建环境

1)导入相关jar包,jdbc和ibatis

2)3类配置文件,数据源properties文件、sqlmapconfig文件、sqlmap配置文件

3.代码

 SqlMap.properties(配置数据源)

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@172.21.0.77:1524:acdb
username=bnms
password=bnms

SqlMapConfig.xml(iBatis的sqlMapConfig配置文件,事务管理)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
        "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
        
<sqlMapConfig>
    <properties resource="com/itcast/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="com/itcast/Student.xml"/>
</sqlMapConfig>
        

Student.xml(sql文件)

<?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.itcast.Student"/>
    
    <select id="selectAllStudent" resultClass="Student">
        select * from student
    </select>
    <select id="selectStudentById" parameterClass="int" resultClass="Student">
        select * from student where sid=#sid#
    </select>
    <insert id="insertStudent" parameterClass="Student" >
        insert into student (sid ,sname ,major, birth,score)
        values (#sid#,#sname#,#major#,#birth#,#score#)
    </insert>
    <delete id="deleteStudentById" parameterClass="int">
        delete from student where sid=#sid#
    </delete>
    <update id="updateStudent" parameterClass="Student">
         update  student set major=#major#,sname=#sname#,birth=#birth#,score=#score# 
         where sid=#sid#
    </update>
    <select id="selectStudentByName" parameterClass="String" resultClass="Student">
        select * from student where sname like '%$sname$%'
    </select>
    <insert id="insertStudentBySecquence"  parameterClass="Student" >
    <selectKey resultClass="int" keyProperty="sid">
        select studentPKSewuence.nextVal from dual
    </selectKey>
        insert into student (sid ,sname ,major, birth,score)
        values (#sid#,#sname#,#major#,#birth#,#score#)
    </insert>
</sqlMap>

Student.java(实体类)

package com.itcast;

import java.sql.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;
    }
  //重写toString方法
    public String toString() {
        String content="sid:"+sid+"\tsname:"+sname+"\tmajor:"+major+"\tbirth:"+birth+"\tscore:"+score;
        return content;
    }
    
}

IStudentDao.java

package com.itcast;

import java.util.List;

public interface IStudentDao {
  //新增
public void addStudent(Student student);
  //新增,sequence自增长
public void addStudentBysequence(Student student);
  //根据id删除
public void deleteStudentById(int id);
  //根据id修改
public void updateStudentById(Student student);
  //查询所有记录
public List<Student> queryAllStudents();
  //模糊查询
public List<Student> queryAllStudentsByName(String name);
  //根据id查询
public Student queryStudentById(int id); }

StudentDao.java

package com.itcast;

import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

public class StudentDao implements IStudentDao{
    
    private  static  SqlMapClient  sqlMapClient = null;
    
    static{
        try {
            Reader reader=com.ibatis.common.resources.Resources.getResourceAsReader("com/itcast/SqlMapConfig.xml");
            sqlMapClient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void addStudent(Student student) {
        try {
            sqlMapClient.insert("insertStudent", student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addStudentBysequence(Student student) {
        try {
            sqlMapClient.insert("insertStudentBySecquence", student);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    public void deleteStudentById(int id) {
        try {
            sqlMapClient.delete("deleteStudentById", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }

    public List<Student> queryAllStudents() {
        List<Student> studentList=null;
        try {
            studentList=sqlMapClient.queryForList("selectAllStudent");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return studentList;
    }

    public List<Student> queryAllStudentsByName(String name) {
         List<Student> students =null;
        try {
            students=sqlMapClient.queryForList("selectStudentByName", name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }

    public Student queryStudentById(int id) {
        Student student=null;
        try {
            student= (Student) sqlMapClient.queryForObject("selectStudentById",id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return student;
    }

    public void updateStudentById(Student student) {
        try {
            sqlMapClient.update("updateStudent", student);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
  //测试
public static void main(String[] args) { IStudentDao studentDao=new StudentDao(); //queryAllStudents // for (Student student:studentDao.queryAllStudents()){ // System.out.println(student); // } //queryStudentById // System.out.println(studentDao.queryStudentById(1)); //addStudent // Student student=new Student(); // student.setSid(13); // student.setSname("fang32"); // student.setMajor("football"); // student.setBirth(Date.valueOf("2010-04-02")); // student.setScore((float) 1.2343); // studentDao.addStudent(student); // // studentDao.deleteStudentById(2); Student student=new Student(); student.setSid(101); student.setSname("tianzhen123"); student.setMajor("football"); student.setBirth(Date.valueOf("2010-04-02")); student.setScore((float) 1.2343); studentDao.updateStudentById(student); // for (Student student:studentDao.queryAllStudentsByName("fang")){ // System.out.println(student); // } //insertStudentBySecquence // Student student=new Student(); // //student.setSid(13); // student.setSname("fang32"); // student.setMajor("football"); // student.setBirth(Date.valueOf("2010-04-02")); // student.setScore((float) 1.2343); // studentDao.addStudentBysequence(student); } }

项目结构:

4.总结:

优点:1)减少代码量2)简单3)架构级性能增强4)sql语句与程序代码分离5)简化项目分工6)增强移植性

缺点:1)sql需要自己写2)参数只能传一个

 

 

posted @ 2015-09-14 17:09  柱柱  阅读(210)  评论(1编辑  收藏  举报