各位大佬,本人自学小白求教
现在已得到
String[] interest = req.getParameterValues("interest");
for (int i = 0; i < interest.length; i++) {
System.out.println(interest[i]);
}
String date1 = req.getParameter("date1");
String date2 = req.getParameter("date2");
两个请求,然后怎么通过得到的请求传送给mapper.xml 进行sql搜索
sql语句是
SELECT
ch.pin,
ch.checktime,
us.badgenumber,
us.name,
us.defaultdeptid
FROM
checkinout ch
LEFT JOIN
userinfo us on ch.pin=us.badgenumber
WHERE
ch.checktime between '2019/04/01' AND '2019/04/30'
AND
us.defaultdeptid IN (3,4);
楼上的答案都是依赖spring或者springMVC,springboot等框架的,楼主可能只是单纯原生MVC,servlet+mybatis
1.引入jjar包
mybatis-3.4.6.jar;
mysql-connector-java-5.1.21.jar
2.src根目录配置mybatis配置文件mybatis-config.xml,重点是mappers这个标签很关键,具体自行查询
3.写对应的mapper类和mapper.xml,名字最好一一对应,减少很多烦恼,xml中记得写对应的namespace,
4.参考资料:https://blog.csdn.net/heyanfeng22/article/details/80679400
5.手打不易,望采纳
首先在controller层接收到前端传过来的参数,如a,b两个参数,在你的mapper.xml中定义的查询sql应该是和代码中的mapper接口对应的,
在mapper接口中的查询方法中定义参数,如select(@Param("a") String a,@Param("b") Date b);,这个select方法映射到mapper.xml中的
sql,在sql中需要传参数的地方用#{参数名}这种格式接收,如:
select * from table1 where a=#{a} and b=#{b}
希望可以帮到你
方式一、见楼上答案;
方式二、如果参数可以构成一个关联对象,最好封装成对象,方便扩展,以对象方式传入mapper接口中,然后在xml里
同方式一,传参数的地方用#{参数名}这种格式接收,如:
select * from table1 where a=#{a} and b=#{b}
如果满意请采纳,谢谢
@ResponseBody
@Controller
@RequestMapping("selectController")
public class SelectController {
@Resource
private SelectService selectService;
@PostMapping(value = "/selectData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<CheckinoutEntiry> selectData(HttpServletRequest request) {
String date1 = request.getParameter("date1");
String date2 = request.getParameter("date2");
List<CheckinoutEntiry> list = selectService.selectData(date1,date2);
return list;
}
}
//SelectService接口
public interface SelectService {
public List<CheckinoutEntiry> selectData(String date1,String date2);
}
//SelectService接口实现类
@Service("selectService")
public class SelectServiceImpl implements SelectService {
@Resource
private SelectServiceDao selectServiceDao;
public List<CheckinoutEntiry> selectData(String date1,String date2){
List<CheckinoutEntiry> list = selectServiceDao.selectData(date1,date2);
return list;
}
}
//SelectServiceDao接口
@Repository
public interface SelectServiceDao {
public List<CheckinoutEntiry> selectData(@Param("date1") String date1,@Param("date2") String date2);
}
//mapper
<select id="selectData" resultType="CheckinoutEntiry" parameterType="string">
//这里就写你的sql,例如
ch.checktime between #{date1} AND #{date2}
</select>
//CheckinoutEntiry实体类
public class CheckinoutEntiry实体类 {
//属性字段
//get set
}