jsp整合mybatis案例

举报
tea_year 发表于 2021/12/29 23:29:01 2021/12/29
【摘要】 View视图层:注册页面   <form action="regServlet" method="post"> 账号:<input type="text" name="username"/><br/> 密码:<input type="password" name="...

View视图层:注册页面

 


  
  1. <form action="regServlet" method="post">
  2. 账号:<input type="text" name="username"/><br/>
  3. 密码:<input type="password" name="pwd"/><br/>
  4. 确认密码:<input type="password" name="repwd"/><br/>
  5. 爱好:<input type="checkbox" name="hobby" value="读书"/>读书
  6. <input type="checkbox" name="hobby" value="旅游"/>旅游
  7. <input type="checkbox" name="hobby" value="逛街"/>逛街
  8. <br/>
  9. <input type="submit" value="注册"/>"
  10. </form>


显示所有页面的jsp代码

 

 


  
  1. <table border="1" width="100%">
  2. <tr>
  3. <td>账户</td><td>密码</td><td>爱好</td><td colspan="3">操作</td>
  4. </tr>
  5. <c:forEach items="${list}" var="m">
  6. <tr>
  7. <td>${m["USERNAME"] }</td>
  8. <td>${m["PWD"] }</td>
  9. <td>${m["AIHAO"] }</td>
  10. <td><a href='zhuce.jsp'>增加</a></td>
  11. <td><a href='delServlet?username=${m["USERNAME"] }'>删除</a></td>
  12. <td><a href='javascript:alert("a")'>删除</a></td>
  13. </tr>
  14. </c:forEach>
  15. </table>


web.xml配置文件

 

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>zhuce.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 配置servlet -->
  11. <servlet>
  12. <servlet-name>regServlet</servlet-name>
  13. <servlet-class>com.controller.RegServlet</servlet-class>
  14. </servlet>
  15. <servlet>
  16. <servlet-name>allServlet</servlet-name>
  17. <servlet-class>com.controller.AllServlet</servlet-class>
  18. </servlet>
  19. <servlet>
  20. <servlet-name>DelServlet</servlet-name>
  21. <servlet-class>com.controller.DelServlet</servlet-class>
  22. </servlet>
  23. <servlet-mapping>
  24. <servlet-name>regServlet</servlet-name>
  25. <url-pattern>/regServlet</url-pattern>
  26. </servlet-mapping>
  27. <servlet-mapping>
  28. <servlet-name>allServlet</servlet-name>
  29. <url-pattern>/allServlet</url-pattern>
  30. </servlet-mapping>
  31. <servlet-mapping>
  32. <servlet-name>DelServlet</servlet-name>
  33. <url-pattern>/delServlet</url-pattern>
  34. </servlet-mapping>
  35. </web-app>


mybatis的配置文件

 

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <environments default="development">
  6. <environment id="development">
  7. <transactionManager type="JDBC"/>
  8. <dataSource type="POOLED">
  9. <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
  10. <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
  11. <property name="username" value="scott"/>
  12. <property name="password" value="tiger"/>
  13. </dataSource>
  14. </environment>
  15. </environments>
  16. <mappers>
  17. <mapper resource="com/mapper/UserinfoMapper.xml"/>
  18. </mappers>
  19. </configuration>

