标签:
---恢复内容开始---
这个例子的实现过程:用户登录----->servlet验证用户是否存在----->显示商品信息----->可以进行商品的删除、修改、添加功能(商品信息是分页显示的)
例子很简单,但是基本的mybatis框架一个也差不多是这样的
数据库表格如下
项目的结构
1.src目录下的mybatis文件和外部源文件
jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.username=vitelon jdbc.password=vitelon
mybatis.xml文件
2.对应的mapper映射文件UsersMapper.xml和GoodMapper.xml文件和对应的mapper接口文件
UsersMapper.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"> <!-- 命名空间的路径和StudentMapper接口的路径一定要一样 --> <!-- mapper和mapper接口一定要一样 --> <mapper namespace="vitelon.mapper.UsersMapper"> <!-- 查询登录用户 --> <select id="selectUsers" resultType="Users"> select *from users where username=#{name} and password=#{pwd} </select> </mapper>
GoodMapper.xml
UsersMapper接口
package vitelon.mapper; import org.apache.ibatis.annotations.Param; import vitelon.bean.Users; public interface UsersMapper { public Users selectUsers(@Param("name") String username, @Param("pwd") String password); }
GoodMapper接口
package vitelon.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; public interface GoodMapper { public List selectGoodAll(); public int deleteGoodId(@Param("did") int did); public int updateUser(@Param("name") String name, @Param("price") int price, @Param("amount") int amount, @Param("id") int gid); public int insertGood(@Param("id") int gid, @Param("name") String name, @Param("price") int price, @Param("amount") int amount); public List selectPage(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize); public int allRecords(); }
3.dao层接口文件
UsersDao接口
package vitelon.dao; import org.apache.ibatis.session.SqlSession; import vitelon.bean.Users; public interface UsersDao { // 获取SqlSession对象方法; public SqlSession getSqlSession(); public Users selectUsers(String username, String password); }
GoodDao接口
package vitelon.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.SqlSession; public interface GoodDao { // 获取SqlSession对象 public SqlSession getSqlSession(); public List selectGoodAll(); public int deleteGoodId(int did); public int updateUser(String name, int price, int amount, int gid); public int insertGood(int gid, String name, int price, int amount); public List selectPage(int pageIndex, int pageSize); public int allRecords(); }
dao接口的实现类
UsersDaoImpl实现类
package vitelon.dao.impl; import org.apache.ibatis.session.SqlSession; import vitelon.bean.Users; import vitelon.dao.UsersDao; import vitelon.mapper.UsersMapper; public class UsersDaoImpl extends BaseDaoImpl<UsersMapper>implements UsersDao { public UsersDaoImpl() { this.setMapper(UsersMapper.class); } @Override public SqlSession getSqlSession() { // TODO Auto-generated method stub return this.sqlSession; } @Override public Users selectUsers(String username, String password) { // TODO Auto-generated method stub return this.getMapper().selectUsers(username, password); } }
GoodDaoImpl实现类
BaseDaoImpl获取资源类
package vitelon.dao.impl; /** * 初始化并数据库连接的类 */ import java.io.*; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.*; public class BaseDaoImpl<T> { private SqlSessionFactory sqlSessionFactory; protected SqlSession sqlSession; private Class<T> mapper; // 泛型类的接口,传什么型就是什么类型 private String name; // 连接数据库 public BaseDaoImpl() { initSqlSessionFactory();// 初始化方法 sqlSession = sqlSessionFactory.openSession(); /* System.out.println("连接成功:" + sqlSession); */ } // 初始化SqlSessionFactory()方法; public void initSqlSessionFactory() { InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatis.xml"); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } public T getMapper() { return sqlSession.getMapper(mapper); } public void setMapper(Class<T> mapper) { this.mapper = mapper; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.Service层
UsersService接口
package vitelon.service; import org.apache.ibatis.session.SqlSession; import vitelon.bean.Users; public interface UsersService { // 获取SqlSession对象方法; public SqlSession getSqlSession(); public Users selectUsers(String username, String password); }
GoodService接口
package vitelon.service; import java.util.List; import org.apache.ibatis.session.SqlSession; public interface GoodService { // 获取SqlSession对象 public SqlSession getSqlSession(); public List selectGoodAll(); public int deleteGoodId(int did); public int updateUser(String name, int price, int amount, int gid); public int insertGood(int gid, String name, int price, int amount); public List selectPage(int pageIndex, int pageSize); public int allRecords(); }
Service接口的实现类
UsersService实现类
package vitelon.service.impl; import org.apache.ibatis.session.SqlSession; import vitelon.bean.Users; import vitelon.dao.UsersDao; import vitelon.dao.impl.UsersDaoImpl; import vitelon.service.UsersService; public class UsersServiceImpl implements UsersService { UsersDao ud = new UsersDaoImpl(); public UsersServiceImpl() { } @Override public SqlSession getSqlSession() { // TODO Auto-generated method stub return ud.getSqlSession(); } @Override public Users selectUsers(String username, String password) { // TODO Auto-generated method stub return ud.selectUsers(username, password); } }
GoodServiceImpl实现类
package vitelon.service.impl; import java.util.List; import javax.annotation.Resource; import org.apache.ibatis.session.SqlSession; import org.springframework.stereotype.Service; import vitelon.dao.GoodDao; import vitelon.dao.impl.GoodDaoImpl; import vitelon.service.GoodService; @Service public class GoodServiceImpl implements GoodService { GoodDao gd = new GoodDaoImpl(); public GoodServiceImpl() { super(); // TODO Auto-generated constructor stub } @Override public SqlSession getSqlSession() { // TODO Auto-generated method stub return gd.getSqlSession(); } @Override public List selectGoodAll() { // TODO Auto-generated method stub return gd.selectGoodAll(); } @Override public int deleteGoodId(int did) { // TODO Auto-generated method stub return gd.deleteGoodId(did); } @Override public int updateUser(String name, int price, int amount, int gid) { // TODO Auto-generated method stub return gd.updateUser(name, price, amount, gid); } @Override public int insertGood(int gid, String name, int price, int amount) { // TODO Auto-generated method stub return gd.insertGood(gid, name, price, amount); } @Override public List selectPage(int pageIndex, int pageSize) { // TODO Auto-generated method stub return gd.selectPage(pageIndex, pageSize); } @Override public int allRecords() { // TODO Auto-generated method stub return gd.allRecords(); } }
5.servlet层
deleteGood删除商品的servlet
package vitelon.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import vitelon.service.GoodService; import vitelon.service.impl.GoodServiceImpl; /** * Servlet implementation class deleteGood */ @WebServlet("/deleteGood") public class deleteGood extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public deleteGood() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //获取商品id int did = Integer.parseInt(request.getParameter("did")); GoodService gs = new GoodServiceImpl(); //根据商品id删除商品 int res = gs.deleteGoodId(did); gs.getSqlSession().commit(); if (res > 0) { response.sendRedirect("GoodServlet"); } else { System.out.println("no"); } } }
dologin处理用户名登录验证的servlet
package vitelon.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import vitelon.bean.Users; import vitelon.service.UsersService; import vitelon.service.impl.UsersServiceImpl; /** * Servlet implementation class dologin */ @WebServlet("/dologin.do") public class dologin extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dologin() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String name=request.getParameter("username"); String pwd=request.getParameter("password"); UsersService us=new UsersServiceImpl(); //查询用户是否存在 Users u=us.selectUsers(name, pwd); if (u!=null) { response.sendRedirect("GoodServlet"); } else { System.out.println("用户不存在!"); } } }
GoodServlet分页显示商品的servlet
package vitelon.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import vitelon.mapper.GoodMapper; import vitelon.service.GoodService; import vitelon.service.impl.GoodServiceImpl; /** * Servlet implementation class GoodServlet */ @WebServlet("/GoodServlet") public class GoodServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GoodServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); GoodService gs=new GoodServiceImpl(); HttpSession session=request.getSession(); int pageSize = 2; // 每页行数 //得到总行数 int allRecords = gs.allRecords(); //得到总页数 int totalPages = (allRecords % pageSize == 0) ? (allRecords / pageSize) : (allRecords / pageSize + 1); // 获取当前页 int pageIndex = 1;// 当前页设为1 String pageIndexStr = request.getParameter("pageIndex"); if (pageIndexStr == null) { pageIndexStr = "1"; } pageIndex = Integer.parseInt(pageIndexStr.trim()); //执行分页查询SQL语句,获取结果集 List list = gs.selectPage(pageIndex, pageSize); session.setAttribute("goodlist", list); session.setAttribute("pageIndex", pageIndex); session.setAttribute("totalPages", totalPages); response.sendRedirect("goodShow.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
insertGood添加商品的servlet
package vitelon.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import vitelon.service.GoodService; import vitelon.service.impl.GoodServiceImpl; /** * Servlet implementation class insertGood */ @WebServlet("/insertGood") public class insertGood extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public insertGood() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); int gid = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); int price = Integer.parseInt(request.getParameter("price")); int amount = Integer.parseInt(request.getParameter("amount")); GoodService gs=new GoodServiceImpl(); //添加商品 int res = gs.insertGood(gid, name, price, amount); gs.getSqlSession().commit(); if (res > 0) { response.sendRedirect("GoodServlet"); } else { System.out.println("no"); } } }
updateaddGood修改商品的servlet
package vitelon.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import vitelon.service.GoodService; import vitelon.service.impl.GoodServiceImpl; /** * Servlet implementation class updateaddGood */ @WebServlet("/updateaddGood") public class updateaddGood extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public updateaddGood() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); int gid = Integer.parseInt(request.getParameter("gid")); String name = request.getParameter("newname"); int price = Integer.parseInt(request.getParameter("newprice")); int amount = Integer.parseInt(request.getParameter("newamount")); GoodService gs = new GoodServiceImpl(); //根据商品id修改商品信息 int res = gs.updateUser(name, price, amount, gid); gs.getSqlSession().commit(); if (res > 0) { response.sendRedirect("GoodServlet"); } else { System.out.println("no"); } } }
6.login.jsp登录的动态网页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="dologin.do" method="post"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="submit" name="submit" value="登录"><br> </form> </body> </html>
goodShow.jsp显示商品的动态网页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="jquery-1.8.3.js"></script> </head> <body> <h3 > <c:if test="${pageIndex>1 }"> <a href="GoodServlet?pageIndex=1">首页</a> <a href="GoodServlet?pageIndex=${pageIndex -1}">上一页</a> </c:if> <c:if test="${pageIndex < totalPages }"> <a href="GoodServlet?pageIndex=${pageIndex +1} ">下一页</a> <a href="GoodServlet?pageIndex=${totalPages }">尾页</a> </c:if> </h3> <c:forEach var="info" items="${goodlist }"> <form action="updateaddGood?gid=${info.g_id }" method="post"> 商品ID:${info.g_id } 商品名称:${info.g_name } 平商品价格:${info.g_price } 商品数量:${info.g_amount } <a href="deleteGood?did=${info.g_id }">删除</a> <br> 商品名称:<input type="text" name="newname" size="10"> 商品价格:<input type="text" name="newprice" size="10"> 商品数量:<input type="text" name="newamount" size="10"> <input type="submit" name="submit" value="修改"> <a href="#insertGood" onclick="addGood(this)">添加</a> </form> <br><br><br> </c:forEach> <br><br><br><br> <div id="addGood" style="display: none;"> ********************************************************************************************** <br> <form action="insertGood" method="post" id="insertGood"> 商品ID:<input type="text" name="id" size="10"> 商品名称:<input type="text" name="name" size="10"> 商品价格:<input type="text" name="price" size="10"> 商品数量:<input type="text" name="amount" size="10"> <input type="submit" name="submit" value="添加"> </form> <br> ********************************************************************************************** </div> <script type="text/javascript"> function addGood(id){ $("#addGood").toggle("slow"); } </script> </body> </html>
2015-10-26 23:32:25
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/vitelon/p/4912898.html