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

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 高焕杰 */@Overridepublic boolean add(Student student) throws SQLException{return getSqlMapClientTemplate().update("add", student) > 0;}/** * 依据用户名删除学生信息 * * @author 高焕杰 */@Overridepublic boolean deleteByUserName(String userName) throws SQLException{return getSqlMapClientTemplate().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 getSqlMapClientTemplate().update("updatePasswordByUserName", params) > 0;}/** * 根据学生用户名查询学生信息 * * @author 高焕杰 */@Overridepublic 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=""xmlns:xsi=""xmlns:tx=""xsi:schemaLocation=""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>只做第一个我,不做第二个谁。

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

相关文章:

你感兴趣的文章:

标签云: