SSH整合- 2- add service layout

从业务里抽出实体类,并添加service层!管理user的业务逻辑。

1. 主要文件

 

SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout

 

2. registerDeal.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ page import="java.sql.Connection" %>

<%@ page import="java.sql.DriverManager" %>

<%@ page import="java.sql.PreparedStatement" %>

<%@ page import="java.sql.SQLException" %>

<%@ page import="com.edu.ssh.model.User" %>

<%@ page import="com.edu.ssh.service.UserManager" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

  <title>登录处理</title>

  </head>

 

  <body>

<%

 

String username = request.getParameter("username").trim();

String password = request.getParameter("password").trim();

String password2 = request.getParameter("password2").trim();

User user = new User();

user.setUsername(username);

user.setPassword(password);

UserManager userManager = new UserManager();

 

// 简单验证,不填其他的复杂逻辑

if (!userManager.exists(user)) {

userManager.insertOneUser(user);

//response.sendRedirect("registerSucess.jsp");

request.setAttribute("username", username);

request.getRequestDispatcher("registerSucess.jsp").forward(request, response);

} else {

//response.sendRedirect("registerFail.jsp");

request.getRequestDispatcher("registerFail.jsp").forward(request, response);

}

%> 

  </body>

</html>

 

3. UserManager.java

 

package com.edu.ssh.service;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.edu.ssh.model.User;

 

 

public class UserManager {

 

private static String url = "jdbc:mysql://localhost:3306/ssh";

private static String dbusername = "root";

private static String pw = "mysql123";

 

 

public Connection getConnection() {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

}

try {

return DriverManager.getConnection(url, dbusername, pw);

} catch (SQLException e) {

e.printStackTrace();

}

 

return null;

}

 

 

public void closeAll(PreparedStatement pstat, Connection conn) {

if (null != null) {

try {

pstat.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

 

if (null != conn) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

 

 

public boolean exists(User user) {

String selectSql = "select count(*) from tb_user where username=?";

Connection conn = this.getConnection();

PreparedStatement pstat = null;

 

try {

pstat = conn.prepareStatement(selectSql);

pstat.setString(1, user.getUsername());

ResultSet rs = pstat.executeQuery();

rs.next();

int count = rs.getInt(1);

if (count > 0) {

 

return true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll(pstat, conn);

}

 

return false;

}

 

 

public void insertOneUser(User user) {

String sql = "insert into tb_user(id, username, password) values(null, ?, ?)";

Connection conn = this.getConnection();

PreparedStatement pstat = null;

try {

pstat = conn.prepareStatement(sql);

pstat.setString(1, user.getUsername());

pstat.setString(2, user.getPassword());

pstat.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll(pstat, conn);

}

}

}

 

4. User.java

 

package com.edu.ssh.model;

 

 

public class User {

private int id;

private String username;

private String password;

 

 

public int getId() {

return id;

}

 

 

public void setId(int id) {

this.id = id;

}

 

 

public String getUsername() {

return username;

}

 

 

public void setUsername(String username) {

this.username = username;

}

 

 

public String getPassword() {

return password;

}

 

 

public void setPassword(String password) {

this.password = password;

}

}

 

 

5. UserManagerTest

 

package com.edu.ssh.service;

 

import static org.junit.Assert.*;

 

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

 

import com.edu.ssh.model.User;

 

 

public class UserManagerTest {

 

private UserManager userManager = null;

 

@Before

public void setUp() throws Exception {

userManager = new UserManager();

}

 

 

@After

public void tearDown() throws Exception {

userManager = null;

}

 

 

@Test

public void testExists() {

User user = new User();

user.setUsername("admin");

user.setPassword("admin");

assertTrue("exists() 方法失败了!!", userManager.exists(user));

}

 

 

@Test

public void testInsertOneUser() {

User user = new User();

user.setUsername("adminff");

user.setPassword("adminff");

userManager.insertOneUser(user);

assertTrue("insertOneUser() 方法失败了!!", userManager.exists(user));

}

 

}

 

6. 结果

SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout

SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout


SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout


SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout

7. UML 图

 

SSH整合- <wbr>2- <wbr>add <wbr>service <wbr>layout



 

8. 分析

     由于吧把业务逻辑和操作数据库都放在jsp里,使得整个jsp层很混乱,修改维护困难,基本不可重用。由此,我们为了增加灵活性,我们增加了 service层专门来管理User的逻辑,数据库的操作。使得jsp仅仅是作为显示。但是在service层不应该直接和数据库操作。如果我想换个数据 库我们就要找到连接数据库的代码并修改它,加入我们有很多了service层的类,我们可能就要一个个修改,维护麻烦。一次我们可以考虑在增加一个层 --dao层,dao层专门处理数据库我们只在service里调用dao提供的接口就可以了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-09-09 10:02  ubuntuvim  阅读(202)  评论(0编辑  收藏  举报