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 print?
  1. #############################################################################################
  2. CREATE DATABASE MYDB;
  3. use MYDB;
  4. Drop TABLE IF EXISTS `MYDB`.`student`;
  5. Create TABLE `MYDB`.`student` (
  6. `name` varchar(40) NOT NULL,
  7. `psw` varchar(10) NOT NULL,
  8. `enabled` boolean
  9. );
  10. insert into student values("lanp","lanpiao",true);
  11. insert into student values("ph","ph",true);
  12. insert into student values("wxh","wxh",true);

3. 创建实体Bean,Student.Java

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

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

[html] view plaincopy print?
  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. <!-- 配置表和实体Bean之间的映射关系 -->
  9. <resultMap id="studentMap" class="com.lanp.beans.Student">
  10. <result property="name" column="name"/>
  11. <result property="psw" column="psw"/>
  12. <result property="enabled" column="enabled"/>
  13. </resultMap>
  14. <insert id="insertStudent" parameterClass="student">
  15. <![CDATA[
  16. insert into student values(#name#,#psw#,#enabled#);
  17. ]]>
  18. </insert>
  19. <!-- 查看特定用户 -->
  20. <select id="queryStudentById" parameterClass="string" resultMap="studentMap">
  21. <![CDATA[
  22. SELECT * FROM STUDENT WHERE NAME=#name#
  23. ]]>
  24. </select>
  25. <!-- 查看所有的用户 -->
  26. <select id="queryAllStudents" resultMap="studentMap">
  27. <![CDATA[
  28. SELECT * FROM STUDENT
  29. ]]>
  30. </select>
  31. </sqlMap>

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

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

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

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

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

[html] view plaincopy print?
  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 print?
  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. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  6. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  7. <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/>
  8. <property name="username" value="root"/>
  9. <property name="password" value="157891"/>
  10. </bean>
  11. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  12. <property name="configLocation">
  13. <value>sqlMapConfig.xml</value>
  14. </property>
  15. <property name="dataSource">
  16. <ref bean="dataSource"/>
  17. </property>
  18. </bean>
  19. <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl">
  20. <property name="sqlMapClient">
  21. <ref bean="sqlMapClient"/>
  22. </property>
  23. </bean>
  24. </beans>

9. 测试类,TestStudent:

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

OK,TKS!

Spring + Ibatis + MySql实例详解相关推荐

  1. ibatis mysql 配置文件详解_MyBatis Generator 配置文件详解

    MyBatis Generator (MBG) 是由一个XML配置文件驱动的.这个配置文件中会声明以下内容: 如何连接数据库 要生成什么对象,以及如何生成它们 哪些表需要应用于对象生成. 根元素 ge ...

  2. linux mysql 实例详解_MySQL 多实例详解

    目录 一.基本概念 1.MySQL多实例 就是在一台机器上面开启多个不同的端口,运行多个MySQL服务进程.这些MySQL多实例公用一套安装程序,使用不同的(也可以是相同的)配置文件,启动程序,数据文 ...

  3. mysql实例详解_MySQL 多实例详解

    MySQL多实例配置方法 1.单一配置文件 2.多配置文件. 二.实战步骤: 1.同步时间 2.准备mysql依赖包 3.环境准备 3.1添加mysql用户 3.2编译安装mysql 4.安装MySQ ...

  4. connection url mysql,JDBC 连接MySQL实例详解

    JDBC连接MySQL JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forNam ...

  5. ibatis mysql 配置文件详解_MyBatis配置文件详解

    Mybatis配置文件 /p> PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dt ...

  6. 微信小程序form表单提交到MYSQL实例详解(PHP)

    1.小程序相对于之前的WEB+PHP建站来说,个人理解为只是将web放到了微信端,用小程序固定的格式前前端进行布局.事件触发和数据的输送和读取,服务器端可以用任何后端语言写,但是所有的数据都要以JSO ...

  7. mysql的json函数与实例_Mysql实例详解Mysql中的JSON系列操作函数

    <Mysql实例详解Mysql中的JSON系列操作函数>要点: 本文介绍了Mysql实例详解Mysql中的JSON系列操作函数,希望对您有用.如果有疑问,可以联系我们. MYSQL必读前言 ...

  8. java stopwatch_Spring StopWatch使用实例详解

    这篇文章主要介绍了Spring StopWatch使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 StopWatch简单的秒表,允许多个 ...

  9. spring框架使用Quartz执行定时任务实例详解

    版权声明:本文为博主原创文章,如需转载,请标明出处. https://blog.csdn.net/alan_liuyue/article/details/80382324 Quartz简介 1.Qua ...

最新文章

  1. C++、嵌入式软开之数据结构
  2. Redis生产环境架构选型方案对比
  3. mysql更改可执行文件路径_Mysql 服务 1067 错误 的解决方法:修改mysql可执行文件路径...
  4. Python-EEG工具库MNE中文教程(1)-MNE中数据结构Raw及其用法简介
  5. 16道嵌入式C语言面试题
  6. 【Java Web开发指南】redis笔记
  7. IDEA系列(十)--新建一个项目后之前的项目不显示
  8. 小白入门深度学习 | 第五篇:数据不均衡的处理方法
  9. 错误代码中文查询GetLastError返回代码的含义
  10. VideoLAN,VLC和FFmpeg社区联合开发AV1解码器
  11. redhat mysql编译安装_RHEL6.2编译安装MySQL 5.6.16过程分享
  12. 在c语言程序中 对文件进行操作首先要,《C语言程序设计》试题八及答案
  13. 卢伟冰正式预告Redmi K30:首发双开孔全面屏+双模5G
  14. javascript、jquery获取网页的高度和宽度
  15. reply-code=404, reply-text=NOT_FOUND - no exchange
  16. JAVA消息中间件面试题
  17. 深入浅出设计模式 ------ Prototype(原型模式)之深度克隆
  18. 从开发应用的角度来看,物联网的关键技术包括哪几个方面?
  19. 解决 adb no permissions (user mi is not in the plugdev group);
  20. 简转繁等中文转换(Golang)

热门文章

  1. android 插件化 androdpluginmgr 扩展开发问题
  2. DWR操作java对象
  3. Maven 配置使用小技巧
  4. 响应式布局及bootstrap(实例)
  5. mini2440 貌似复杂的mmu
  6. IE9为啥没有加载进度进度提示
  7. 如何阻止分布式拒绝服务***
  8. 【安全风险通告】Spring Framework远程代码执行漏洞(CVE-2022-22965)安全风险通告第二次更新...
  9. 开源论坛软件 NodeBB 中存在多个严重漏洞
  10. 全球最大域名注册商 GoDaddy 的托管账户凭证遭泄露