疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

spring与hibernate框架整合


 

       Spring和Ibatis框架整合的思路与spring和hibernate框架的整合思路基本一致。

  步骤一:新建立一个项目。

  步骤二:为该项目添加spring的应用环境。

  步骤三:导入Ibatis的必须JAR包以及数据库JAR包。

  步骤四:新建实体Bean。如下:

  package cn.test.entity;

  import java.io.Serializable;

  /**

  * @author Administrator

  *

  *学生实体Bean

  *

  */

  public class Student implements Serializable {

  /**

  *

  */

  private static final long serialVersionUID = 1L;

  private Integer id;

  private String studentname;

  private Integer studentage;

  private String studentaddress;

  public Integer getId() {

  return id;

  }

  public void setId(Integer id) {

  this.id = id;

  }

  public String getStudentname() {

  return studentname;

  }

  public void setStudentname(String studentname) {

  this.studentname = studentname;

  }

  public Integer getStudentage() {

  return studentage;

  }

  public void setStudentage(Integer studentage) {

  this.studentage = studentage;

  }

  public String getStudentaddress() {

  return studentaddress;

  }

  public void setStudentaddress(String studentaddress) {

  this.studentaddress = studentaddress;

  }

  }

  步骤五:新建相应的Bean的配置文件。如下:Student.xml

  PUBLIC “-//ibatis.apache.org//DTD SQL Map 2.0//EN”

  “ibatis.apache.org/dtd/sql-map-2.dtd“>

  select * from student where 1=1

  select * from student where id=#ids#

  步骤六:新建Ibatis的应用配置文件。sql-map-config.xml

  PUBLIC “-//ibatis.apache.org//DTD SQL Map Config 2.0//EN”

  “ibatis.apache.org/dtd/sql-map-config-2.dtd“>

  步骤七:修改spring的applicationContext.xml文件。添加如下代码:

  com.mysql.jdbc.Driver

  jdbc:mysql://localhost:3306/test

  root

  root

  cn/test/configfile/sql-map-config.xml

  步骤八:书写数据持久层次以及业务层代码。数据持久层代码如下:

  public class StudentDAOImple implements IStudentDAO{

  private SqlMapClientTemplate sqlMapClientTemplate;

  public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {

  this.sqlMapClientTemplate = sqlMapClientTemplate;

  }

  public List getAllStudent() {

  List list = sqlMapClientTemplate.queryForList(“getAllStudent”);

  return list;

  }

  }

  以及修改修改spring的applicationContext.xml文件

  步骤九:建立测试类以及测试方法。

  public class Test {

  /**

  * @param args

  */

  public static void main(String[] args) {

  ApplicationContext factory = new ClassPathXmlApplicationContext(“applicationContext.xml”);

  IStudentDAO dao = (IStudentDAO) factory.getBean(“studentDAO”);

  List list = dao.getAllStudent();

  for (int i = 0; i < list.size(); i++) {

  Student stu = list.get(i);

  System.out.println(stu.getId()+”:”+stu.getStudentname()+”:”+stu.getStudentage()+”:”+stu.getStudentaddress());

  }

  }

  }

  通过以上的配置,相信对SSI的配置也应该易如反掌了吧!