返回SqlSession对象的公共类

 

 


  
  1. package com.util;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. //公共类,读取config.xml文件,用来和数据库建立联系
  9. //The AdapterNet wrong:修改config.xml为ip地址
  10. public class SSFU {
  11. public static SqlSession getSqlSession(){
  12. Reader reader=null;
  13. SqlSessionFactory factory=null;
  14. SqlSession session=null;
  15. try {
  16. reader = Resources.getResourceAsReader("config.xml");
  17. factory=new SqlSessionFactoryBuilder().build(reader);
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }finally{
  21. try {
  22. if(reader!=null){reader.close();}
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. return factory.openSession();
  28. }
  29. }


model层

 

实体类:

 


  
  1. package com.model;
  2. //用户信息实体类
  3. public class Userinfo {
  4. private String username;
  5. private String pwd;
  6. private String aihao;
  7. public String getUsername() {
  8. return username;
  9. }
  10. public void setUsername(String username) {
  11. this.username = username;
  12. }
  13. public String getPwd() {
  14. return pwd;
  15. }
  16. public void setPwd(String pwd) {
  17. this.pwd = pwd;
  18. }
  19. public String getAihao() {
  20. return aihao;
  21. }
  22. public void setAihao(String aihao) {
  23. this.aihao = aihao;
  24. }
  25. }

接口业务定义:

 

 


  
  1. package com.biz;
  2. import java.util.List;
  3. import java.util.Map;
  4. import com.model.Userinfo;
  5. //接口,定义了4个抽象方法,增删改查,对userinfo表
  6. public interface UserinfoMapper {
  7. public int insertUserinfo(Userinfo u);
  8. public int updateUserinfo(Userinfo u);
  9. public int deleteUserinfo(Userinfo u);
  10. public List<Map>selectAll();//无参数;
  11. }


接口对应的映射文件

 

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!--这个映射文件,相当于接口的实现类,4个方法,现在4个实现. -->
  5. <mapper namespace="com.biz.UserinfoMapper">
  6. <insert id="insertUserinfo" parameterType="com.model.Userinfo">
  7. insert into userinfo values(#{username},#{pwd},#{aihao})
  8. </insert>
  9. <update id="updateUserinfo" parameterType="com.model.Userinfo">
  10. update userinfo set pwd=#{pwd} where username=#{username}
  11. </update>
  12. <delete id="deleteUserinfo" parameterType="com.model.Userinfo">
  13. delete from userinfo where username=#{username}
  14. </delete>
  15. <select id="selectAll" resultType="java.util.Map">
  16. select * from userinfo
  17. </select>
  18. </mapper>

控制层,控制器

 

注册Servlet

 


  
  1. package com.controller;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import org.apache.ibatis.session.SqlSession;
  9. import com.biz.UserinfoMapper;
  10. import com.model.Userinfo;
  11. import com.util.SSFU;
  12. //作用是一个控制器,一手托view,一手托model
  13. public class RegServlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  16. throws ServletException, IOException {
  17. //0.解决乱码
  18. req.setCharacterEncoding("utf-8");
  19. //1.接受从zhuce页面传过来的数据;
  20. String username=req.getParameter("username");//名字不一样,会报NUllpointerException
  21. String pwd=req.getParameter("pwd");
  22. String[]hobbys=req.getParameterValues("hobby");
  23. //1.2将爱好数组转化为字符串 ;
  24. String aihao="";
  25. for(int i=0;i<hobbys.length;i++){
  26. aihao+=hobbys[i]+",";
  27. }
  28. aihao=aihao.substring(0,aihao.length()-1);//截取最后的一个","
  29. //*********************************
  30. //操作model对象;新增对象;
  31. Userinfo u=new Userinfo();
  32. u.setUsername(username);
  33. u.setPwd(pwd);
  34. u.setAihao(aihao);
  35. //真正的存放到数据库,利用mybatis
  36. SqlSession session =SSFU.getSqlSession();
  37. UserinfoMapper um = session.getMapper(UserinfoMapper.class);
  38. um.insertUserinfo(u);
  39. session.commit();
  40. session.close();
  41. //将数据放到session,然后重定向;这个是假的增加数据;
  42. HttpSession hsession=req.getSession();
  43. hsession.setAttribute("u",u);//设置session的属性,存放刚才的对象;
  44. resp.sendRedirect("chenggong.jsp");
  45. }
  46. @Override
  47. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  48. throws ServletException, IOException {
  49. this.doGet(req, resp);
  50. }
  51. }


查找所有Servlet

 

 


  
  1. package com.controller;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import org.apache.ibatis.session.SqlSession;
  11. import com.biz.UserinfoMapper;
  12. import com.util.SSFU;
  13. //查询所有用户信息的控制器;
  14. public class AllServlet extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  17. throws ServletException, IOException {
  18. //从数据库查询所有信息;
  19. SqlSession session =SSFU.getSqlSession();
  20. UserinfoMapper um = session.getMapper(UserinfoMapper.class);
  21. List<Map>list=um.selectAll();
  22. System.out.println(list.size());
  23. for(Map map:list){
  24. System.out.println("姓名:"+map.get("USERNAME")+",密码:"+map.get("PWD")+",爱好:"+map.get("AIHAO"));
  25. }
  26. session.commit();
  27. session.close();
  28. HttpSession hsession=req.getSession();
  29. hsession.setAttribute("list", list);
  30. resp.sendRedirect("showAll.jsp");
  31. }
  32. @Override
  33. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  34. throws ServletException, IOException {
  35. this.doGet(req, resp);
  36. }
  37. }


读者可以在此基础上,完善下修改 和删除操作,如果修改和删除,是汉字的话,可能会有get传值乱码问题,可以到tomcat的conf目录下,修改server.xml,在connector标签下,增加URIEncoding="utf-8"即可。

 


 

 

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/69470195

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。