目录
1.MVC架构
一种软件架构模式,把整个软件分为三层:
- Model模型——获取数据,并且返回给Controller
- entity:——数据库的实体类
- service(业务):——业务控制层,【其余的活都交给service】
- dao层(数据模型层)——操作数据库
- View视图——看见的页面,渲染数据,渲染页面
- Controller控制器——Servlet,接请求,给响应
耦合度:【代码之间的关联关系】。
为什么要分层?
- MVC
- 降耦合
- 重用性高
- 可维护性强
设计理念——一张表,一个entity,一个service,一个dao,一个controller
使用关系
View层发起请求——Controller——Service——Dao层——Service——Controller——View
Model模型层
实体类(entity)
实体类要求:
- 类名和表名相同
- 类中的属性和表中的字段名相同
- 类中只能有对应的set,get方法和需要用到的构造器,如果有需要,可以写toString
- 需要序列化,实现序列化接口
package com.gyc.morning.entity;
import java.io.Serializable;
public class Vip implements Serializable {
private static final long serialVersionUID = -681512470754667710L;
private Integer id;
private String username;
private String password;
private String name;
private String gender;
private String profile;
private String salt;
public Vip() {
}
public Vip(String username, String password, String name, String gender) {
this.username = username;
this.password = password;
this.name = name;
this.gender = gender;
}
public Vip(Integer id, String username, String password, String name, String gender, String profile) {
this.id = id;
this.username = username;
this.password = password;
this.name = name;
this.gender = gender;
this.profile = profile;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
@Override
public String toString() {
return "Vip{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", profile='" + profile + '\'' +
'}';
}
}
业务层(service)
业务:软件的功能——例如:登录,注册
先创建接口
// 业务层
public interface VipService {
// 注册的方法
int register(Vip vip);
// 登录
Long login(String username,String password);
// 根据用户名查询--盐
String select(String username);
}
实现接口的类
public class VipServiceImpl implements VipService {
private VipDao vipDao=new VipDaoImpl();
// 注册
@Override
public int register(Vip vip) {
try {
String salt = MD5Util.getSalt();
vip.setPassword(MD5Util.stringToMD5(vip.getPassword()+salt));
vip.setSalt(salt);
return vipDao.save(vip);
} catch (Exception e) {
throw new RuntimeException();
}
}
// 登录
@Override
public Long login(String username, String password) {
try {
password=MD5Util.stringToMD5(password+select(username));
return vipDao.login(username,password);
} catch (Exception e) {
throw new RuntimeException();
}
}
// 查询--盐
@Override
public String select(String username) {
try {
return vipDao.select(username);
} catch (Exception e) {
throw new RuntimeException();
}
}
}
数据模型层(dao)
- 执行SQL语句,获取数据
先创建接口
public interface VipDao {
// 注册
int save(Vip vip) throws Exception;
// 登录
Long login(String username,String password) throws Exception;
// 添加
String select(String username) throws Exception;
}
实现接口的类
public class VipDaoImpl extends DAOImpl<Vip> implements VipDao {
@Override
public int save(Vip vip) throws Exception {
// 添加用户名,密码,性别,真实姓名,盐
String sql="insert into vip(username,password,gender,name,salt) values(?,?,?,?,?)";
return update(sql,vip.getUsername(),vip.getPassword(),vip.getGender(),vip.getName(),vip.getSalt());
}
@Override
public Long login(String username, String password) throws Exception {
// 根据用户名和密码查询是否存在
String sql="select count(*) from vip where username=? and password=?";
return getForValue(sql,username,password);
}
@Override
public String select(String username) throws Exception {
// 根据用户名查询---盐
String sql="select salt from vip where username=? ";
return getForValue(sql,username);
}
}
View层
前端页面,用来渲染实现页面的各种功能
代码实例
<!--index.html-->
<body>
<a href="login.html">登录</a>
<a href="register.html">注册</a>
</body>
<!--login.html-->
<body>
<form action="login.do" method="post">
<p>账户: <input type="text" name="username"></p>
<p>密码: <input type="password" name="password"></p>
<input type="submit" value="登录">
</form>
</body>
<!--register.html-->
<body>
<form action="addVip.do" method="post">
<p>账户:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p>姓名:<input type="text" name="name"></p>
<p>
性别:<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女" checked>女
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
</body>
Controller层
用来接受请求,发出响应
// 以.do结尾的所有请求路径
@WebServlet("*.do")
public class VipController extends HttpServlet {
private VipService vipService = new VipServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求的根路径
String servletPath = req.getServletPath();
// 去掉请求根路径前边的/
String substring = servletPath.substring(1);
// 拿到.do前边的字符串
substring = substring.substring(substring.lastIndexOf("/")+1, substring.length() - 3);
try {
// 拿到当前类要执行的方法的对象
Method declaredMethod = getClass().getDeclaredMethod(substring, HttpServletRequest.class, HttpServletResponse.class);
// 让方法去执行
declaredMethod.invoke(this, req, resp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private void addVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String gender = req.getParameter("gender");
Vip vip = new Vip(username, password, name, gender);
vipService.register(vip);
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
Long login = vipService.login(username, password);
// 如果为0,代表用户的账户密码不存在
// 如果大于0,代表用户和密码存在
if (login != 0) {
System.out.println("Success");
} else {
System.out.println("Fail");
resp.sendRedirect("login.html");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
工具类
加密类
public class MD5Util {
// MD5加密
public static String stringToMD5(String str){
return DigestUtils.md5Hex(str.getBytes());
}
// 创建盐
public static String getSalt(){
String words="qwertyuioplkjhgfdsazxcvb1234567890./;'[]-=`!@#$%";
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 8; i++) {
Random random = new Random();
stringBuilder.append(words.charAt(random.nextInt(words.length()-1)));
}
return stringBuilder.toString();
}
}