插入數(shù)據(jù)實(shí)現(xiàn)
list.jsp 21行,跳轉(zhuǎn)到另一個(gè)頁(yè)面,接受輸入? ??<p class="g_title fix">內(nèi)容列表 <a class="btn03" href="index.jsp">新 增</a>
和list.jsp同級(jí)創(chuàng)建index.jsp,寫一個(gè)表單? ? ?
<form action="<%=basePath %>InsertOneServlet.action" id="mainForm" method="post">
<tr>
<td width="90" align="right">指令名稱:</td>
<td>
<input name="command" type="text" class="allInput" value="${command}"/>
</td>
? ? ? ? </tr>
? ? ? ? <tr>
<td width="90" align="right">描述:</td>
<td>
<input name="description" type="text" class="allInput" value="${description}"/>
</td>
? ? ? ? </tr>
? ? ? ? <tr>
<td width="90" align="right">內(nèi)容:</td>
<td>
<input name="content" type="text" class="allInput" value="${content}"/>
</td>
</tr>
? ? ? ? <tr>
? ? ? ? ? ? <td colspan="2" width="85" align="right"><input type="submit" class="tabSub" value="確定" /></td>
? ? ? ? </tr>
創(chuàng)建InsertOneServlet,配置web.xml
//接受頁(yè)面的值
String command = req.getParameter("command");
String description = req.getParameter("description");
String content = req.getParameter("content");
MaintainService maintainService = new MaintainService();
maintainService.insertOne(command, description, content);
//向頁(yè)面跳轉(zhuǎn)
req.getRequestDispatcher("/List.action").forward(req, resp);
編寫service
public void insertOne(String command,String description,String content){
MessageDao messageDao = new MessageDao();
Message message = new Message();
message.setCommand(command);
message.setDescription(description);
message.setContent(content);
messageDao.insertOne(message);
}
編寫DAO
方法頭部? public void insertOne(Message m)
sqlSession = dBAccess.getSqlSession();
//通過(guò)sqlSession實(shí)現(xiàn)sql語(yǔ)句
sqlSession.insert("Message.insertOne",m);
System.out.println("插入方法執(zhí)行....."+"content:"+m.getContent());
sqlSession.commit();
配置Message.xml
<insert id="insertOne" parameterType="com.imooc.bean.Message" useGeneratedKeys="false">
? ? INSERT INTO MESSAGE
? ? ( ID,
? ? COMMAND,
? ? DESCRIPTION,
? ? CONTENT
? ? )
? ? VALUES
? ? ( #{id},
? ? #{command,jdbcType=VARCHAR},
? ? #{description,jdbcType=VARCHAR},
? ? #{content,jdbcType=BOOLEAN}
? ? )
? </insert>
我先從Message.xml開(kāi)始寫,寫到service不知道方法的參數(shù)該怎么寫,又從頁(yè)面開(kāi)始寫,用三個(gè)String接收輸入,在service中傳給Message對(duì)象
2019-07-16
傳入對(duì)象即可