Bill
public class Bill { private Integer id; //id private String billCode; //账单编码 private String productName; //商品名称 private String productDesc; //商品描述 private String productUnit; //商品单位 private BigDecimal productCount; //商品数量 private BigDecimal totalPrice; //总金额 private Integer isPayment; //是否支付 private Integer providerId; //供应商ID private Integer createdBy; //创建者 private Date creationDate; //创建时间 private Integer modifyBy; //更新者 private Date modifyDate;//更新时间 private String providerName;//供应商名称 }Provider
public class Provider { private Integer id; //id private String proCode; //供应商编码 private String proName; //供应商名称 private String proDesc; //供应商描述 private String proContact; //供应商联系人 private String proPhone; //供应商电话 private String proAddress; //供应商地址 private String proFax; //供应商传真 private Integer createdBy; //创建者 private Date creationDate; //创建时间 private Integer modifyBy; //更新者 private Date modifyDate;//更新时间 }Role
public class Role { private Integer id; //id private String roleCode; //角色编码 private String roleName; //角色名称 private Integer createdBy; //创建者 private Date creationDate; //创建时间 private Integer modifyBy; //更新者 private Date modifyDate;//更新时间 }User
public class User { private Integer id;//id private String userCode;//用户编码 private String userName;//用户名 private String userPassword;//用户密码 private Integer gender;//性别 private Date birthday;//出生日期 private String phone;//电话号码 private String address;//地址 private Integer userRole;//用户角色 private Integer createdBy;//创建者 private Date creationDate;//创建日期 private Integer modifyBy;//更新者 private Date modifyDate;//更新日期 private Integer age;//年龄 private String userRoleName;//用户角色名称 public String getUserRoleName(){return userRoleName;} public void setUserRoleName(String userRoleName){this.userRoleName=userRoleName;} public Integer getAge(){ Date date = new Date(); Integer age = date.getYear() - birthday.getYear(); return age; } }BaseDao:
//操作数据库的公共类 public class BaseDao { private static String driver; private static String url; private static String username; private static String password; //静态代码块,类加载的时候就加载了 static { InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } //获取数据库的连接 public static Connection getConnection(){ Connection connection=null; try { Class.forName(driver); connection = DriverManager.getConnection(url,username,password); } catch (Exception e) { e.printStackTrace(); } return connection; } //编写查询公共方法 public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params) throws SQLException { //获取preparedStatement对象,用以操作mysql preparedStatement = connection.prepareStatement(sql); //遍历占位符 for (int i = 0; i <params.length ; i++) { //数组开始于 0 号,而占位符的编号开始于 1 号 preparedStatement.setObject(i + 1, params[i]); } //查询 resultSet = preparedStatement.executeQuery(); //返回结果集 return resultSet; } //编写增删改公共方法 public static int execute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws SQLException { //获取preparedStatement对象,用以操作mysql preparedStatement = connection.prepareStatement(sql); //遍历占位符 for (int i = 0; i <params.length ; i++) { //数组开始于 0 号,而占位符的编号开始于 1 号 preparedStatement.setObject(i + 1, params[i]); } //查询 int updateRows = preparedStatement.executeUpdate(); //返回结果集 return updateRows; } //关闭资源 public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) { boolean flag = true; if(resultSet!=null){ try { resultSet.close(); //GC回收 resultSet=null; } catch (SQLException throwables) { throwables.printStackTrace(); flag= false; } } if(preparedStatement!=null){ try { preparedStatement.close(); // preparedStatement=null; } catch (SQLException throwables) { throwables.printStackTrace(); flag= false; } } if(connection!=null){ try { connection.close(); // connection=null; } catch (SQLException throwables) { throwables.printStackTrace(); flag= false; } } return flag; } }UserDao
public interface UserDao { public User getLoginUser(Connection connection, String userCode) throws SQLException; }UserDaoImpl
public class UserDaoImpl implements UserDao{ public User getLoginUser(Connection connection, String userCode) throws SQLException { PreparedStatement prst = null; ResultSet rest = null; User user = null; if(connection!=null){//判断数据库是否已经连接上,防止异常 String sql = "select * from smbms_user where userCode=?"; Object[] params={userCode}; rest = BaseDao.execute(connection, prst, rest, sql, params); if(rest.next()){ user = new User(); user.setId(rest.getInt("id")); user.setUserCode(rest.getString("userCode")); user.setUserPassword(rest.getString("userPassword")); user.setGender(rest.getInt("gender")); user.setBirthday(rest.getDate("birthday")); user.setPhone(rest.getString("phone")); user.setAddress(rest.getString("address")); user.setUserRole(rest.getInt("userRole")); user.setCreatedBy(rest.getInt("createdBy")); user.setCreationDate(rest.getDate("creationDate")); user.setModifyBy(rest.getInt("modifyBy")); user.setModifyDate(rest.getDate("modifyDate")); } //关闭资源 BaseDao.closeResource(null,prst,rest); } return user; } }filter下的
Filter
public class CharacterEncodingFilter implements Filter{ public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); filterChain.doFilter(servletRequest,servletResponse); } public void destroy() { } }UserService
public interface UserService { //用户登录 public User login(String userCode,String password); }UserServiceImpl
public class UserServiceImpl implements UserService{ //业务层都会调用dao层,所以我们要我引入dao层; private UserDao userDao; //构造方法中 new UserDaoImpl(); public UserServiceImpl(){ userDao = new UserDaoImpl(); } public User login(String userCode, String password) { Connection connection=null; User user= null; try { connection= BaseDao.getConnection(); user = userDao.getLoginUser(connection, userCode); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { BaseDao.closeResource(connection,null,null); } return user; } @Test public void test(){ UserServiceImpl userService = new UserServiceImpl(); User admin = userService.login("admin", "123456"); System.out.println(admin.getUserPassword()); } }LoginServlet
public class LoginServlet extends HttpServlet { //servlet:控制层,调用业务层代码 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("LoginServlet--start..."); //获取用户名和密码 String userCode = req.getParameter("userCode"); String userPassword = req.getParameter("userPassword"); //和数据库中的密码做对比,调用service层 UserService userService = new UserServiceImpl(); User user = userService.login(userCode, userPassword);//查出了登录的人 if(user!=null){//查有此人可以登录 //保存用户信息到session中 req.getSession().setAttribute(Constants.USER_SESSION,user); //跳转到主页 resp.sendRedirect("jsp/frame.jsp"); }else {//查无此人,无法登录 //转发回登陆页面,顺带提示,用户名或者密码不正确 req.setAttribute("error","用户名或者密码错误"); req.getRequestDispatcher("login.jsp").forward(req,resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }Constants
public class Constants { public static final String USER_SESSION="userSession"; }by 狂神老师