`
cozilla
  • 浏览: 89444 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

spring+ibatis+mysql简单搭建

阅读更多

Ibatis2.3.4、spring2.0、mysql5.1、mysql-connect-java-5.0.4-bin.jar。

1、Mysql准备数据(use itcast数据库)

mysql> create  table  student(id int  primary  key  auto_increment, firstname varchar
(20), lastname varchar (20));

2、准备POJO类

package  cn.itcast;

public  class  Student implements  java.io.Serializable {
  private  static  final  long  serialVersionUID = 1L;
  private  Integer id;
  private  String firstname;
  private  String lastname;

  public  String getFirstname() {
    return  firstname;
  }

  public  void  setFirstname(String firstname) {
    this .firstname = firstname;
  }

  public  Integer getId() {
    return  id;
  }

  public  void  setId(Integer id) {
    this .id = id;
  }

  public  String getLastname() {
    return  lastname;
  }

  public  void  setLastname(String lastname) {
    this .lastname = lastname;
  }
}

3、准备3大类配置文件, 都在包configfile下面

(1)与POJO的映射文件:Student.xml
<? xml  version ="1.0"  encoding ="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
  <!--这是POJO映射文件的根元素 -->
< sqlMap  namespace ="Student" >
  <!--
    select元素的id属性用来标识此元素,resultClass属性的值是Java类的全限定名(即包括类的包名)。
    resultClass属性可以让您指定一个Java类,根据ResultSetMetaData将其自动映射到JDBC的ResultSet。
    只要是Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。
    parameterClass属性是参数的类型,此属性的值是Java类的全限定名(即包括类的包名)。 它是可选的,但强烈建议使用。它的目的是
    限制输入参数的类型为指定的Java类,并 优化框架的性能。
  
-->
  < select  id ="getStudentById"  resultClass ="cn.itcast.Student"
    parameterClass ="int" >
    select id,firstname,lastname from student where id=#value#
  </ select >

  < insert  id ="insertStudent"  parameterClass ="cn.itcast.Student" >
    insert into student(firstname,lastname) values(#firstname#,#lastname#)
  </ insert >
</ sqlMap >

(2)连接mysql数据库的配置文件:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/itcast
jdbc.username=root
jdbc.password=admin

(3)Ibatis的总控文件:sql-map-config.xml
<? xml  version ="1.0"  encoding ="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
< sqlMapConfig >
  < sqlMap  resource ="configfile/Student.xml"  />
</ sqlMapConfig >

3、此时该Spring出场了,Spring的配置文件(applicationContext.xml)将jdbc.properties配置文件的内容关联成DataSource,再把DataSource关联到Ibatis的配置文件, 最后统一封装成为SqlMapClientTemplate模板类向外提供服务。 
<? xml  version ="1.0"  encoding ="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

< beans >
  <!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句 -->
  < bean  id ="propertyConfig"
    class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    < property  name ="location" >
      < value > configfile/jdbc.properties</ value >
    </ property >
  </ bean >

  <!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息 -->
  < bean  id ="dataSource"
    class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    < property  name ="driverClassName" >
      < value > ${jdbc.driver}</ value >
    </ property >
    < property  name ="url" >
      < value > ${jdbc.url}</ value >
    </ property >
    < property  name ="username" >
      < value > ${jdbc.username}</ value >
    </ property >
    < property  name ="password" >
      < value > ${jdbc.password}</ value >
    </ property >
  </ bean >

  <!--根据dataSource和configLocation创建一个SqlMapClient -->
  < bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
    < property  name ="configLocation" >
      < value > configfile/sql-map-config.xml</ value >
    </ property >
    < property  name ="dataSource" >
      < ref  bean ="dataSource"  />
    </ property >
  </ bean >

  <!--根据sqlMapClien创建一个SqlMapClient模版类 -->
  < bean  id ="sqlMapClientTemplate"  class ="org.springframework.orm.ibatis.SqlMapClientTemplate" >
    < property  name ="sqlMapClient" >
      < ref  bean ="sqlMapClient"  />
    </ property >
  </ bean >

  <!--将上面的模版类织入到我们的DAO对象中 -->
  < bean  id ="studentDao"  class ="cn.itcast.StudentDaoSqlMap" >
    < property  name ="sqlMapClientTemplate" >
      < ref  bean ="sqlMapClientTemplate"  />
    </ property >
  </ bean >

</ beans >

4、在Dao层使用模板类

此时再次可以Spring的强大的注入功能,将SqlMapClientTemplate注入到DAO层之中,这样就可以像操作对象一样操作数据库了。
package  cn.itcast;

import  org.springframework.orm.ibatis.SqlMapClientTemplate;

public  class  StudentDaoSqlMap {
  private  SqlMapClientTemplate sqlMapClientTemplate;

  public  SqlMapClientTemplate getSqlMapClientTemplate() {
    return  sqlMapClientTemplate;
  }

  public  void  setSqlMapClientTemplate(
      SqlMapClientTemplate sqlMapClientTemplate) {
    this .sqlMapClientTemplate = sqlMapClientTemplate;
  }

  // 此方法的返回值与Student.xml的select元素的resultClass对应.
  public  Student getStudent(Integer id) {
    return  (Student) sqlMapClientTemplate.queryForObject("getStudentById" ,
        id);
    // 注意:queryForObject方法返回一个Object,第一个参数与Student.xml的select元素
    // 的id属性值对应,第二个参数的类型与Student.xml的select元素的parameterClass
    // 属性值对应.
  }

  public  Object insertStudent(Student student) {
    return  sqlMapClientTemplate.insert("insertStudent" , student);
  }
}

5、客户端测试:Client.java

package  cn.itcast;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public  class  Client {

  public  static  void  main(String[] args) {
    ApplicationContext factory = new  ClassPathXmlApplicationContext(
        "applicationContext.xml" );

    StudentDaoSqlMap studentDao = (StudentDaoSqlMap) factory
        .getBean("studentDao" );

    // 插入一个student
    Student student = new  Student();
    student.setFirstname("tian" );
    student.setLastname("xiangdong" );
    studentDao.insertStudent(student);

    // 查询出id是1的Student对象.
    // Student student = studentDao.getStudent(1);
    // System.out.println(student.getId());
    // System.out.println(student.getFirstname());
    // System.out.println(student.getLastname());
  }

}

6、总结

【注意】其中用到了Spring读取properites文件的PropertyPlaceholderConfigurer类,配置称DataSource的DriverManagerDataSource类,对Ibatis配置文件的工厂类SqlMapClientFactoryBean, 统一向外提供服务的SqlMapClientTemplate模板类等, 最后把这个摸版类再注入到需要的Dao层之中 。 一定到理顺他们之间的关系,这个很关键。

分享到:
评论

相关推荐

    Maven+spring+ struts2+ Ibatis+mysql整合增删改查

    Maven+spring+ struts2+ Ibatis+mysql整合增删改查

    Struts+Spring+iBatis

    Struts 1.2.9 + Spring 2.5.6 + iBATIS 2.3.4 + MySQL 3.1 + Tomcat 5.5.26 &gt; 开发环境: * MyEclipse、JDK1.5、J2EE... * 采用 Struts 1.2.9 + Spring 2.5.6 + iBATIS 2.3.4 + MySQL 3.1 + Tomcat 5.5.26 来搭建环境

    spring mvc+ibatiS+mysql代码

    在myeclipse10+spring mvc+ibatiS搭建的一个demo。后台数据库使用mysql,表参见实体类建立

    struts2+spring+ibatis框架实例

    搭建好的struts2+spring+ibatis框架,用的是mysql数据库

    flex+spring+struts2+ibatis 整合的eclipse工程

    flex+spring+struts2+ibatis 整合的eclipse工程,可以导入eclipse环境下直接使用,因为加入开发的jar大于了上传的最大限制,只能把jar另外打包上传,下载可以从我上传资源的lib1,lib2下载,这个工程的搭建花费了我两...

    ibatis+spring+cxf+mysql搭建webservice的客户端

    ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729

    ibatis+spring+cxf+mysql搭建的myWebservice服务端

    ibatis+spring+cxf+mysql搭建webservice的服务端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755

    spring3 ibatis struts2 搭建的简单项目

    spring3 ibatis sturts2搭建的简单项目,只供学习用,里面只有一个简单的例子,框架本身可用 使用mysql数据库

    struts2+ibatis+spring整合开发.doc

    SSI+mysql的框架搭建。这个文档是作为我发帖求助用,给想搭SSI的人借鉴一下,我是从百度文库下下来的。。。但是因为我用的是oracle,这里用的mysql,而且,里面jar包比较老,想找人问问。

    Java开发工程师简历模板.docx

    SpringMVC+Spring+ibatis+Springboot+SpringCloud+Redis+MySQL+Shiro+ActiveMQ+Thymeleaf 1.项目使用 IDEA 开发环境; 2.使用 maven 搭建项目工程,Git 工具管理项目代码; 3.技术框架为:SpringBoot+SpringCloud +...

    SSI框架搭建源码

    stuts+spring+ibatis框架源码,webroot文件下用user数据库,放在mysql中即可使用

    jdbc+mybatis+spring所有jar包

    所以,搭建ibatis的框架也会有多种方式(我这里mybatis是3.0的,ibatis是2.3的,spring是3.0的,数据库是mysql)。下面介绍3中方式 1,只是用mybatis3。 2,使用mybatis3+spring3(使用mybatis的SqlSessionFactory ...

    CXF Spring搭建WebServer服务器

    使用的是CXF+Spring搭建,后台DB层使用ibatis ,数据库使用Mysql 5.5,后台与页面传输使用的是Json数据格式.

    SSI实例(源码+mysql数据库+部署)

    SSI实例(源码+数据库+部署说明),数据库使用连接池,自己编写的搭建框架的代码,包含登录,增删改查,包含jar包: commons-dbcp.jar ibatis-2.3.0.677.jar mysql-connector-java-5.1.13.jar spring-aop-3.2.1....

    SpringCloud技术栈微服务架构天猫商城企业级开发实战(附源码)

    天猫商城是一个基于SSM框架的综合性B2C电商平台,需求设计主要参考天猫商城的购物流程:用户从注册开始,到完成登录,浏览商品,加入购物车,进行下单,确认收货...采用SpringMVC+Spring+IBatis完成项目的整合 采用Mysq

    springmybatis

    前面一章,已经搭建好了eclipse,mybatis,mysql的环境,并且实现了一个简单的查询。请注意,这种方式是用SqlSession实例来直接执行已映射的SQL语句: session.selectOne(...

    WindRapid1.0.0

    可以快速搭建项目 支持 spring struts2 springMVC ibatis mybatis oracle mysql 建好数据库到能访问 只需要 2分钟

    基于EXT SSI的简单树实现

    这个实例基于spring+struts+ibatis,外加js lib ext 3.3.1. 发挥连接作用的就是这个/WEB-INF/web.xml文件了。贴内容。 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi=...

    SSI框架搭建增删改查以及分页

    struts2 Spring ibatis 整合实现增删改查以及分页 含有sql 到oracle数据库里执行就好了很适合新手 那个功能都实现了 而且代码很清晰

Global site tag (gtag.js) - Google Analytics