1. Web层:它包含了jsp servlet javaBean相关内容,当作业务流程的跳转,层中可写mvc设计模式,便于人机交互。
2. Service层:它也叫做bussiness层,类似中枢神经的存在关联其他两层,但它只处理业务逻辑操作。
3. Dao层 数据层 也叫做持久化层(持久化框架) 它的主要功能是负责对数据源(解析xml,数据库)访问,简单说,它就是包含对数据的CRUD 操作。
javaweb三层架构:
代码结构:
web架构:
架包:
实体类:
package com.dd.pojo;
public class User {
private int id;
private String username;
private String password;
private String email;
public User() {
}
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public User(int id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
连接数据库:
配置文件:
连接数据库:
package com.dd.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static DruidDataSource dataSource;
static {
try {
Properties properties=new Properties();
properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
dataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从数据库连接池中获取数据库连接
* @return
*/
public static Connection getConn(){
Connection conn=null;
try {
conn=dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
/**
* 关闭数据库连接
* @param conn
*/
public static void close(Connection conn){
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
工具类:
package com.dd.dao.impl;
import com.dd.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public abstract class BaseDao {
private QueryRunner queryRunner=new QueryRunner();
/**
* 执行 inset update delete 语句的方法
* @param sql 要执行的 sql 语句
* @param args 要执行的 sql 语句的参数值
* @return 受影响的行数
*/
public int update(String sql,Object... args){
Connection conn = JdbcUtils.getConn();
try {
return queryRunner.update(conn,sql,args);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.close(conn);
}
return -1;
}
/**
* 返回查询 JavaBean 的方法
* @param type
* @param sql 要执行的 sql 语句
* @param args 执行的 sql 语句的参数值
* @param 返回 JavaBean 的泛型
* @return 返回 null 说明查询失败
*/
public T queryForOne(Class type,String sql,Object... args){
Connection conn=JdbcUtils.getConn();
try {
return queryRunner.query(conn,sql,new BeanHandler(type),args);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.close(conn);
}
return null;
}
/**
* 返回多个 JavaBean 的方法
* @param type
* @param sql
* @param args
* @param
* @return
*/
public List queryForList(Class type,String sql,Object... args){
Connection conn=JdbcUtils.getConn();
try {
return queryRunner.query(conn,sql,new BeanListHandler(type),args);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.close(conn);
}
return null;
}
}
package com.dd.dao.impl;
import com.dd.dao.UserDao;
import com.dd.pojo.User;
public class UserDaoImpl extends BaseDao implements UserDao {
@Override
public User queryByUsername(String username) {
String sql = "select id,username,password,email from user where username=?";
return queryForOne(User.class, sql, username);
}
@Override
public User queryByUserNameAndPassword(String username, String password) {
String sql = "select id,username,password,email from user where username=? and password=?";
return queryForOne(User.class,sql,username,password);
}
@Override
public int saveUser(User user) {
String sql = "insert into user(username,password,email) values(?,?,?)";
return update(sql,user.getUsername(),user.getPassword(),user.getEmail());
}
}
package com.dd.dao;
import com.dd.pojo.User;
public interface UserDao {
/**
* 通过用户查询用户信息
* @param username
* @return 如果正常返回,说明用户名已存在,如果返回null,说明用户名可用。
*/
public User queryByUsername(String username);
/**
* 通过用户名和密码查询用户信息
* @param username
* @param password
* @return 如果返回正常,说明可以登录,否则登录失败。
*/
public User queryByUserNameAndPassword(String username,String password);
/**
* 保存用户信息
* @param user
* @return 如果返回-1说明保存失败。
*/
public int saveUser(User user);
}
测试登录和注册:
package com.dd.service.impl;
import com.dd.dao.UserDao;
import com.dd.dao.impl.UserDaoImpl;
import com.dd.pojo.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserDao userDao = new UserDaoImpl();
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username != null && password != null ) {
//验证用户名和密码是否正确
User user = userDao.queryByUserNameAndPassword(username, password);
if (user!=null) {
System.out.println("登录成功");
getServletContext().getRequestDispatcher("/pages/user/login_success.html").forward(req,resp);
}else {
System.out.println("登录失败");
getServletContext().getRequestDispatcher("/pages/user/login.html").forward(req,resp);
}
}
}
}
package com.dd.service.impl;
import com.dd.dao.UserDao;
import com.dd.dao.impl.UserDaoImpl;
import com.dd.pojo.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RegisterTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserDao userDao = new UserDaoImpl();
String username = req.getParameter("username");
String password = req.getParameter("password");
String email = req.getParameter("email");
String code = req.getParameter("code");
System.out.println(username);
if (username != null && password != null && email != null && code != null) {
//验证注册码是否正确
if (code.equalsIgnoreCase("abcd")) {
//验证用户名是否可用
if (userDao.queryByUsername(username) == null) {
int resoult = userDao.saveUser(new User(username, password, email));
if (resoult!=-1){
System.out.println("注册成功");
getServletContext().getRequestDispatcher("/pages/user/regist_success.html").forward(req, resp);
}else {
System.out.println("注册失败");
getServletContext().getRequestDispatcher("/pages/user/regist.html").forward(req, resp);
}
} else {
System.out.println("注册失败");
getServletContext().getRequestDispatcher("/pages/user/regist.html").forward(req, resp);
}
} else {
System.out.println("注册失败");
getServletContext().getRequestDispatcher("/pages/user/regist.html").forward(req, resp);
}
}
}
}
web相关代码都在链接里
全部代码分享到这里:
链接:https://pan.baidu.com/s/13fNRMHDnqhr6MuBuhfZlww 提取码:omt0