1.MD5工具类
package com.ssm.util; import java.security.MessageDigest; public class MD5Util { //将字节数组转成十六进制字符串 private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } //对单个字节转换成整数进行取商取余数计算 private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; //根据下标d1,d2去数组hexDigits的数据 return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) //将加密之后的字节数据转换成16进制的字符串 resultString = byteArrayToHexString(md.digest(resultString .getBytes())); else resultString = byteArrayToHexString(md.digest(resultString .getBytes(charsetname))); } catch (Exception exception) { } return resultString; } private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; }
2.在登录注册中使用MD5加密算法
demo目录结构
数据库用户表
CREATE TABLE `user` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT ‘编号‘, `userName` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT ‘用户名‘, `password` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT ‘密码‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
pojo对象User.java
package com.ssm.pojo; public class User { private Integer id; private String userName; private String password; 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; } }
2.1DAO层
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,
- DAO层的设计首先是设计DAO的接口,
- 然后在Spring的配置文件中定义此接口的实现类,
- 然后就可在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪个类,显得结构非常清晰,
- DAO层的数据源配置,以及有关数据库连接的参数都在Spring的配置文件中进行配置。
UserDaO.java
package com.ssm.dao; import com.ssm.pojo.User; public interface UserDao { //用户注册 public int register(User user) ; //登陆 public User login(User user); }
UserMapper.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"> <mapper namespace="com.ssm.dao.UserDao"> <resultMap type="com.ssm.pojo.User" id="UserResult"> <result property="id" column="id" /> <result property="userName" column="userName" /> <result property="password" column="password" /> </resultMap> <select id="login" parameterType="com.ssm.pojo.User" resultMap="UserResult"> select * from user where userName=#{userName} and password=#{password} limit 1 </select> <insert id="register" parameterType="com.ssm.pojo.User"> insert into user(userName,password) values(#{userName},#{password}) </insert> </mapper>
2.2业务层:Service层
Service层:Service层主要负责业务模块的逻辑应用设计。
- 首先设计接口,再设计其实现的类
- 接着再在Spring的配置文件中配置其实现的关联。这样我们就可以在应用中调用Service接口来进行业务处理。
- Service层的业务实现,具体要调用到已定义的DAO层的接口,
- 封装Service层的业务逻辑有利于通用的业务逻辑的独立性和重复利用性,程序显得非常简洁。
UserService.java
package com.ssm.service.impl; import com.ssm.pojo.User; public interface UserService { public User login(User user); public int register(User user); }
UserServiceImpl.java
package com.ssm.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.ssm.dao.UserDao; import com.ssm.pojo.User; @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public User login(User user) { // TODO Auto-generated method stub return userDao.login(user); } public int register(User user) { // TODO Auto-generated method stub return userDao.register(user); } }
2.3控制层:Controller层
Controller层:Controller层负责具体的业务模块流程的控制,
- 在此层里面要调用Service层的接口来控制业务流程,
- 控制的配置也同样是在Spring的配置文件里面进行,针对具体的业务流程,会有不同的控制器,我们具体的设计过程中可以将流程进行抽象归纳,设计出可以重复利用的子单元流程模块,这样不仅使程序结构变得清晰,也大大减少了代码量。
RegisterController.java
时间: 2024-10-16 20:24:40