首页 > 代码库 > 通过自动回复机器人学Mybatis:MySQL脚本 + db>>dao>>service>>servlet
通过自动回复机器人学Mybatis:MySQL脚本 + db>>dao>>service>>servlet
留着参考
makeData.sql
delimiter //create procedure make_data() begin declare i int default 1; while i < 1000 do insert into message values(i, ‘a‘, ‘b‘, ‘c‘); set i = i + 1; end while;end;//call make_data();DROP PROCEDURE IF EXISTS make_data;
Message.xml
<?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="Message"> <resultMap type="com.imooc.bean.Message" id="MessageResult"> <!-- 主键用id标签,其它的用result标签 --> <id column="id" jdbcType="INTEGER" property="id"/> <result column="COMMAND" jdbcType="VARCHAR" property="command"/> <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/> <result column="CONTENT" jdbcType="VARCHAR" property="content"/> </resultMap> <!-- Message.xml的目的:配置如下的sql语句让SqlSession读到并执行 --> <!-- id是为了方便sqlSession调用,相同namespace下必须唯一 --> <select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1 <!-- OGNL表达式,它不是Mybatis专有的,类似于EL表达式,#{}是Mybatis专有的 --> <!-- 原: <if test="command != null && !"".equals(command.trim())"></if> --> <if test="command != null and !"".equals(command.trim())"> and COMMAND=#{command}</if> <if test="description != null and !"".equals(description.trim())"> and DESCRIPTION like ‘%‘ #{description} ‘%‘</if> </select> <delete id="deleteOne" parameterType="int"> delete from MESSAGE where ID = #{_parameter} </delete> <delete id="deleteBatch" parameterType="java.util.List"> delete from MESSAGE where ID in ( <foreach collection="list" item="item" separator=","> #{item} </foreach> ) </delete> </mapper>
访问数据库类
package com.imooc.db;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 DBAccess { public SqlSession getSqlSession() throws IOException { Reader reader = Resources.getResourceAsReader("com/imooc/config/Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; }}
dao
package com.imooc.dao;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.log4j.Logger;import com.imooc.bean.Message;import com.imooc.db.DBAccess;public class MessageDao { /** * 根据查询条件获取消息列表 */ public List<Message> queryMessageList(String command, String description) { List<Message> messageList = new ArrayList<>(); DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); // 通过sqlSession执行SQL语句 Message message = new Message(); message.setCommand(command); message.setDescription(description); messageList = sqlSession.selectList("Message.queryMessageList", message); } catch (IOException e) { e.printStackTrace(); } finally { // 如果中间发生异常sqlSession可能是null if (sqlSession != null) { sqlSession.close(); } } return messageList; } /** * 单条删除 */ public void deletOne(int id) { DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); sqlSession.delete("Message.deleteOne", id); sqlSession.commit(); // Mybatis不默认提交 // JDBC默认自动提交,除非setAutoCommit(false) // 这个时候姐需要conn.commit()提交事务 // 可以通过conn.rollback([Savepoint savepoint])回滚 } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } } /** * 删除多条消息 */ public void deleteBatch(List<Integer> ids) { DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); sqlSession.delete("Message.deleteBatch", ids); sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } } public static void main(String[] args) { MessageDao messageDao = new MessageDao(); messageDao.queryMessageList("", ""); }}
service
MaintainService.java
package com.imooc.service;import java.util.ArrayList;import java.util.List;import com.imooc.dao.MessageDao;/** * 维护相关的业务功能 */public class MaintainService { /** * 删除单条信息 */ public void deleteOne(String id) { // servlet负责接收页面的值、向页面传值 // service负责接收servlet穿过来的值并对这个值进行处理,做业务的操作,算法等等 // 如果有需要调用相应的Dao if (id != null && !"".equals(id.trim())) { // 这个逻辑判断实际还是不够的! MessageDao messageDao = new MessageDao(); messageDao.deletOne(Integer.valueOf(id)); } } /** * 删除多条消息 */ public void deleteBatch(String[] ids) { MessageDao messageDao = new MessageDao(); List<Integer> idList = new ArrayList<>(); for (String id : ids) { idList.add(Integer.valueOf(id)); } messageDao.deleteBatch(idList); }}
QueryService.java
package com.imooc.service;import java.util.List;import com.imooc.bean.Message;import com.imooc.dao.MessageDao;import com.imooc.util.Iconst;/** * 列表相关的业务功能 */public class QueryService { /** * 获取消息列表 */ public List<Message> queryMessageList(String command, String description) { MessageDao dao = new MessageDao(); return dao.queryMessageList(command, description); } /** * 通过指令查询 */ public String queryByCommand(String command) { MessageDao dao = new MessageDao(); List<Message> messageList; // 用户输入帮助 if (Iconst.HELP_COMMAND.equals(command)) { messageList = dao.queryMessageList(null, null); StringBuilder result = new StringBuilder(); for (int i = 0; i < messageList.size(); ++i) { if (i != 0) { result.append("<br />"); } result.append("回复[" + messageList.get(i).getContent() + "]可以查看" + messageList.get(i).getDescription()); } return result.toString(); } // 用户输入具体指令 messageList = dao.queryMessageList(command, null); if (messageList.size() > 0) { return messageList.get(0).getContent(); } // 用户输入位置指令 return Iconst.NO_MATCHING_CONTENT; }}
servlet
后台方面:
package com.imooc.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.imooc.service.QueryService;/** * 列表页面初始化控制 */@SuppressWarnings("serial")public class ListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置编码 req.setCharacterEncoding("UTF-8"); // 接受页面的值 String command = req.getParameter("command"); String description = req.getParameter("description"); // 向页面传值 req.setAttribute("command", command); req.setAttribute("description", description); // 查询消息列表并传给页面 QueryService listService = new QueryService(); req.setAttribute("messageList", listService.queryMessageList(command, description)); // 页面跳转 req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
package com.imooc.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.imooc.service.MaintainService;/** * 单条删除控制层 */@SuppressWarnings("serial")public class DeleteOneServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置编码 req.setCharacterEncoding("UTF-8"); // 接受页面的值 String id = req.getParameter("id"); MaintainService maintainService = new MaintainService(); maintainService.deleteOne(id); // 页面跳转 req.getRequestDispatcher("/List.action").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
package com.imooc.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.imooc.service.MaintainService;@SuppressWarnings("serial")public class DeleteBatchServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置编码 req.setCharacterEncoding("UTF-8"); // 接受页面的值 String[] ids = req.getParameterValues("id"); MaintainService maintainService = new MaintainService(); maintainService.deleteBatch(ids); // 页面跳转 req.getRequestDispatcher("/List.action").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
前台方面:
package com.imooc.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * * 对话页的初始化控制 * */@SuppressWarnings("serial")public class InitTalkServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置编码 req.setCharacterEncoding("UTF-8"); // 页面跳转 req.getRequestDispatcher("/WEB-INF/jsp/front/talk.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
package com.imooc.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.imooc.service.QueryService;/** * 自动回复功能控制层, * 针对AJAX的 */@SuppressWarnings("serial")public class AutoReplyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); QueryService queryService = new QueryService(); out.write(queryService.queryByCommand(req.getParameter("content"))); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
通过自动回复机器人学Mybatis:MySQL脚本 + db>>dao>>service>>servlet
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。