Ibatis结合MySQL数据库的使用方法

来源:互联网 发布:mac mini好吗 编辑:程序博客网 时间:2024/03/29 05:55

        Ibatis是MyBatis的前身,它是一个开源的持久层框架。它的核心是SqlMap——将实体Bean跟关系数据库进行映射,将业务代码和SQL语句的书写进行分开。Ibatis是“半自动化”的ORM持久层框架。这里的“半自动化”是相对Hibernate等提供了全面的数据库封装机制的“全自动化”ORM实现而言的,“全自动”ORM实现了POJO与数据库表字段之间的映射并且实现了SQL的自动生成和执行。而Ibatis的着力点,则在于POJO与SQL之间的映射关系,即Ibatis并不会为程序员在运行期自动生成并执行SQL,具体的SQL语句需要程序员编写,然后通过映射配置文件将SQL语句所需的参数和返回的结果字段映射到指定POJO中。本篇博客将演示Ibatis结合MySQL数据库的使用方法:

        工程结构如下图:


        由于该例子介绍的比较全面,文件比较多,这里只给出上图标出的两个文件中的代码,完整的源码可通过点击本文最下面的超链接下载:

        StudentDao.java文件中的代码:

package com.ghj.dao.imp;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import com.ghj.dao.IStudentDao;import com.ghj.vo.Student;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;/** * 学生管理数据访问层接口实现类 *  * @author 高焕杰 */public class StudentDao implements IStudentDao {private SqlMapClient sqlMapClient;public StudentDao() {String resource = "config/sqlMapConfig.xml";try {Reader reader = Resources.getResourceAsReader(resource);//读取配置文件sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);} catch (IOException e) {e.printStackTrace();}}/** * 添加学生信息 *  * @author 高焕杰 */@Overridepublic boolean add(Student student) throws SQLException{return sqlMapClient.update("add", student) > 0;}/** * 依据用户名删除学生信息 *  * @author 高焕杰 */@Overridepublic boolean deleteByUserName(String userName) throws SQLException{return sqlMapClient.delete("deleteByUserName", userName) > 0;}/** * 依据用户名更新密码 *  * @author 高焕杰 */@Overridepublic boolean updatePasswordByUserName(String userName, String password) throws SQLException{Map<String, Object> params = new HashMap<String, Object>();params.put("userName", userName);params.put("password", password);return sqlMapClient.update("updatePasswordByUserName", params) > 0;}/** * 根据学生用户名查询学生信息 *  * @author 高焕杰 */@Overridepublic Student findByUserName(String userName) throws SQLException{return (Student)sqlMapClient.queryForObject("findByUserName", userName);}/** * 查询所有学生信息 *  * @author 高焕杰 */@Override@SuppressWarnings("unchecked")public List<Student> findAll() throws SQLException{return sqlMapClient.queryForList("findAll");}}

        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><!-- 为Student类设置一个别名 --><typeAlias alias="student" type="com.ghj.vo.Student"/><!-- 配置表和实体Bean之间的映射关系 --><resultMap id="studentMap" class="com.ghj.vo.Student"><result property="id" column="id"/><result property="userName" column="user_name"/><result property="password" column="password"/><result property="state" column="state"/></resultMap><!-- 添加学生信息 --><insert id="add" parameterClass="student">insert into student values(#id#, #userName#, #password#, #state#)</insert><!-- 依据用户名删除学生信息 --><delete id="deleteByUserName" parameterClass="java.lang.String">      delete from student where user_name=#userName# </delete><!-- 依据用户名更新密码 --><update id="updatePasswordByUserName" parameterClass="java.util.HashMap">        update student set password=#password# where user_name=#userName# </update><!-- 根据学生用户名查询学生信息 --><select id="findByUserName" parameterClass="string" resultMap="studentMap">select * from student where user_name=#userName#</select><!-- 查询所有学生信息 --><select id="findAll" resultMap="studentMap">select * from student</select></sqlMap>

        【0分下载该示例代码

0 0
原创粉丝点击