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:
<?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> <!-- 为Person类设置一个别名 --> <typeAlias alias="student" type="com.lanp.beans.Student"/> <!-- 配置表和实体Bean之间的映射关系 --> <resultMap id="studentMap" class="com.lanp.beans.Student"> <result property="name" column="name"/> <result property="psw" column="psw"/> <result property="enabled" column="enabled"/> </resultMap> <insert id="insertStudent" parameterClass="student"> <![CDATA[ insert into student values(#name#,#psw#,#enabled#); ]]> </insert> <!-- 查看特定用户 --> <select id="queryStudentById" parameterClass="string" resultMap="studentMap"> <![CDATA[ SELECT * FROM STUDENT WHERE NAME=#name# ]]> </select> <!-- 查看所有的用户 --> <select id="queryAllStudents" resultMap="studentMap"> <![CDATA[ SELECT * FROM STUDENT ]]> </select> </sqlMap>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:
<?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> <!-- 配置Ibatis要使用的SqlMap文件信息 --> <sqlMap resource="com/lanp/beans/student.xml"/> </sqlMapConfig>
8. spring配置文件,SQLMapClient.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 相关数据源和事务管理的定义 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/> <property name="username" value="root"/> <property name="password" value="157891"/> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>sqlMapConfig.xml</value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> </beans>
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注入失败!"); } } }