ibatis入门及环境搭建

使用ibatis的理由:

 

1. 知道怎样操作10种以上的数据库

2. 可配置的caching(包括从属)
3. 支持DataSource、local transaction managemen和global transaction
4. 简单的XML配置文档
5. 支持Map, Collection, List和简单类型包装(如Integer, String)
6. 支持JavaBeans类(get/set 方法)
7. 支持复杂的对象映射(如populating lists, complex object models)
8. 对象模型从不完美(不需要修改)
9. 数据模型从不完美(不需要修改)
10. 你已经知道SQL,为什么还要学习其他东西

11. ibatis把sql语句从Java源程序中独立出来,放在单独的XML文件中编写,给程序的维护带来了很大便
利。
12. ibatis封装了底层JDBC API的调用细节,并能自动将结果集转换成Java Bean对象,大大简化了Java数
据库编程的重复工作。
13. 简单易于学习,易于使用, 非常实用。
14. 因为Ibatis需要程序员自己去编写sql语句,程序员可以结合数据库自身的特点灵活控制sql语句,因
此能够实现比hibernate等全自动orm框架更高的查询效率,能够完成复杂查询。

15. 阿里巴巴、慧点科技等多家知名软件公司都使用Ibatis。(这才是重点啊,不学何以学sofa啊)

 

环境搭建流程:(java语言+oralce)

1、下载jar包

2、eclipse中新建工程,将相关jar包导入ibatis-2.3.0.677.jar  ojdbc14.jar

3、配置文件(三种):

     jdbc连接的属性文件

     总配置文件

     关于每个实体的映射文件

 

SqlMap.properties(oralce版)

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:wxy
username=wxy
password=123456

SqlMap.properties(mysql版)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/wxy
username=root
password=123456

 

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
   <!--添加JDBC配置文件-->
   <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="com/wxy/Student.xml"/>
</sqlMapConfig>

实体类的映射文件Student.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.rog//DTD SQL Map 2.0//EN" "http://ibatis.apche.org/dtd/sql-map-2.dtd">
<sqlMap>
<!--
   <resultMap id="StudentResult" class="Student">
      <result property="sid" column="sid"/>
      <result property="sname" column="sname"/>
      <result property="major" column="major"/>
      <result property=birth" column="birth"/>
   </resultMap>
-->
</sqlMap>



 

创建对应的Student.java

package com.wxy;

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 Student(){}

	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;
	}

}

 

定义crud接口IStudentDao.java

package com.wxy;

import java.util.List;

public interface IStudentDao {
	   public void addStudentBySequence(Student student);
	   public void addStudent(Student student);
	   public void deleteStudentById(int id);
	   public void updateStudentById(Student student);
	   public List<Student>queryAllStudent();
	   public Student queryStudentById(int id);
	   public List<Student>queryStudentByName(String name);
}

 

在Student.xml中配置sql语句:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.rog//DTD SQL Map 2.0//EN" "http://ibatis.apche.org/dtd/sql-map-2.dtd">
<sqlMap>
<!--
   <resultMap id="StudentResult" class="Student">
      <result property="sid" column="sid"/>
      <result property="sname" column="sname"/>
      <result property="major" column="major"/>
      <result property=birth" column="birth"/>
   </resultMap>
-->
    <!-- typeAlias别名,缩减类名前的包名字 -->
	<typeAlias alias="Student" type="com.wxy.Student"/>
	
	<selet id ="selectAllStudent" resultClass="Student">
	select * from student
	</selet>
	<!--查询所有学生-->
   <select id="selectAllStudent" resultClass="Student">
       select * from student
   </select>


   <!--根据id查询学生-->
   <select id="selectStudentById" parameterClass="int" resultClass="Student">
      select * from student where id=#id#
   </select>


   <!--添加学生-->
   <insert id="insertStudent" parameterClass="Student">
      insert into Student(id, name, major, birth, score)
      values (#id#, #name#, #major#, #birth#, #score#)
   </insert>


   <!--根据id删除学生-->
   <delete id="deleteStudentById" parameterClass="int">
      delete from student where id = #id#
   </delete>


   <!--根据sid修改学生-->
   <update id="updateStudentById" parameterClass="Student">
      update Student set
             name=#name#
             major=#major#
             score=#score#
             birth=#birth#
      where 
             id=#id#
   </update>


   <!--模糊查询-->
   <select id="selectStudentByName" parameterClass="String" resultClass="Student">
      select id, name, major, birth, score from Student 
      where name like '%$name$%'
   </select>

<!--主键自增方式添加学生-->
<insert id="insertStudentBySequence" parameterClass="Student">
   <selectKey resultClass="int" keyProperty="id">
      select studentPKSequence.nextVal from dual
   </select>
      insert into Student(id, name, birth, major, score)
                   values(#id#, #name#, #birth#, #major#, #score#)
</insert>

	
	
</sqlMap>



 

  写接口的实现类IStudentDAOImpl .java

package com.wxy;

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/wxy/SqlMapConfig.xml");
	              sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
	              reader.close();
	           }catch(IOException e){
	                        e.printStackTrace();
	           }
	   }
	   
	   //查询所有学生
	   public List<Student>queryAllStudent(){
	      List<Student>studentList = null;
	      try{
	         studentList = sqlMapClient.queryForList("selectAllStudent");
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	      return studentList;
	   }
	   
	   //根据id查询学生
	   public Student queryStudentById(int id){
	      Student student = null;
	      try{
	         student = (Student)sqlMapClient.queryForObject("selectStudentById",id);
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	      return student;
	   }
	   
	   //添加学生
	   public void addStudent(Student student){
	      try{
	         sqlMapClient.insert("insertStudent", student);
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	   }
	   
	   //根据sid删除学生
	   public void deleteStudentById(int id){
	      try{
	         sqlMapClient.delete("deleteStudentById", id);
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	   }
	   
	    //根据id修改学生
	   public void updateStudentById(Student student){
	      try{
	         sqlMapClient.update("updateStudentById",student);
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	   }
	   
	   //模糊查询
	   public List<Student>queryStudentByName(String name){
	      List<Student>studentList = null;
	      try{
	         studentList = sqlMapClient.queryForList("selectStudentByName", name);
	      }catch(SQLException e){
	         e.printStackTrace();
	      }
	      return studentList;
	   }


	//根据主键自增方式添加学生,让数据库自动给对象设定id
	public void addStudentBySequence(Student student){
	    try{
	       sqlMapClient.insert("insertStudentBySequence",student);
	    }catch(SQLException e){
	      e.printStackTrace();
	    }
	}

	@Override
	public void updateStudentById(int id) {
		// TODO Auto-generated method stub
		
	}
}

 

编写测试类 ibatisTest.java

package com.wxy;

public class ibatisTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		IStudentDAO dao = new IStudentDAOImpl();
		
		for(Student student:dao.queryAllStudent()){
			System.out.println(student);
		}
	}

}

 

 

 

你可能感兴趣的:(编程,框架,ibatis,阿里巴巴)