jsp整合mybatis案例
【摘要】
View视图层:注册页面
<form action="regServlet" method="post"> 账号:<input type="text" name="username"/><br/> 密码:<input type="password" name="...
View视图层:注册页面
-
<form action="regServlet" method="post">
-
账号:<input type="text" name="username"/><br/>
-
密码:<input type="password" name="pwd"/><br/>
-
确认密码:<input type="password" name="repwd"/><br/>
-
爱好:<input type="checkbox" name="hobby" value="读书"/>读书
-
<input type="checkbox" name="hobby" value="旅游"/>旅游
-
<input type="checkbox" name="hobby" value="逛街"/>逛街
-
<br/>
-
<input type="submit" value="注册"/>"
-
</form>
显示所有页面的jsp代码
-
<table border="1" width="100%">
-
<tr>
-
<td>账户</td><td>密码</td><td>爱好</td><td colspan="3">操作</td>
-
</tr>
-
<c:forEach items="${list}" var="m">
-
<tr>
-
<td>${m["USERNAME"] }</td>
-
<td>${m["PWD"] }</td>
-
<td>${m["AIHAO"] }</td>
-
<td><a href='zhuce.jsp'>增加</a></td>
-
<td><a href='delServlet?username=${m["USERNAME"] }'>删除</a></td>
-
<td><a href='javascript:alert("a")'>删除</a></td>
-
</tr>
-
</c:forEach>
-
</table>
web.xml配置文件
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5"
-
xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
<welcome-file-list>
-
<welcome-file>zhuce.jsp</welcome-file>
-
</welcome-file-list>
-
<!-- 配置servlet -->
-
<servlet>
-
<servlet-name>regServlet</servlet-name>
-
<servlet-class>com.controller.RegServlet</servlet-class>
-
</servlet>
-
<servlet>
-
<servlet-name>allServlet</servlet-name>
-
<servlet-class>com.controller.AllServlet</servlet-class>
-
</servlet>
-
<servlet>
-
<servlet-name>DelServlet</servlet-name>
-
<servlet-class>com.controller.DelServlet</servlet-class>
-
</servlet>
-
-
-
<servlet-mapping>
-
<servlet-name>regServlet</servlet-name>
-
<url-pattern>/regServlet</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>allServlet</servlet-name>
-
<url-pattern>/allServlet</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>DelServlet</servlet-name>
-
<url-pattern>/delServlet</url-pattern>
-
</servlet-mapping>
-
</web-app>
mybatis的配置文件
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
-
"http://mybatis.org/dtd/mybatis-3-config.dtd">
-
<configuration>
-
<environments default="development">
-
<environment id="development">
-
<transactionManager type="JDBC"/>
-
<dataSource type="POOLED">
-
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
-
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
-
<property name="username" value="scott"/>
-
<property name="password" value="tiger"/>
-
</dataSource>
-
</environment>
-
</environments>
-
<mappers>
-
<mapper resource="com/mapper/UserinfoMapper.xml"/>
-
</mappers>
-
</configuration>
返回SqlSession对象的公共类
-
package com.util;
-
-
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;
-
//公共类,读取config.xml文件,用来和数据库建立联系
-
//The AdapterNet wrong:修改config.xml为ip地址
-
public class SSFU {
-
public static SqlSession getSqlSession(){
-
Reader reader=null;
-
SqlSessionFactory factory=null;
-
SqlSession session=null;
-
try {
-
reader = Resources.getResourceAsReader("config.xml");
-
factory=new SqlSessionFactoryBuilder().build(reader);
-
} catch (IOException e) {
-
e.printStackTrace();
-
}finally{
-
try {
-
if(reader!=null){reader.close();}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
return factory.openSession();
-
-
}
-
}
model层
实体类:
-
package com.model;
-
//用户信息实体类
-
public class Userinfo {
-
private String username;
-
private String pwd;
-
private String aihao;
-
public String getUsername() {
-
return username;
-
}
-
public void setUsername(String username) {
-
this.username = username;
-
}
-
public String getPwd() {
-
return pwd;
-
}
-
public void setPwd(String pwd) {
-
this.pwd = pwd;
-
}
-
public String getAihao() {
-
return aihao;
-
}
-
public void setAihao(String aihao) {
-
this.aihao = aihao;
-
}
-
-
-
}
接口业务定义:
-
package com.biz;
-
-
import java.util.List;
-
import java.util.Map;
-
import com.model.Userinfo;
-
-
//接口,定义了4个抽象方法,增删改查,对userinfo表
-
public interface UserinfoMapper {
-
-
public int insertUserinfo(Userinfo u);
-
public int updateUserinfo(Userinfo u);
-
public int deleteUserinfo(Userinfo u);
-
public List<Map>selectAll();//无参数;
-
}
接口对应的映射文件
-
<?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">
-
<!--这个映射文件,相当于接口的实现类,4个方法,现在4个实现. -->
-
<mapper namespace="com.biz.UserinfoMapper">
-
<insert id="insertUserinfo" parameterType="com.model.Userinfo">
-
insert into userinfo values(#{username},#{pwd},#{aihao})
-
</insert>
-
<update id="updateUserinfo" parameterType="com.model.Userinfo">
-
update userinfo set pwd=#{pwd} where username=#{username}
-
</update>
-
<delete id="deleteUserinfo" parameterType="com.model.Userinfo">
-
delete from userinfo where username=#{username}
-
</delete>
-
<select id="selectAll" resultType="java.util.Map">
-
select * from userinfo
-
</select>
-
</mapper>
控制层,控制器
注册Servlet
-
package com.controller;
-
-
import java.io.IOException;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
import org.apache.ibatis.session.SqlSession;
-
-
import com.biz.UserinfoMapper;
-
import com.model.Userinfo;
-
import com.util.SSFU;
-
//作用是一个控制器,一手托view,一手托model
-
public class RegServlet extends HttpServlet {
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
//0.解决乱码
-
req.setCharacterEncoding("utf-8");
-
-
//1.接受从zhuce页面传过来的数据;
-
String username=req.getParameter("username");//名字不一样,会报NUllpointerException
-
String pwd=req.getParameter("pwd");
-
String[]hobbys=req.getParameterValues("hobby");
-
//1.2将爱好数组转化为字符串 ;
-
String aihao="";
-
for(int i=0;i<hobbys.length;i++){
-
aihao+=hobbys[i]+",";
-
}
-
aihao=aihao.substring(0,aihao.length()-1);//截取最后的一个","
-
//*********************************
-
//操作model对象;新增对象;
-
Userinfo u=new Userinfo();
-
u.setUsername(username);
-
u.setPwd(pwd);
-
u.setAihao(aihao);
-
-
//真正的存放到数据库,利用mybatis
-
SqlSession session =SSFU.getSqlSession();
-
UserinfoMapper um = session.getMapper(UserinfoMapper.class);
-
um.insertUserinfo(u);
-
session.commit();
-
session.close();
-
//将数据放到session,然后重定向;这个是假的增加数据;
-
HttpSession hsession=req.getSession();
-
hsession.setAttribute("u",u);//设置session的属性,存放刚才的对象;
-
-
resp.sendRedirect("chenggong.jsp");
-
-
}
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
this.doGet(req, resp);
-
}
-
-
}
查找所有Servlet
-
package com.controller;
-
-
-
import java.io.IOException;
-
import java.util.List;
-
import java.util.Map;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
import org.apache.ibatis.session.SqlSession;
-
-
import com.biz.UserinfoMapper;
-
import com.util.SSFU;
-
//查询所有用户信息的控制器;
-
public class AllServlet extends HttpServlet {
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
//从数据库查询所有信息;
-
SqlSession session =SSFU.getSqlSession();
-
UserinfoMapper um = session.getMapper(UserinfoMapper.class);
-
List<Map>list=um.selectAll();
-
System.out.println(list.size());
-
for(Map map:list){
-
System.out.println("姓名:"+map.get("USERNAME")+",密码:"+map.get("PWD")+",爱好:"+map.get("AIHAO"));
-
}
-
-
session.commit();
-
session.close();
-
HttpSession hsession=req.getSession();
-
hsession.setAttribute("list", list);
-
resp.sendRedirect("showAll.jsp");
-
}
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
this.doGet(req, resp);
-
}
-
}
读者可以在此基础上,完善下修改 和删除操作,如果修改和删除,是汉字的话,可能会有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)