Spring + Ibatis + MySql实例详解

1. 环境:
将以下jar包加入到工程,commons-logging-1.0.4.jar、ibatis-2.3.0.677.jar、mysql-connector-java-5.0.3-bin.jar、spring.jar。

2. 在MySql中创建数据库和相应的表:

#############################################################################################
CREATE DATABASE MYDB;
use MYDB;

Drop TABLE IF EXISTS `MYDB`.`student`;
Create TABLE `MYDB`.`student` (
`name` varchar(40) NOT NULL,
`psw` varchar(10) NOT NULL,
`enabled` boolean
);
insert into student values("lanp","lanpiao",true);
insert into student values("ph","ph",true);
insert into student values("wxh","wxh",true);

3. 创建实体Bean,Student.java

package com.lanp.beans;

import java.io.Serializable;

/**
 * Student Bean
 * @author LanP
 * @since 2011-11-27 15:36
 * @version V1.0
 */
public class Student implements Serializable {

	private static final long serialVersionUID = -7163004163334815825L;
	
	private String name;
	private String psw;
	private Boolean enabled;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPsw() {
		return psw;
	}
	public void setPsw(String psw) {
		this.psw = psw;
	}
	public Boolean getEnabled() {
		return enabled;
	}
	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
}
4. 创建Student实体Bean与数据库映射的SQLMap文件,student.xml:




	
	
	
	
	
		
		
		
	
	
	
		
	
	
	
	
	
	
	
5. 创建访问数据库的DAO接口,StudentDao.java:

package com.lanp.dao;

import com.lanp.beans.Student;

public interface StudentDao {
	Student getStudent(String name);
}

6. 创建访问数据库的DAO接口实现类,StudentDaoImpl.java:

package com.lanp.dao;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lanp.beans.Student;

public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {

	@Override
	public Student getStudent(String name) {
		try{
			return (Student)getSqlMapClientTemplate().queryForObject("queryStudentById", name);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}

7. Ibatis总配置文件,sqlMapConfig.xml:




	
	

8. spring配置文件,SQLMapClient.xml:




    
    
	  
	    
	    
	    
	    
	  
    
    
  		
    		sqlMapConfig.xml
  		
  		
    		
  		
	
    
	
  		
    		
  		
	
	

9. 测试类,TestStudent:

package com.lanp.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lanp.dao.StudentDao;

/**
 * 测试Ibatis
 * @author LanP
 * @since 2011-11-27 15:36
 * @version V1.0
 */
public class TestStudent {

	public static void main(String[] args) {

		//1.初始化beans.xml文件
		ApplicationContext ctx = new ClassPathXmlApplicationContext("SQLMapClient.xml");
		//2.获取MYDB数据库Student表中的内容
		StudentDao studentDao = (StudentDao)ctx.getBean("studentDao");
		if(null != studentDao) {
			Student student = studentDao.getStudent("ph");
			if(null != student) {
				System.out.println("== 学生名字:" + student.getName() + ",学生密码:" + student.getPsw());
			} else {
				System.out.println("== 没有该学生信息!");
			}
		} else {
			System.out.println("== StudentDao注入失败!");
		}
			
	}

}

OK,TKS!


你可能感兴趣的:(IT海洋——Java,IT海洋——Spring,IT海洋——Ibatis)