`

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中创建数据库和相应的表:

 

[sql] view plaincopy
 
  1. ############################################################################################# 
  2. CREATE DATABASE MYDB; 
  3. use MYDB; 
  4.  
  5. Drop TABLE IF EXISTS `MYDB`.`student`; 
  6. Create TABLE `MYDB`.`student` ( 
  7. `namevarchar(40) NOT NULL
  8. `psw` varchar(10) NOT NULL
  9. `enabled` boolean 
  10. ); 
  11. insert into student values("lanp","lanpiao",true); 
  12. insert into student values("ph","ph",true); 
  13. insert into student values("wxh","wxh",true); 
[sql] view plaincopy
 
  1. #############################################################################################  
  2. CREATE DATABASE MYDB;  
  3. use MYDB;  
  4.   
  5. Drop TABLE IF EXISTS `MYDB`.`student`;  
  6. Create TABLE `MYDB`.`student` (  
  7. `namevarchar(40) NOT NULL,  
  8. `psw` varchar(10) NOT NULL,  
  9. `enabled` boolean  
  10. );  
  11. insert into student values("lanp","lanpiao",true);  
  12. insert into student values("ph","ph",true);  
  13. insert into student values("wxh","wxh",true);  

 

 

 

3. 创建实体Bean,Student.java

 

[java] view plaincopy
 
  1. package com.lanp.beans; 
  2.  
  3. import java.io.Serializable; 
  4.  
  5. /**
  6. * Student Bean
  7. * @author LanP
  8. * @since 2011-11-27 15:36
  9. * @version V1.0
  10. */ 
  11. public class Student implements Serializable { 
  12.  
  13.     private static final long serialVersionUID = -7163004163334815825L; 
  14.      
  15.     private String name; 
  16.     private String psw; 
  17.     private Boolean enabled; 
  18.      
  19.     public String getName() { 
  20.         return name; 
  21.     } 
  22.     public void setName(String name) { 
  23.         this.name = name; 
  24.     } 
  25.     public String getPsw() { 
  26.         return psw; 
  27.     } 
  28.     public void setPsw(String psw) { 
  29.         this.psw = psw; 
  30.     } 
  31.     public Boolean getEnabled() { 
  32.         return enabled; 
  33.     } 
  34.     public void setEnabled(Boolean enabled) { 
  35.         this.enabled = enabled; 
  36.     } 
[java] view plaincopy
 
  1. package com.lanp.beans;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * Student Bean 
  7.  * @author LanP 
  8.  * @since 2011-11-27 15:36 
  9.  * @version V1.0 
  10.  */  
  11. public class Student implements Serializable {  
  12.   
  13.     private static final long serialVersionUID = -7163004163334815825L;  
  14.       
  15.     private String name;  
  16.     private String psw;  
  17.     private Boolean enabled;  
  18.       
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public String getPsw() {  
  26.         return psw;  
  27.     }  
  28.     public void setPsw(String psw) {  
  29.         this.psw = psw;  
  30.     }  
  31.     public Boolean getEnabled() {  
  32.         return enabled;  
  33.     }  
  34.     public void setEnabled(Boolean enabled) {  
  35.         this.enabled = enabled;  
  36.     }  
  37. }  

4. 创建Student实体Bean与数据库映射的SQLMap文件,student.xml:

 

 

 

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE sqlMap       
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"       
  4.     "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
  5. <sqlMap> 
  6.     <!-- 为Person类设置一个别名 --> 
  7.     <typeAlias alias="student" type="com.lanp.beans.Student"/> 
  8.      
  9.     <!-- 配置表和实体Bean之间的映射关系 --> 
  10.     <resultMap id="studentMap" class="com.lanp.beans.Student"> 
  11.         <result property="name" column="name"/> 
  12.         <result property="psw" column="psw"/> 
  13.         <result property="enabled" column="enabled"/> 
  14.     </resultMap> 
  15.      
  16.     <insert id="insertStudent" parameterClass="student"> 
  17.         <![CDATA[
  18.             insert into student values(#name#,#psw#,#enabled#);
  19.         ]]> 
  20.     </insert> 
  21.      
  22.     <!-- 查看特定用户 --> 
  23.     <select id="queryStudentById" parameterClass="string" resultMap="studentMap"> 
  24.         <![CDATA[
  25.             SELECT * FROM STUDENT WHERE NAME=#name#
  26.         ]]> 
  27.     </select> 
  28.      
  29.     <!-- 查看所有的用户 --> 
  30.     <select id="queryAllStudents" resultMap="studentMap"> 
  31.         <![CDATA[
  32.             SELECT * FROM STUDENT
  33.         ]]> 
  34.     </select> 
  35. </sqlMap> 
[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMap        
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
  4.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
  5. <sqlMap>  
  6.     <!-- 为Person类设置一个别名 -->  
  7.     <typeAlias alias="student" type="com.lanp.beans.Student"/>  
  8.       
  9.     <!-- 配置表和实体Bean之间的映射关系 -->  
  10.     <resultMap id="studentMap" class="com.lanp.beans.Student">  
  11.         <result property="name" column="name"/>  
  12.         <result property="psw" column="psw"/>  
  13.         <result property="enabled" column="enabled"/>  
  14.     </resultMap>  
  15.       
  16.     <insert id="insertStudent" parameterClass="student">  
  17.         <![CDATA[ 
  18.             insert into student values(#name#,#psw#,#enabled#); 
  19.         ]]>  
  20.     </insert>  
  21.       
  22.     <!-- 查看特定用户 -->  
  23.     <select id="queryStudentById" parameterClass="string" resultMap="studentMap">  
  24.         <![CDATA[ 
  25.             SELECT * FROM STUDENT WHERE NAME=#name# 
  26.         ]]>  
  27.     </select>  
  28.       
  29.     <!-- 查看所有的用户 -->  
  30.     <select id="queryAllStudents" resultMap="studentMap">  
  31.         <![CDATA[ 
  32.             SELECT * FROM STUDENT 
  33.         ]]>  
  34.     </select>  
  35. </sqlMap>  

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

 

 

 

 

[java] view plaincopy
 
  1. package com.lanp.dao; 
  2.  
  3. import com.lanp.beans.Student; 
  4.  
  5. public interface StudentDao { 
  6.     Student getStudent(String name); 
[java] view plaincopy
 
  1. package com.lanp.dao;  
  2.   
  3. import com.lanp.beans.Student;  
  4.   
  5. public interface StudentDao {  
  6.     Student getStudent(String name);  
  7. }  

 

 

 

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

 

[java] view plaincopy
 
  1. package com.lanp.dao; 
  2.  
  3. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; 
  4.  
  5. import com.lanp.beans.Student; 
  6.  
  7. public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao { 
  8.  
  9.     @Override 
  10.     public Student getStudent(String name) { 
  11.         try
  12.             return (Student)getSqlMapClientTemplate().queryForObject("queryStudentById", name); 
  13.         } catch(Exception e) { 
  14.             e.printStackTrace(); 
  15.         } 
  16.         return null
  17.     } 
  18.  
[java] view plaincopy
 
  1. package com.lanp.dao;  
  2.   
  3. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  
  4.   
  5. import com.lanp.beans.Student;  
  6.   
  7. public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {  
  8.   
  9.     @Override  
  10.     public Student getStudent(String name) {  
  11.         try{  
  12.             return (Student)getSqlMapClientTemplate().queryForObject("queryStudentById", name);  
  13.         } catch(Exception e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         return null;  
  17.     }  
  18.   
  19. }  

 

 

 

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

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE sqlMapConfig       
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"       
  4.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 
  5. <sqlMapConfig> 
  6.     <!-- 配置Ibatis要使用的SqlMap文件信息 --> 
  7.     <sqlMap resource="com/lanp/beans/student.xml"/> 
  8. </sqlMapConfig> 
[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMapConfig        
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
  4.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  5. <sqlMapConfig>  
  6.     <!-- 配置Ibatis要使用的SqlMap文件信息 -->  
  7.     <sqlMap resource="com/lanp/beans/student.xml"/>  
  8. </sqlMapConfig>  

 

 

 

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

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
  3. <beans> 
  4.      
  5.     <!-- 相关数据源和事务管理的定义 --> 
  6.       <bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
  7.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
  8.         <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/> 
  9.         <property name="username" value="root"/> 
  10.         <property name="password" value="157891"/> 
  11.       </bean> 
  12.      
  13.     <bean id="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
  14.         <property name="configLocation"> 
  15.             <value>sqlMapConfig.xml</value> 
  16.         </property> 
  17.         <property name="dataSource"> 
  18.             <ref bean="dataSource"/> 
  19.         </property> 
  20.     </bean> 
  21.      
  22.     <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl"> 
  23.         <property name="sqlMapClient"> 
  24.             <ref bean="sqlMapClient"/> 
  25.         </property> 
  26.     </bean> 
  27.      
  28. </beans> 
[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.       
  5.     <!-- 相关数据源和事务管理的定义 -->  
  6.       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  7.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  8.         <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/>  
  9.         <property name="username" value="root"/>  
  10.         <property name="password" value="157891"/>  
  11.       </bean>  
  12.       
  13.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  14.         <property name="configLocation">  
  15.             <value>sqlMapConfig.xml</value>  
  16.         </property>  
  17.         <property name="dataSource">  
  18.             <ref bean="dataSource"/>  
  19.         </property>  
  20.     </bean>  
  21.       
  22.     <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl">  
  23.         <property name="sqlMapClient">  
  24.             <ref bean="sqlMapClient"/>  
  25.         </property>  
  26.     </bean>  
  27.       
  28. </beans>  

 

 

 

9. 测试类,TestStudent:

 

  1. package com.lanp.beans; 
  2.  
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6. import com.lanp.dao.StudentDao; 
  7.  
  8. /**
  9. * 测试Ibatis
  10. * @author LanP
  11. * @since 2011-11-27 15:36
  12. * @version V1.0
  13. */ 
  14. public class TestStudent { 
  15.  
  16.     public static void main(String[] args) { 
  17.  
  18.         //1.初始化beans.xml文件 
  19.         ApplicationContext ctx = new ClassPathXmlApplicationContext("SQLMapClient.xml"); 
  20.         //2.获取MYDB数据库Student表中的内容 
  21.         StudentDao studentDao = (StudentDao)ctx.getBean("studentDao"); 
  22.         if(null != studentDao) { 
  23.             Student student = studentDao.getStudent("ph"); 
  24.             if(null != student) { 
  25.                 System.out.println("== 学生名字:" + student.getName() + ",学生密码:" + student.getPsw()); 
  26.             } else { 
  27.                 System.out.println("== 没有该学生信息!"); 
  28.             } 
  29.         } else { 
  30.             System.out.println("== StudentDao注入失败!"); 
  31.         } 
  32.              
  33.     } 
  34.  

 

原文地址:http://blog.csdn.net/lanpiao_87/article/details/7036409

分享到:
评论

相关推荐

    jpetstore (spring+strust+ibatis)

    《基于Spring+Struts+Ibatis的jPetStore实战详解》 jPetStore是一个经典的开源项目,它展示了如何使用Spring、Struts和Ibatis等技术构建一个完整的MVC(Model-View-Controller)架构的Web应用程序。这个项目是Java...

    ibatis+mysql

    《Ibatis与MySQL整合详解:从入门到精通》 Ibatis,一款优秀的Java持久层框架,以其轻量级、灵活性和高效性深受开发者喜爱。它将SQL语句与Java代码分离,提供了更直观的数据库操作方式。而MySQL,作为世界上最受...

    springboot+mybatis plus整合案例

    在本文中,我们将深入探讨如何将SpringBoot与MyBatis Plus进行整合,以便构建一个高效、简洁的...在实际应用中,你还可以结合其他SpringBoot特性,如自动配置、AOP、Spring Security等,进一步提升应用的功能和安全性。

    ibatis简单实例

    # iBATIS 简单实例详解 iBATIS 是一款优秀的开源持久层框架,它为Java应用程序提供了灵活的数据访问接口。本篇文章将带你快速上手iBATIS,理解其配置、基础语义以及高级特性,并通过一个简单的实例来演示其工作流程...

    ibatis的jar包

    **Ibatis jar包详解** Ibatis 是一个优秀的Java持久层框架,它主要负责数据库操作,提供了灵活的SQL映射机制,让开发者能够更方便地控制SQL的编写与执行。Ibatis 不是完整的ORM(对象关系映射)框架,而是简化了...

    ibatis相关配置

    ### Ibatis配置详解 1. **全局配置文件(ibatis-config.xml)** 全局配置文件是Ibatis系统的起点,它包含了数据源、事务管理器、插件、类型别名等整体设置。例如: ```xml &lt;!DOCTYPE configuration PUBLIC "-/...

    IBATIS 配置+简单案列

    使用Spring框架时,可以使用`@Autowired`注解自动注入`UserMapper`实例。 ### 5. 简单案例 假设我们有一个简单的用户注册功能,可以通过以下步骤实现: 1. 创建`User`实体类。 2. 在`UserMapper.xml`中编写插入...

    ibatis和java集成

    **Ibatis和Java集成详解** Ibatis,作为一个轻量级的持久层框架,与Java的集成使得数据库操作变得更加简单和灵活。它不同于传统的ORM(对象关系映射)框架如Hibernate,Ibatis更注重SQL的控制权,允许开发者编写...

    ibatis 入门例子

    **Ibatis 入门例子详解** Ibatis 是一款优秀的、轻量级的Java持久层框架,它主要解决了Java应用程序与数据库交互的问题。本教程将通过一个基础的Ibatis入门示例,帮助你理解如何在实际项目中使用Ibatis进行数据操作...

    ibatis 入门代码

    **Ibatis 入门代码详解** Ibatis 是一个优秀的开源持久层框架,它允许开发者将SQL语句直接写在配置文件中,与Java代码进行分离,从而降低了数据库操作的复杂性,提高了开发效率。本教程将带你一步步了解并掌握...

    ibatis 环境搭建

    - 需要注意的是,这里使用了Spring框架的注解,如果你的项目中没有引入Spring,可以手动创建`SqlSession`和`SqlSessionFactory`实例。 7. **测试** - 创建一个`UserTest`类,使用JUnit或Spock等测试框架来测试`...

    mybatis的Helloworld

    本篇文章将详细解析MyBatis的“Hello World”实例,帮助初学者快速入门。 【描述】"初始的mybatis的helloworld文档" MyBatis的“Hello World”教程是学习其基本概念和配置的绝佳起点。通过这个简单的例子,我们...

    初识MyBatis

    【初识MyBatis——实现增删改查详解】 MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解...

    iuhyiuhkjh908u0980

    ant配置文件实例详解 build.xml 代码 xml version="1.0" encoding="UTF-8"?&gt; &lt;!-- name:对应工程的名字;default:需要的缺省任务(运行"ant"不指明任务时执行的任务) --&gt; by duzn 2007-04-02 回复 (0) Antenna与j2me...

    文思面试题(java)

    【Java工程师面试知识点详解】 1. **项目经验与开发流程**:面试时,面试官可能会询问你在公司的任职时间,参与的项目以及项目的意义。你需要准备关于项目简介、目标、你在项目中的角色,以及项目开发流程的详细...

Global site tag (gtag.js) - Google Analytics