使用 spring2.5 +ibatis2.3.4+oscache2.4+struts2+oracle
建表
create table STUDENT ( SID NUMBER(8) primary key not null, SNAME VARCHAR2(20) not null, MAJOR VARCHAR2(100), BIRTH DATE, SCORE NUMBER(6,2) ) -- Create sequence create sequence STUDENT_SEQ minvalue 1 maxvalue 999999999999999999999999999 start with 21 increment by 1 cache 20;
applicationContext.xml
classpath:SqlMapConfig.xml PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED readOnly
SqlMapConfig.xml
Student.xml
insert into student(sid,sname,major,birth,score) values (#sid#,#sname#,#major#,#birth#,#score#) delete from student where sid=#sid# update student set sname=#sname#, major=#major#, birth=#birth#, score=#score# where sid=#sid# select STUDENT_SEQ.nextVal from dual insert into student(sid,sname,major,birth,score) values (#sid#,#sname#,#major#,#birth#,#score#)
Student.java
package com.ibatis.student; import java.util.Date; public class Student { private int sid; private String sname; private String major; private Date birth; private float score; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } @Override public String toString() { // TODO Auto-generated method stub String content = "sid:" + sid + "\tsname:" + sname + "\tmajor:" + major + "\tbirth:" + birth + "\tscore=" + score; return content; } }
IStudentDAO.java
package com.ibatis.student; import java.util.List; public interface IStudentDAO { public void addStudent(Student student); //使用自动增长 主键 public void addStudentBySequence(Student student); public void delStudentById(int id); public void updStudentById(Student student); public ListqueryAllStudent(); //使用模糊查询 public List queryStudentByName(String name); public Student queryStudentById(int id); }
IStudentDAOImpl.java
package com.ibatis.student; import java.io.IOException; import java.io.Reader; import java.sql.Date; import java.sql.SQLException; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.ibatis.sqlmap.client.SqlMapClient; public class IStudentDAOImpl extends SqlMapClientDaoSupport implements IStudentDAO { //private static SqlMapClient sqlMapClient=null; /*static{ try { Reader reader=com.ibatis.common.resources.Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ public void addStudent(Student student) { // TODO Auto-generated method stub student=(Student)getSqlMapClientTemplate().insert("addStudent", student); System.out.println(student.getSid()); } public void addStudentBySequence(Student student) { // TODO Auto-generated method stub getSqlMapClientTemplate().insert("insertStudentBySequence", student); } public void delStudentById(int id) { // TODO Auto-generated method stub try { int rows=getSqlMapClientTemplate().delete("deleteStudentById", id); System.out.println(rows); } catch (DataAccessException e) { e.printStackTrace(); } } public ListqueryAllStudent() { // TODO Auto-generated method stub List studentList=null; try { studentList=getSqlMapClientTemplate().queryForList("queryAllStudent"); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return studentList; } public Student queryStudentById(int id) { Student student=null; try { student=(Student)getSqlMapClientTemplate().queryForObject("queryStudentById",id); } catch (Exception e) { e.printStackTrace(); } return student; } public List queryStudentByName(String name) { // TODO Auto-generated method stub List studentList=null; try { studentList=getSqlMapClientTemplate().queryForList("queryStudentByName", name); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return studentList; } public void updStudentById(Student student) { // TODO Auto-generated method stub try { int rows=getSqlMapClientTemplate().update("updateStudent", student); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
IStudentServiceImpl.java
package com.ibatis.student; import java.util.List; public class IStudentServiceImpl implements IStudentService { private IStudentDAO studentDAO; public void setStudentDAO(IStudentDAO studentDAO) { this.studentDAO = studentDAO; } public void addStudent(Student student) { // TODO Auto-generated method stub studentDAO.addStudent(student); } public void addStudentBySequence(Student student) { // TODO Auto-generated method stub studentDAO.addStudentBySequence(student); } public void delStudentById(int id) { // TODO Auto-generated method stub studentDAO.delStudentById(id); } public ListqueryAllStudent() { // TODO Auto-generated method stub return studentDAO.queryAllStudent(); } public Student queryStudentById(int id) { // TODO Auto-generated method stub return studentDAO.queryStudentById(id); } public List queryStudentByName(String name) { // TODO Auto-generated method stub return studentDAO.queryStudentByName(name); } public void updStudentById(Student student) { // TODO Auto-generated method stub studentDAO.updStudentById(student); } }
Test.java
package com.ibatis.student; import java.sql.Date; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String [] args){ ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); IStudentService studentService=(IStudentService)context.getBean("studentService"); //1. for (Student student : studentService.queryAllStudent()) { System.out.println(student); } //2. // System.out.println(dao.queryStudentById(1)); //3. // Student student=new Student(); // student.setSid(5); // student.setSname("admin"); // student.setScore(100); // student.setMajor("Games"); // student.setBirth(Date.valueOf("2008-08-08")); // // dao.addStudent(student); //4. // dao.delStudentById(1); //5. // Student student=new Student(); // student.setSid(2); // student.setSname("luob"); // student.setScore(50); // student.setMajor("Games"); // student.setBirth(Date.valueOf("2008-08-08")); // dao.updStudentById(student); //6. // for (Student student : dao.queryStudentByName("l%")) { // System.out.println(student); // } //7. /*Student student=new Student(); student.setSid(2); student.setSname("SCOTT"); student.setScore(50); student.setMajor("paly Games"); student.setBirth(Date.valueOf("2008-08-08")); studentService.addStudentBySequence(student); System.out.println("success");*/ } }
struts.xml
/index.jsp
BaseAction.java
package com.ibatis.student.common; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport{ public Object getServiceBean(String beanId){ ServletContext sc=ServletActionContext.getServletContext(); WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(sc); return ctx.getBean(beanId); } public HttpServletRequest getRequest(){ return ServletActionContext.getRequest(); } public HttpServletResponse getResponse(){ return ServletActionContext.getResponse(); } public MapgetSession() { ActionContext act=ActionContext.getContext(); return act.getSession(); } }
StudentAction.java
package com.ibatis.student.common; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport{ public Object getServiceBean(String beanId){ ServletContext sc=ServletActionContext.getServletContext(); WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(sc); return ctx.getBean(beanId); } public HttpServletRequest getRequest(){ return ServletActionContext.getRequest(); } public HttpServletResponse getResponse(){ return ServletActionContext.getResponse(); } public MapgetSession() { ActionContext act=ActionContext.getContext(); return act.getSession(); } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>${sesssion_msg} ${request_msg}Ibatis + Spring + Struts2