先来看目录结构
来看配置文件
applicationContext.xml
mybatis-config.xml
struts.xml
/index.jsp /index.jsp /index.jsp /index1.jsp /index1.jsp
web.xml
contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* index.jsp
--映射文件
StudentMapper.xml
sid,sname,score select STUDENT_SEQ.nextVal from dual insert into student(sid,sname,major,birth,score) values (#{sid},#{sname},#{major},#{birth},#{score})insert into student(sid,sname,major,birth,score) values (#{sid},#{sname},#{major},#{birth},#{score}) delete student where sid=#{sid} update student where sid=#{sid} sname=#{sname}, majoir=#{major}, birth=#{birth}, score=#{score}
ClassesMapper.xml
delete classes where cid=#{cid}
--dao 和 impl
IStudentDAO.java
package com.mybatis.student; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.mybatis.classes.Classes; /** * 手动写dao 然后注入sqlSession 或者 继承 * @author Administrator * */ public interface IStudentDAO { //手动添加 id public int addStudent(Student student); //自动生成 id public int addStudentBySequence(Student student); //根据id删除 public int delStudentById(int id); //测试更新 public int updStudentById(Student student); //查询所有 public ListqueryAllStudent(); //测试 模糊查询 public List queryStudentByName(Student name); //测试 id查询 public Student queryStudentById(int id); //测试 自定义 resultMap List studentResultMap(String sname); //测试 左连接查询 List selectStudentAndClassBySname(String sname); //测试 右联合查询 Classes selectClassAndStudentListById(int id); //测试动态sql List selectStudentByDynamicSql(Student student); //测试动态sql List selectStudentByDynamicSqlChoose(Student student); //测试 foreach 和集合 List selectStudentByIds(ArrayList ids); //测试 foreach 和 数组 List selectStudentByIdArray(Integer[] idArry); //测试 map Map getAllStudentAfterupdate(Map map); }
IStudentDAOImpl.java
package com.mybatis.student; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate; import com.mybatis.classes.Classes; /** * @author Administrator * 如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession * 可以只注入sqlSessionFactory 然后 像mybatis 中使用 sqlSession 一样 openSession() .close() * 否则 可以 继承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") * 和 SqlSessionTemplate 得到 spring 管理的 线程安全的 sqlSession * 或者 简单的使用XML中配置 MapperFactoryBean 这样就省去了我们 获取sqlSession了 */ public class IStudentDAOImpl implements IStudentDAO { private SqlSessionTemplate sqlSession; public SqlSessionTemplate getSqlSession() { return sqlSession; } public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public int addStudent(Student student) { // TODO Auto-generated method stub return getSqlSession().insert("com.mybatis.student.addStudent", student); } public int addStudentBySequence(Student student) { // TODO Auto-generated method stub return getSqlSession().insert("com.mybatis.student.addStudentBySequence", student); } public int delStudentById(int id) { int rows=getSqlSession().delete("com.mybatis.student.delStudentById", id); System.out.println(rows); return rows; } public ListqueryAllStudent() { List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.queryAllStudent"); return stuList; } public Student queryStudentById(int id) { // TODO Auto-generated method stub return (Student)getSqlSession().selectOne("com.mybatis.student.queryStudentById",id); } public Map getAllStudentAfterupdate(Map map) { // TODO Auto-generated method stub getSqlSession().selectOne("com.mybatis.student.getAllStudentAfterupdate",map); return map; } public Classes selectClassAndStudentListById(int id) { // TODO Auto-generated method stub return (Classes)getSqlSession().selectOne("com.mybatis.classes.selectClassAndStudentListById",id); } public List selectStudentAndClassBySname(String sname) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.selectStudentAndClassBySname",sname); return stuList; } public List selectStudentByDynamicSql(Student student) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSql",student); } public List selectStudentByDynamicSqlChoose(Student student) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student); } public List selectStudentByIdArray(Integer[] idArry) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByIdArray",idArry); } public List selectStudentByIds(ArrayList ids) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByIds",ids); } public List studentResultMap(String sname) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectMapResult",sname); } public List queryStudentByName(Student name) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.queryStudentByName","%"+name+"%"); return stuList; } public int updStudentById(Student student) { return getSqlSession().update("com.mybatis.student.addStudentBySequence", student); } }
IStudentDAOImpl_sqlSessionDaoSupport.java
package com.mybatis.student; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.mybatis.classes.Classes; /** * @author Administrator * 如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession * 可以只注入sqlSessionFactory 然后 像mybatis 中使用 sqlSession 一样 openSession() .close() * 否则 可以 继承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") * 和 SqlSessionTemplate 得到 spring 管理的 线程安全的 sqlSession * 或者 简单的使用XML中配置 MapperFactoryBean 这样就省去了我们 获取sqlSession了 */ public class IStudentDAOImpl_sqlSessionDaoSupport extends SqlSessionDaoSupport implements IStudentDAO { public int addStudent(Student student) { // TODO Auto-generated method stub return getSqlSession().insert("com.mybatis.student.addStudent", student); } public int addStudentBySequence(Student student) { // TODO Auto-generated method stub return getSqlSession().insert("com.mybatis.student.addStudentBySequence", student); } public int delStudentById(int id) { int rows=getSqlSession().delete("com.mybatis.student.delStudentById", id); System.out.println(rows); return rows; } public ListqueryAllStudent() { List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.queryAllStudent"); return stuList; } public Student queryStudentById(int id) { // TODO Auto-generated method stub return (Student)getSqlSession().selectOne("com.mybatis.student.queryStudentById",id); } public Map getAllStudentAfterupdate(Map map) { // TODO Auto-generated method stub getSqlSession().selectOne("com.mybatis.student.getAllStudentAfterupdate",map); return map; } public Classes selectClassAndStudentListById(int id) { // TODO Auto-generated method stub return (Classes)getSqlSession().selectOne("com.mybatis.classes.selectClassAndStudentListById",id); } public List selectStudentAndClassBySname(String sname) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.selectStudentAndClassBySname",sname); return stuList; } public List selectStudentByDynamicSql(Student student) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSql",student); } public List selectStudentByDynamicSqlChoose(Student student) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student); } public List selectStudentByIdArray(Integer[] idArry) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByIdArray",idArry); } public List selectStudentByIds(ArrayList ids) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectStudentByIds",ids); } public List studentResultMap(String sname) { // TODO Auto-generated method stub return getSqlSession().selectList("com.mybatis.student.selectMapResult",sname); } public List queryStudentByName(Student name) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=getSqlSession().selectList("com.mybatis.student.queryStudentByName","%"+name+"%"); return stuList; } public int updStudentById(Student student) { return getSqlSession().update("com.mybatis.student.addStudentBySequence", student); } }
IStudentDAOImpl_sqlSessionTemplate.java
package com.mybatis.student; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.mybatis.classes.Classes; /** * @author Administrator * 如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession * 可以只注入sqlSessionFactory 然后 像mybatis 中使用 sqlSession 一样 openSession() .close() * 否则 可以 继承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") * 和 SqlSessionTemplate 得到 spring 管理的 线程安全的 sqlSession * 或者 简单的使用XML中配置 MapperFactoryBean 这样就省去了我们 获取sqlSession了 */ public class IStudentDAOImpl_sqlSessionTemplate extends SqlSessionTemplate implements IStudentDAO { //同样是 创建一个 可以批量操作的 sqlSession public IStudentDAOImpl_sqlSessionTemplate( SqlSessionFactory sqlSessionFactory) { super(sqlSessionFactory); // TODO Auto-generated constructor stub } public int addStudent(Student student) { // TODO Auto-generated method stub return this.insert("com.mybatis.student.addStudent", student); } public int addStudentBySequence(Student student) { // TODO Auto-generated method stub return this.insert("com.mybatis.student.addStudentBySequence", student); } public int delStudentById(int id) { int rows=this.delete("com.mybatis.student.delStudentById", id); System.out.println(rows); return rows; } public ListqueryAllStudent() { List stuList=new ArrayList (); stuList=this.selectList("com.mybatis.student.queryAllStudent"); return stuList; } public Student queryStudentById(int id) { // TODO Auto-generated method stub return (Student)this.selectOne("com.mybatis.student.queryStudentById",id); } public Map getAllStudentAfterupdate(Map map) { // TODO Auto-generated method stub this.selectOne("com.mybatis.student.getAllStudentAfterupdate",map); return map; } public Classes selectClassAndStudentListById(int id) { // TODO Auto-generated method stub return (Classes)this.selectOne("com.mybatis.classes.selectClassAndStudentListById",id); } public List selectStudentAndClassBySname(String sname) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=this.selectList("com.mybatis.student.selectStudentAndClassBySname",sname); return stuList; } public List selectStudentByDynamicSql(Student student) { // TODO Auto-generated method stub return this.selectList("com.mybatis.student.selectStudentByDynamicSql",student); } public List selectStudentByDynamicSqlChoose(Student student) { // TODO Auto-generated method stub return this.selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student); } public List selectStudentByIdArray(Integer[] idArry) { // TODO Auto-generated method stub return this.selectList("com.mybatis.student.selectStudentByIdArray",idArry); } public List selectStudentByIds(ArrayList ids) { // TODO Auto-generated method stub return this.selectList("com.mybatis.student.selectStudentByIds",ids); } public List studentResultMap(String sname) { // TODO Auto-generated method stub return this.selectList("com.mybatis.student.selectMapResult",sname); } public List queryStudentByName(Student name) { // TODO Auto-generated method stub List stuList=new ArrayList (); stuList=this.selectList("com.mybatis.student.queryStudentByName","%"+name+"%"); return stuList; } public int updStudentById(Student student) { return this.update("com.mybatis.student.addStudentBySequence", student); } }
IClassesDAO.java
package com.mybatis.classes; /** * 使用 MapperFactoryBean 来管理 sqlSession * @author Administrator * */ public interface IClassesDAO { // 也可以在这里使用注解配置 sql语句 这样就不用写 映射文件了 Classes selectClassAndStudentListById(int cid); int delClassesBycid(int cid); }
Student.java
package com.mybatis.student; import java.io.Serializable; import java.util.Date; import com.mybatis.classes.Classes; public class Student implements Serializable { private int sid; private String sname; private String major; private Date birth; private float score; private int cid; private int status; //get set() }
Classes.java
package com.mybatis.classes; import java.sql.Date; import java.util.List; import com.mybatis.student.Student; public class Classes { private int cid; private String cname; private String teacher; private Date createDate; private Liststudents; //get set }
BaseAction.java
package com.mybatis.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.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.mybatis.action; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.mybatis.common.BaseAction; import com.mybatis.student.IStudentService; import com.mybatis.student.Student; public class StudentAction extends BaseAction { private Student student; private ListstuList; public String getAllStudent(){ try { IStudentService stuService=(IStudentService)this.getServiceBean("studentService"); stuList=stuService.queryAllStudent(); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public String delStudentById(){ try { HttpServletRequest request=this.getRequest(); IStudentService stuService=(IStudentService)this.getServiceBean("studentService"); int rows=stuService.delStudentById(student.getSid()); if(rows>0){ request.setAttribute("msg", ""); }else{ request.setAttribute("msg", ""); } } catch (Exception e) { e.printStackTrace(); } return getAllStudent(); } public String queryStudentById(){ try { IStudentService stuService=(IStudentService)this.getServiceBean("studentService"); student=stuService.queryStudentById(student.getSid()); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public String getAllStudentAfterupdate(){ try { Map map=new HashMap(); map.put("sid", student.getSid()); map.put("sname", student.getSname()); IStudentService stuService=(IStudentService)this.getServiceBean("studentService"); map=stuService.getAllStudentAfterupdate(map); stuList=(List )map.get("studentList"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public List getStuList() { return stuList; } public void setStuList(List stuList) { this.stuList = stuList; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
ClassesAction.java
package com.mybatis.action; import javax.servlet.http.HttpServletRequest; import com.mybatis.classes.Classes; import com.mybatis.classes.IClassesService; import com.mybatis.common.BaseAction; public class ClassesAction extends BaseAction { private Classes classes; /** * 根据 id 删除 * @return */ public String delClassesById(){ try { HttpServletRequest request=this.getRequest(); IClassesService classesService=(IClassesService)this.getServiceBean("classesService"); int rows=classesService.delClassesBycid(classes.getCid()); if(rows>0){ request.setAttribute("msg", ""); }else{ request.setAttribute("msg", ""); } } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } /** * 测试 右连接查询 会查询出 班级中拥有的学生 * @return */ public String queryClassesById(){ try { IClassesService classesService=(IClassesService)this.getServiceBean("classesService"); classes=classesService.selectClassAndStudentListById(classes.getCid()); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>MyBatis3.11 + Spring3.1.2 + Struts2
sid | sname | major | birth | score | 操作 |
${student.sid} | ${student.sname} | ${student.major} | ${student.score} |
index1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>MyBatis3.11 + Spring3.1.2 + Struts2
cid | cname | teacher | crateDate | 操作 |
${classes.cid} | ${classes.cname} | ${classes.teacher} | ${classes.createDate} | |
sid | sname | birth | score | |
${student.sid} | ${student.sname} | ${student.score} |