通过自动回复机器人学Mybatis:MySQL脚本 + db >> dao >> service >> servlet
留着参考


makeData.sql
delimiter //
create procedure make_data()
begin
declare i int default ;
while i < do
insert into message values(i, 'a', 'b', 'c');
set i = i + ;
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的更多相关文章
- 通过自动回复机器人学Mybatis:搭建核心架构
		
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 MessageDao.java package com.imooc.dao; impor ...
 - 通过自动回复机器人学Mybatis 笔记:接口式编程
		
[接口式编程]尚未遇见Spring --> 代码量反而增加 1.增加约定,减少犯错的可能(不用直接去写字符串 修改点1:命名空间 修改点2:增加接口,方法名与配置文件中的id对应 package ...
 - 通过自动回复机器人学Mybatis笔记:接口式编程
		
[接口式编程]尚未遇见Spring --> 代码量反而增加 1.增加约定,减少犯错的可能(不用直接去写字符串 修改点1:命名空间 修改点2:增加接口,方法名与配置文件中的id对应 package ...
 - 通过自动回复机器人学Mybatis:代码重构(分层)
		
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 ListServlet.java package com.imooc.servlet; ...
 - 通过自动回复机器人学Mybatis:OGNL+log4j.properties
		
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 OGNL规则: 从哪里取?(作用域.取值范围,例如封装入一个对象,该对象就是取值范围) ...
 - 通过自动回复机器人学Mybatis:原始版本(包括JDBC、pom.xml等)
		
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 list.jsp <%@ page contentType="text/ ...
 - Spring+Mybatis+MySql+Maven 简单的事务管理案例
		
利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ...
 - springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
		
@_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...
 - SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现
		
上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1. 外部架包依赖引入 外部依赖包引入 ...
 
随机推荐
- Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)
			
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
 - Effective C++ —— 定制new和delete(八)
			
STL容器所使用的heap内存是由容器所拥有的分配器对象管理,不是被new和delete直接管理.本章并不讨论STL分配器. 条款49 : 了解new-handler的行为 当operator new ...
 - iOS应用国际化教程(2014版)
			
本文转载至 http://www.cocoachina.com/industry/20140526/8554.html 这篇教程将通过一款名为iLikeIt的应用带你了解最基础的国际化概念,并为你的应 ...
 - 配置linux DNS
			
DNS服务器地址配置 在Linux下面,有一个默认的DNS服务器地址配置文件的设置,存放在 /etc/resolv.conf 设置方法很简单,通过编辑 vi /etc/resolv.conf 设置首选 ...
 - [UML]UML 教程 - 第二部分
			
UML作为软件开发典型的开发过程 业务过程模型创建 业务过程模型被用来定义发生在企业或组织内部的高级业务活动和业务过程,并且是建立用例模型的基础.一般来说业务过程模型比一个软件系统所能实现的更多(比如 ...
 - vue.js实现初了解(一)
			
1. vue 2.0是用Flow做静态类型检查, 3.0对TypeScript的支持更好了: 2. vue.js是基于Rollup(更轻量,适合js库的构建)构建的,它的构建相关配置都在scripts ...
 - FZU 2105 Digits Count
			
Problem 2105 Digits Count Accept: 444 Submit: 2139 Time Limit: 10000 mSec Memory Limit : 2621 ...
 - Ubuntu下MySQL主从同步配置
			
一.在两台Linux机器上安装MySQL 二.Master主服务器配置(192.168.1.3) 1.编辑my.cnf编(命令查找文件位置:find / -name my.cnf) vi /etc/m ...
 - Linux eventfd分析
			
2017-07-20 eventfd在linux中是一个较新的进程通信方式,和信号量等不同的是event不仅可以用于进程间的通信,还可以用户内核发信号给用户层的进程.eventfd在virtIO后端驱 ...
 - 006-ant design -结合echart-地址map市
			
基于上节的引用 // 引入 ECharts 主模块 import echarts from 'echarts/lib/echarts'; // 引入 ECharts 图形模块 import 'echa ...