Java多条件模糊查询jsp+servlet实现
根据多个条件进⾏模糊查询
思路:判断前端传来的数据,判断那些条件是填写的,最后在Dao层判断进⾏sql语句拼接
效果
具体代码实现
前端
<div ><h3 align="center"class="form-inline">当前⽤户:${user}欢迎你!</h3></div>
<div align="center" >
<form class="form-inline" action="bookListByType" method="post">
<div class="form-group">
<label>图书分类</label>
<select name="book_type"class="form-control">
<option selected>请选择</option>
<option>⼩说</option>
<option>⽂学</option>
<option>传记</option>
<option>艺术</option>
<option>少⼈</option>
<option>经济</option>
<option>管理</option>
<option>科技</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail2">图书名称</label>
<input type="text"class="form-control" id="exampleInputEmail2" placeholder="请输⼊图书名称" name="book_name"> </div>
<div class="form-group">
<label>是否借阅</label>
<select name="is_borrow"class="form-control">
<option selected>请选择</option>
<option>已借</option>
<option>未借</option>
</select>
</div>
<input type="submit"class="btn btn-default" value="点击查询"/>
</form>
</div>
<table class="table table-hover">
<tr align="center">
<td>ID</td>
<td>图书编号</td>
<td>图书名称</td>
<td>图书类型</td>
<td>图书作者</td>
<td>出版社</td>
<td>出版⽇期</td>
<td>是否借阅</td>
<td>创建⼈</td>
<td>创建⽇期</td>
<td>最新更新时间</td>
<td>借阅⽤户ID</td>
<td colspan="2">操作</td>
</tr>
</tr>
<c:forEach var="item" items="${bookInfos}">
<tr align="center">
<td>${item.book_id}</td>
<td>${item.book_code}</td>
<td>${item.book_name}</td>
<td><c:choose>
<c:when test="${item.book_type==1}">⼩说</c:when>
<c:when test="${item.book_type==2}">⽂学</c:when>
<c:when test="${item.book_type==3}">传记</c:when>
<c:when test="${item.book_type==5}">艺术</c:when>
<c:when test="${item.book_type==6}">少⼉</c:when>
<c:when test="${item.book_type==7}">经济</c:when>
<c:when test="${item.book_type==8}">管理</c:when>
<c:when test="${item.book_type==8}">科技</c:when>
</c:choose>
</td>
<td>${item.book_author}</td>
<td>${item.publish_press}</td>
<td>${item.publish_date}</td>
<td>
<c:choose>
<c:when test="${item.is_borrow==1}">已借</c:when>
<c:when test="${item.is_borrow==0}">未借</c:when>
</c:choose>
</td>
<td>${atedBy}</td>
<td>${ation_time}</td>
<td>${item.last_updatetime}</td>
javaservlet和jsp的比较<td>${item.user_id}</td>
<td><a href="${tPath}/bookInfoListById?id=${item.book_id}">修改</a></td> <td><a href="${tPath}/deleteBookInfoById?id=${item.book_id}">删除</a></td> </tr>
</c:forEach>
</table>
servlet
package com.library.servlet;
import com.library.pojo.BookInfo;
import com.library.service.BookInfoService;
import com.library.service.impl.BookInfoServiceImpl;
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 java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/bookListByType")
public class BookListByType extends HttpServlet {
BookInfoService bookInfoService =new BookInfoServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8");
resp.setContentType("utf-8");
String book_name ="未选择";
Map<String, Integer> map =new HashMap<>();
map.put("请选择",0);
map.put("⼩说",1);
map.put("⽂学",2);
map.put("传记",3);
map.put("艺术",4);
map.put("少⼉",5);
map.put("经济",6);
map.put("管理",7);
map.put("科技",8);
int book_type = (Parameter("book_type"));
String s1 = Parameter("book_name");
if(!"".equals(s1)){
book_name = Parameter("book_name").trim();
}
Map<String, Integer> map1 =new HashMap<>();
map1.put("请选择",-1);
map1.put("已借",1);
map1.put("未借",0);
int is_borrow = (Parameter("is_borrow"));
List<BookInfo> bookInfos = bookInfoService.BookListByType(book_type, book_name, is_borrow);
req.setAttribute("bookInfos", bookInfos);
}
}
⼯具类Util
package com.library.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import flect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class Utils {
private static DataSource dataSource;
// 初始化配置
static{
try{
Properties properties =new Properties();
properties.load(ClassLoader().getResourceAsStream("druid.properties")); dataSource = ateDataSource(properties);
}catch(Exception e){
e.printStackTrace();
}
}
// 获取连接
public static Connection getConnection()throws SQLException {
Connection();
}
// 释放资源
public static void close(ResultSet resultSet, Statement statement, Connection connection){ if(resultSet != null){
try{
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(connection != null){
try{
connection.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
// 通⽤查询参数⽤Object数组存储
public static<T> List<T>executeQuery(String sql, Object[] params, Class<T> clazz){
List<T> list =new ArrayList<>();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try{
connection =getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params != null){
for(int i =0; i < params.length; i++){
preparedStatement.setObject(i +1, params[i]);
}
}
resultSet = uteQuery();
()){
T t = wInstance();
Field[] declaredFields = DeclaredFields();
for(int i =0; i < declaredFields.length; i++){
declaredFields[i].setAccessible(true);
declaredFields[i].set(t, Object(declaredFields[i].getName()));
}
list.add(t);
}
}catch(SQLException | InstantiationException | IllegalAccessException e){
e.printStackTrace();
}finally{
close(resultSet, preparedStatement, connection);
}
return list;
}
// 通⽤查询参数⽤ArrayList集合存储
public static<T> List<T>executeListQuery(String sql, ArrayList params, Class<T> clazz){ List<T> list =new ArrayList<>();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try{
connection =getConnection();
preparedStatement = connection.prepareStatement(sql);
if(!params.isEmpty()){
for(int i =0; i < params.size(); i++){
preparedStatement.setObject(i +1, (i));
}
}
resultSet = uteQuery();
()){
T t = wInstance();
Field[] declaredFields = DeclaredFields();
for(int i =0; i < declaredFields.length; i++){
declaredFields[i].setAccessible(true);
declaredFields[i].set(t, Object(declaredFields[i].getName()));
}
list.add(t);
}
}catch(SQLException | InstantiationException | IllegalAccessException e){
e.printStackTrace();
}finally{
close(resultSet, preparedStatement, connection);
}
return list;
}
// 通⽤增删改
public static int executeUpdate(String sql, Object[] params){
PreparedStatement preparedStatement = null;
Connection connection = null;
int num =-1;
try{
connection =getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params != null){
for(int i =0; i < params.length; i++){
preparedStatement.setObject(i +1, params[i]);
}
}
num = uteUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
close(null, preparedStatement, connection);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论