java如何实现异步模糊查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何实现异步模糊查询相关的知识,希望对你有一定的参考价值。
我要填另一个表里的名字 可是名字不好记 写成一个下拉菜单数据又有点多 人不可能记得那么清 所以必须异步模糊查询 要做到就和百度搜索栏一样的效果 把模糊查询出的结果显示在下面、、像myeclipse alt+/的显示效果也行
用ajax做,进入页面的时候吧表名放在集合里,在搜索text框发生改变的时候就调用ajax访问后台,获取集合里面有包含value的集合,然后回到页面打印在text下面。打印成一个ul就行了,记得进入页面的时候就获取表名的集合,不用每次调用ajax都去访问一次数据库。然后当用户点击某个li再去访问数据库。追问真是懂我。。我还愁数据太多每次交互的时候延迟太大呢 如果进去的时候就加载速度就会快很多 之后呢 每次把输入的数据传到后台 再循环匹配把符合条件的对象分成一个新的集合传到前台?我AJAX学的不是很好、、是用onchange么
追答对,每次匹配集合做成一个新的集合。
是用onchange事件
如你现在有
a="你好吗?";
b="你在哪里?";
c="你过的好不好?";
你输入
你 三个选项都可以选
再输入
你好 只剩下a可以选
再输入
你在 只剩下b可以选
你想要的是这样吗???
要的话输入框 jTextField 要实做
jTextField.addFocusListener(new java.awt.event.FocusListener()
@Override
public void focusGained(FocusEvent e)
@Override
public void focusLost(FocusEvent e)
);
Springmvc+Myabtis+Ajax实现异步分页emp+dept(全部查询及模糊查询)
1.在项目中创建如下目录
2.创建实体类Dept
package com.entity; import java.io.Serializable; /** * 部门表 * @author Administrator * */ public class Dept implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer deptno; private String dname; private String loc; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } public Dept() { super(); } public Dept(Integer deptno, String dname, String loc) { super(); this.deptno = deptno; this.dname = dname; this.loc = loc; } }
3。创建实体类Emp
package com.entity; import java.io.Serializable; /** * 员工表 * @author Administrator * */ public class Emp implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer empno; private String ename; private Double sal; /*多对一*/ private Dept dept; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Emp(Integer empno, String ename, Double sal, Dept dept) { super(); this.empno = empno; this.ename = ename; this.sal = sal; this.dept = dept; } public Emp() { super(); } }
4.封装page类用于放置分页查询的参数
package com.page; public class Page { private Integer pageno;//当前页 private Integer pagesize;//页面大小 private Integer totalcount;//总条数 private Integer totalpage;//总页数 private Integer startrownum;//起始页 private Integer endrownum;//结束页 private Integer deptno;//部门编号 public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public Integer getPageno() { return pageno; } public void setPageno(Integer pageno) { this.pageno = pageno; } public Integer getPagesize() { return pagesize; } public void setPagesize(Integer pagesize) { this.pagesize = pagesize; } public Integer getTotalcount() { return totalcount; } public void setTotalcount(Integer totalcount) { this.totalcount = totalcount; } public Integer getTotalpage() { return totalpage; } public void setTotalpage(Integer totalcount) { this.totalpage = totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1;; } public Integer getStartrownum() { return startrownum; } public void setStartrownum(Integer startrownum) { this.startrownum = startrownum; } public Integer getEndrownum() { return endrownum; } public void setEndrownum(Integer endrownum) { this.endrownum = endrownum; } public Page() { } /** * 传入分页的条件页 * @param pageno * @param pagesize */ public Page(Integer pageno, Integer pagesize,Integer totalcount) { this.pageno = pageno; this.pagesize = pagesize; this.totalcount = totalcount; setTotalpage(totalcount); this.startrownum = (pageno-1)*pagesize; this.endrownum = pageno*pagesize; } /** * 模糊查询分页 * @param pageno * @param pagesize * @param totalcount * @param deptno */ public Page(Integer pageno, Integer pagesize, Integer totalcount, Integer deptno) { this(pageno, pagesize, totalcount); this.deptno = deptno; } @Override public String toString() { return "Page [pageno=" + pageno + ", pagesize=" + pagesize + ", totalcount=" + totalcount + ", totalpage=" + totalpage + ", startrownum=" + startrownum + ", endrownum=" + endrownum + "]"; } }
5.创建emp表的mapper接口
package com.mapper; import java.util.List; import com.entity.Emp; import com.page.Page; /** * 员工映射接口 * @author sam * */ public interface EmpMapper { /** * 查询所有员工信息 * @return */ List<Emp> findEmp(); /** * 根据部门编号查询某个部门下的所有员工 * @return */ List<Emp> findByDeptAndEmp(Integer deptno); /** * 分页查询 * @param page * @return */ List<Emp> findpage(Page page); /** * 模糊分页 * @param page * @return */ List<Emp> findpagelike(Page page); }
6.创建dept表的mapper接口
package com.mapper; import java.util.List; import com.entity.Dept; /** * 部门映射接口 * @author sam * */ public interface DeptMapper { /** * 查询所有部门信息 * @return */ List<Dept> findDept(); }
7.编写emp的映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mapper.EmpMapper"> <!-- 查询所有员工信息 --> <select id="findEmp" resultMap="findEmpAndDept"> select * from emp e inner join dept d on e.deptno=d.deptno </select> <!-- 根据部门编号查询某个部门下的所有员工 --> <select id="findByDeptAndEmp" resultMap="findEmpAndDept" parameterType="Integer"> select * from emp e inner join dept d on e.deptno=d.deptno where e.deptno=#{deptno} </select> <!-- 分页 --> <select id="findpage" parameterType="com.page.Page" resultMap="findEmpAndDept"> <![CDATA[ select * from (select rownum r,e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno and rownum<=#{endrownum}) where r>#{startrownum} ]]> </select> <select id="findpagelike" parameterType="com.page.Page" resultMap="findEmpAndDept"> <![CDATA[ select * from (select rownum r,e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno and rownum<=#{endrownum} and d.deptno=#{deptno}) where r>#{startrownum} ]]> </select> <!-- 配置查询的结果集 --> <resultMap type="com.entity.Emp" id="findEmpAndDept"> <id property="empno" column="EMPNO" javaType="java.lang.Integer" /> <result property="ename" column="ENAME" javaType="java.lang.String" /> <result property="sal" column="SAL" javaType="java.lang.Double" /> <!-- 配置外键的结果集 --> <association property="dept" javaType="com.entity.Dept" column="DEPTNO"> <id property="deptno" column="DEPTNO" javaType="java.lang.Integer" /> <result property="dname" column="DNAME" javaType="java.lang.String" /> <result property="loc" column="LOC" javaType="java.lang.String" /> </association> </resultMap> </mapper>
8.创建dept的映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mapper.DeptMapper"> <!-- 查询所有部门信息 --> <select id="findDept" resultType="com.entity.Dept"> select deptno,dname,loc from dept </select> </mapper>
9.创建emp的service接口
package com.service; import java.util.List; import com.entity.Emp; import com.page.Page; public interface EmpService { /** * 查询所有员工信息 * @return */ List<Emp> findEmp(); /** * 根据部门编号查询某个部门下的所有员工 * @return */ List<Emp> findByDeptnoEmp(Integer deptno); /** * 分页查询 * @param page * @return */ List<Emp> findpage(Integer pageno,Integer pagesize); /** * 模糊分页 * @param page * @return */ List<Emp> findpagelike(Page page); }
10.创建dept的service接口
package com.service; import java.util.List; import org.springframework.web.filter.CharacterEncodingFilter; import com.entity.Dept; public interface DeptService { /** * 查询所有部门信息 * @return */ List<Dept> findDept(); }
11.创建emp的service的实现类
package com.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.entity.Emp; import com.mapper.EmpMapper; import com.page.Page; import com.service.EmpService; @Service @Transactional public class EmpServiceImpl implements EmpService { @Autowired private EmpMapper empMapper; /** * 查询员工信息 */ public List<Emp> findEmp() { // TODO Auto-generated method stub return empMapper.findEmp(); } /** * 模糊查询查询 */ public List<Emp> findByDeptnoEmp(Integer deptno) { // TODO Auto-generated method stub return empMapper.findByDeptAndEmp(deptno); } //普通查询分页 public List<Emp> findpage(Integer pageno, Integer pagesize) { // TODO Auto-generated method stub int totalcount=empMapper.findEmp().size();//通过查询所有结果集(list集合)的size方法获取总条数 Page page=new Page(pageno, pagesize, totalcount); return empMapper.findpage(page); } //模糊查询分页 public List<Emp> findpagelike(Page page) { // TODO Auto-generated method stub return empMapper.findpagelike(page); } }
12.创建dept的service的实现类
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.entity.Dept; import com.mapper.DeptMapper; import com.service.DeptService; @Service @Transactional public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; /** * 查询部门信息 */ public List<Dept> findDept() { // TODO Auto-generated method stub return deptMapper.findDept(); } }
13.在WEB—INF下编写Springmvc的xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 1.支持注解 --> <mvc:annotation-driven/> <!-- 2.全局扫描包注解 --> <context:component-scan base-package="com"></context:component-scan> <!-- 3.驱动管理配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> <!-- 4.数据源管理事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 引入数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5.sqlsessionfactorybean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引入数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 引入myBatis映射文件 --> <property name="mapperLocations"> <value>classpath:com/mapper/*.xml</value> <!-- <list> <value>classpath:com/mapper/DeptMapper.xml</value> <value>classpath:com/mapper/EmpMapper.xml</value> </list> --> </property> </bean> <!-- 6.输入的映射的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 映射接口的路径 --> <property name="basePackage" value="com.mapper"></property> </bean> </beans >
14.创建控制类
package com.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.entity.Dept; import com.entity.Emp; import com.page.Page; import com.service.DeptService; import com.service.EmpService; import com.sun.faces.config.ConfigureListener.ServletContextAdapter; /** * 控制层 * @author Administrator * */ @Controller public class EmpAndDeptController { @Autowired private EmpService empService; @Autowired private DeptService deptService; /* @RequestMapping("/findEmpAndDept.do") public String findEmpAndDept(HttpServletRequest request,String pageNo){ int pageno=1; if (pageNo!=null) { pageno=Integer.parseInt(pageNo); } int pagesize=3; int totalcount=empService.findEmp().size(); Page page=new Page(pageno, pagesize, totalcount); List<Emp> emplList=empService.findpage(pageno, pagesize); List<Dept> deptlist=deptService.findDept(); if (emplList!=null && deptlist!=null) { for (Emp emp : emplList) { System.out.println(emp); } for (Dept dept : deptlist) { System.out.println(dept); } request.setAttribute("emplList", emplList); request.setAttribute("page", page); request.getSession().setAttribute("deptlist", deptlist); }else{ System.out.println("没查到!"); } return "/index.jsp"; } *//** * 根据部门编号查询员工信息 * @param deptno * @param request * @return *//* @RequestMapping("findEmpByDeptno.do") public String findEmpByDeptno(String id,HttpServletRequest request){ System.out.println("前台获取的deptno是"+id); Integer deptno=null; if (id!=null) { deptno=Integer.parseInt(id); } List<Emp> emplist=new ArrayList<Emp>(); if (deptno!=null) { emplist=empService.findByDeptnoEmp(deptno); request.setAttribute("emplList", emplist); request.setAttribute("dno", deptno); } return "/index.jsp"; } */ /** * 分页查询的方法,根据前台的页面请求Ajax回调执行并返回结果 * @param request 请求 * @param response 响应 * @param pageNo 请求页面数 * @param id 请求的模糊查询条件 */ @RequestMapping("/fenyepage.do") public void fenyepage(HttpServletRequest request,HttpServletResponse response,String pageNo,String id){ /** * 得到传入查询方法的page的当前页数 */ int pageno=1; if (pageNo!=null) { pageno=Integer.parseInt(pageNo); } //页面大小 int pagesize=3; //总条数,根据请求不同查询不同 int totalcount=0; //传入查询方法的page对象 Page page=null; //返回结果集,但是由于查询条件分模糊还是所有,所以先创建对象 List<Emp> emplList=new ArrayList<Emp>(); System.out.println("从页面传过来的deptno值是:‘"+id); int deptno=0; //判断页面是否有模糊查询条件传过来 if (id==null || id.equals("")) { System.out.println("进入普通分页"); //获取普通查询总条数 totalcount=empService.findEmp().size(); //得到查询的page对象 page=new Page(pageno, pagesize, totalcount); //得到查询结果 emplList=empService.findpage(pageno, pagesize); } else { System.out.println("进入条件分页"); //得到模糊查询的具体条件 deptno=Integer.parseInt(id); //获取模糊查询的总条数 totalcount=empService.findByDeptnoEmp(deptno).size(); //得到查询的page对象 page=new Page(pageno, pagesize, totalcount, deptno); //得到模糊查询结果 emplList=empService.findpagelike(page); } System.out.println("获取的page是"+page); //获取下拉列表的结果集 List<Dept> dlist=deptService.findDept(); if (emplList!=null && dlist!=null) { for (Emp emp : emplList) { System.out.println(emp); } for (Dept dept : dlist) { System.out.println(dept); } try { //获取json文件的包装对象om,并将所有前台需要结果转换为json格式 ObjectMapper om=new ObjectMapper(); String emplist=om.writeValueAsString(emplList); String pageinfo=om.writeValueAsString(page); String deptlist=om.writeValueAsString(dlist); //获取总的json文件 emplist="{\\"emplist\\":"+emplist+",\\"pageinfo\\":"+pageinfo+",\\"deptlist\\":"+deptlist+",\\"ddno\\":"+deptno+"}"; System.out.println("list集合为"+emplist); PrintWriter out=response.getWriter(); //给请求的Ajax返回值 out.print(emplist); } catch (JsonGenerationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println("没查到!"); } } /* @RequestMapping("/fenyepage.do") public String fenyepage(HttpServletRequest request,String pageNo){ int pageno=1; if (pageNo!=null) { pageno=Integer.parseInt(pageNo); } int pagesize=3; int totalcount=empService.findEmp().size(); Page page=new Page(pageno, pagesize, totalcount); System.out.println("获取的page是"+page); List<Emp> emplList=empService.findpage(pageno, pagesize); List<Dept> deptlist=deptService.findDept(); if (emplList!=null && deptlist!=null) { for (Emp emp : emplList) { System.out.println(emp); } for (Dept dept : deptlist) { System.out.println(dept); } request.setAttribute("emplList", emplList); request.setAttribute("page", page); request.getSession().setAttribute("deptlist", deptlist); }else{ System.out.println("没查到!"); } return "/index.jsp"; } */ }
15.编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
16.,在WEB-INF下创建js文件夹,引入jQuery的库文件并编写自己的js文件
$(function(){ alert("进入ajax方法"); yibushuaxin(); test1(); mohu(); }); function yibushuaxin(pageNo,deptno){ $.ajax({ url:‘fenyepage.do‘, type:‘post‘, data:{‘pageNo‘:pageNo,"id":deptno}, dataType:‘json‘, success:function(obj){ alert("success"); $("table").html(""); $("form").html(""); var str="<tr><td>员工编号</td><td>员工姓名</td><td>" + "员工工资</td><td>部门编号</td><td>部门名称</td>" + "<td>部门地址</td><td>操作</td></tr>"; $.each(obj.emplist,function(i){ str+="<tr><td>"+obj.emplist[i].empno+"</td>"+ "<td>"+obj.emplist[i].ename+"</td>"+ "<td>"+obj.emplist[i].sal+"</td>"+ "<td>"+obj.emplist[i].dept.deptno+"</td>"+ "<td>"+obj.emplist[i].dept.dname+"</td>"+ "<td>"+obj.emplist[i].dept.loc+"</td>"+ "<td><a href=\\"javaScript:void(0)\\">修改</a><a href=\\"javaScript:void(0)\\">删除</a></td></tr>"; }); str+="<tr><td><input type=\\"hidden\\" id=‘pno‘ value=\\""+obj.pageinfo.pageno+"\\"></td>"+ "<td><input type=\\"hidden\\" id=‘topg‘ value=\\""+obj.pageinfo.totalpage+"\\"></td>"+ "<td><input type=\\"hidden\\" id=‘dno‘ value=\\""+obj.pageinfo.deptno+"\\"></td></tr>"; str+="<tr>&l
以上是关于java如何实现异步模糊查询的主要内容,如果未能解决你的问题,请参考以下文章