码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-03-03 22:12:12      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

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

        工程结构如下图:

技术分享

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

        StudentDao.java文件中的代码:

package com.ghj.dao.imp;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.ghj.dao.IStudentDao;
import com.ghj.vo.Student;

/**
 * 学生管理数据访问层接口实现类
 * 
 * @author 高焕杰
 */
public class StudentDao extends SqlMapClientDaoSupport implements IStudentDao {

	/**
	 * 添加学生信息
	 * 
	 * @author 高焕杰
	 */
	@Override
	public boolean add(Student student) throws SQLException{
		return getSqlMapClientTemplate().update("add", student) > 0;
	}

	/**
	 * 依据用户名删除学生信息
	 * 
	 * @author 高焕杰
	 */
	@Override
	public boolean deleteByUserName(String userName) throws SQLException{
		return getSqlMapClientTemplate().delete("deleteByUserName", userName) > 0;
	}

	/**
	 * 依据用户名更新密码
	 * 
	 * @author 高焕杰
	 */
	@Override
	public 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 getSqlMapClientTemplate().update("updatePasswordByUserName", params) > 0;
	}

	/**
	 * 根据学生用户名查询学生信息
	 * 
	 * @author 高焕杰
	 */
	@Override
	public Student findByUserName(String userName) throws SQLException{
		return (Student)getSqlMapClientTemplate().queryForObject("findByUserName", userName);
	}

	/**
	 * 查询所有学生信息
	 * 
	 * @author 高焕杰
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<Student> findAll() throws SQLException{
		return getSqlMapClientTemplate().queryForList("findAll", null);
	}
}

        application-context.xml文件中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	    http://www.springframework.org/schema/tx
	    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
	default-autowire="byName" default-lazy-init="false">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
		<property name="user" value="root"></property>
		<property name="password" value=""></property>
		<property name="acquireIncrement" value="5"></property>
		<property name="initialPoolSize" value="5"></property>
		<property name="minPoolSize" value="5"></property>
		<property name="maxPoolSize" value="20"></property>
		<property name="maxStatements" value="100"></property>
		<property name="numHelperThreads" value="10"></property>
		<property name="maxIdleTime" value="60"></property>
	</bean>

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  		<property name="configLocation">
            <value>classpath:config/sqlMapConfig.xml</value>
        </property>
  		<property name="dataSource" ref="dataSource"/>
	</bean>

	<bean id="studentDao" class="com.ghj.dao.imp.StudentDao">
  		<property name="sqlMapClient">
    		<ref bean="sqlMapClient"/>
  		</property>
	</bean>

	<!-- Spring中配置事务操作:在Spring中实现事务操作有多种方式,其中以注解的方式最为常用,该种方式需要在需要配置事务操作的方法上添加@Transactional注释   -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>

        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分下载该示例代码

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

标签:

原文地址:http://blog.csdn.net/gaohuanjie/article/details/44042979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!