SQl语句
<insert id="addMessage" parameterType="com.imooc.bean.Message">
insert into MESSAGE(command,description,content) values(#{command},#{description},#{content})
</insert>
dao层
/**
* 新增消息
*/
public void addMessage(Message message) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
// 通过sqlSession执行SQL语句
sqlSession.insert("Message.addMessage", message);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
service层
/**
* 添加信息
*/
public void addMessage(Message message) {
MessageDao messageDao = new MessageDao();
messageDao.addMessage(message);
}
servlet层
public class AddMessageServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置编码
req.setCharacterEncoding("UTF-8");
String commands = req.getParameter("commands");
String descriptions = req.getParameter("descriptions");
String contents = req.getParameter("contents");
Message message = new Message();
message.setCommand(commands);
message.setDescription(descriptions);
message.setContent(contents);
MaintainService maintainService = new MaintainService();
maintainService.addMessage(message);
// 跳转
req.getRequestDispatcher("/List.action").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
jsp页面
<form action="<%=basePath%>AddMessageServlet.action" method="post">
<table align="center">
<tr>
<td>指令名称:</td>
<td><input type="text" name="commands" /></td>
</tr>
<tr></tr>
<tr>
<td>描述:</td>
<td><input type="text" name="descriptions" /></td>
</tr>
<tr>
<td>操作:</td>
<td><input type="text" name="contents" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交" /></td>
</tr>
</table>
</form>