最近吴菜鸟在学习mybatis在网上搜了一下,大多要么是mybatis文档,要么是SpringMVC+MYBATIS整合的实例。因为吴菜鸟想一步一步的学习,但是始终找不到一个纯mybatis的实例。所以就结合公司数据库自己写了一个简单的多表联接查询,学习中。。也供像我这样的菜鸟参考。因为之前没有什么基础所以费了点劲。其中还有不明白的地方请高手指教。
开发环境:Eclipse Mars Release (4.5.0)
JDK 1.7.0
tomcat 8.0
maven 3.3.3
mybatis 3.3.0
首先在eclipse中java-compiler中JDK版本设为1.7,然后项目properties-java build path中JRE的版本也应是1.7,maven-project facets中对应dynamic web module为3.1 java 为1.7
此外在pom.xml中加上
WEB.XML头文件应该为
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"/>
这样在eclipse中使用MAVEN就不会报错了。
项目整体目录
3.创建实体类ZhiZao.java
package com.wu.pojo;
public class ZhiZao {
private String ddate; //单据下达日期
private String billno; //生产任务单
private int jsqty; // 计划生产数量
private String jdate;//计划完工日期
private int sqty; //实作数量
private String wuliao; //物料
private String date; //日期
private int scqty; //期间生产数量
private int hgqty; //合格数量
private int fxqty; //返修数量
public String getDdate() {
return ddate;
}
public void setDdate(String ddate) {
this.ddate = ddate;
}
public String getBillno() {
return billno;
}
public void setBillno(String billno) {
this.billno = billno;
}
public int getJsqty() {
return jsqty;
}
public void setJsqty(int jsqty) {
this.jsqty = jsqty;
}
public String getJdate() {
return jdate;
}
public void setJdate(String jdate) {
this.jdate = jdate;
}
public int getSqty() {
return sqty;
}
public void setSqty(int sqty) {
this.sqty = sqty;
}
public String getWuliao() {
return wuliao;
}
public void setWuliao(String wuliao) {
this.wuliao = wuliao;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getScqty() {
return scqty;
}
public void setScqty(int scqty) {
this.scqty = scqty;
}
public int getHgqty() {
return hgqty;
}
public void setHgqty(int hgqty) {
this.hgqty = hgqty;
}
public int getFxqty() {
return fxqty;
}
public void setFxqty(int fxqty) {
this.fxqty = fxqty;
}
public ZhiZao() {
super();
} //必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,在查询数据库时,将不能反射构造出ZhiZao实例
}
package com.wu.dao;
import java.util.List;
import java.util.Map;
import com.wu.pojo.ZhiZao;
public interface ZhiZaoMapper {
public List
}
select CONVERT(varchar(10),t4.fcommitdate,120) as ddate,
t2.FICMOBillNO as billno,
t4.FAuxQty as jsqty,
CONVERT(varchar(10),t4.FPlanFinishDate,120) as jdate,
t4.FAuxQtyFinish as sqty,t3.FName as wuliao,
t1.FCheckDate as date,
t2.FAuxQtyfinish as scqty,
t2.FAuxQtyPass as hgqty,
t2.FAuxReprocessedQty as fxqty
from SHProcRpt t2 left join SHProcRptMain t1 on t2.FinterID=t1.FInterID
left join t_ICItem t3 on t2.FItemID=t3.FItemID
inner join ICMO t4 on t4.FBillNo=t2.FICMOBillNO
where t1.FCheckDate >=#{date1} and t1.FCheckDate <= #{date2} and (t2.FOperID=40015 or t2.FOperID=40002)
注:这里需要注意的是XML文件中的<和>号的处理,有两种方法
第一种方法:
因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用符号进行说明,将此类符号不进行解析 。 mapper文件示例代码
第二种方法:
用了转义字符把>和<替换掉。 mapper文件示例代码:
附:转义字符表
< < 小于号
> > 大于号
& & 和
' ‘ 单引号
" " 双引号
"http://mybatis.org/dtd/mybatis-3-config.dtd">
package com.wu.dbcon;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class DBcon {
public static SqlSession getsqlsession() throws IOException{
//通过配置文件获取数据库连接信息
Reader reader = Resources.getResourceAsReader("conf.xml");
//通过配置信息构建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//通过SqlSessionFactory打开一个数据库会话
SqlSession sqlsession = sqlSessionFactory.openSession();
return sqlsession;
}
}
package com.wu.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import com.wu.dao.ZhiZaoMapper;
import com.wu.dbcon.DBcon;
import com.wu.pojo.ZhiZao;
@WebServlet("/serch")
public class serch extends HttpServlet {
private static final long serialVersionUID = 1L;
public serch() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date stadate = new Date();
Date enddate = new Date();
try {
stadate=df.parse((String)request.getParameter("date1"));
enddate=df.parse((String)request.getParameter("date1"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
String stadate=request.getParameter("date1");
String enddate=request.getParameter("date2");
Map
map.put("date1", stadate);
map.put("date2", enddate);
System.out.println("值"+stadate);
SqlSession sqlsession = null;
sqlsession=DBcon.getsqlsession();
ZhiZaoMapper zm = sqlsession.getMapper(ZhiZaoMapper.class);
List
request.setAttribute("list",list);//将查询结果保存在attribute list中
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
<%@page import="com.wu.pojo.ZhiZao" import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
单据下达日期
生产任务单
计划生产数量
计划完工日期
实作数量
物料
日期
期间生产数量
合格数量
返修数量
<% List
if(list==null||list.size()<1){
out.print("没有数据");
}else{
for(ZhiZao zz:list){
%><%= zz.getDdate() %> <%=zz.getBillno() %> <%=zz.getJsqty() %> <%=zz.getJdate() %> <%=zz.getSqty() %> <%=zz.getWuliao() %> <%=zz.getDate() %> <%=zz.getScqty() %> <%=zz.getHgqty() %> <%=zz.getFxqty() %>
<%
}
}
%>
运行结果: