maven云笔记之注册登录后台模块

8 篇文章 0 订阅

今天我们将利用mms,做一个云笔记项目。
首先,展示项目结构
在这里插入图片描述

  1. 定义用户实体类,并设置getter和setter方法,以及toString方法
public class User implements Serializable{
	private String username;  //用户名
	private String password;//密码
	private String name;//姓名
	private int age; //年龄
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	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;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", name=" + name + ", age=" + age + "]";
	}
}
     
  1. 实体类定义完后,定义一个Dao层接口
public interface UserDao {
    //登录
	public List<User> login(User user);
	
	/**
	 * 查找是否有用户名重复
	 * @param user
	 * @return
	 */
	public List<User> getUserByUsername (User user);
	/**
	 * 用户注册信息
	 */
	public int addUser(User user);	
}
  1. 然后,在定义一个UserService的类
   public interface UserService {
   @Autowired
	//登录
	public User login(User user);
   /**
     * 通过前台传过来的用户名来和数据库中的比较,是否有重复
    * @param user  前台的username
    * @return   返回的数据
    */
	public User getUserByUsername (User user);
	
	/**
	 * 用户注册信息
	 */
	public int addUser(User user);
}
  1. 紧接着定义一个接口,继承UserService,取名为UserImpl
 @Service
public class UserImpl implements UserService{
	@Autowired
	UserDao userDao;
	//登录
	public User login(User user) {
		//System.out.println(user);
		List<User> list=userDao.login(user);
		if(list==null || list.isEmpty()) {
			System.out.println("用户名、密码不匹配");
			return null;
		}else {
			System.out.println(list.get(0));
			return list.get(0);
		}
	}
	//看是否有重复的用户名
	@Override
	public User getUserByUsername(User user) {
	List<User> list = userDao.getUserByUsername(user);
		if(list == null || list.isEmpty()) {
			System.out.println("用户名重复");
			return null;
		}
		return list.get(0);
	}
	@Override
	public int addUser(User user) {
		return userDao.addUser(user);
	}
}
  1. 控制层,取名为UserController
 @Controller //注解,组件扫描加入到spring容器中
@RequestMapping("/user") //类controller的映射路径
@Resource
public class UserController {
	private  Logger logger = LogManager.getLogger(UserController.class);
	
		@Autowired
	UserService userService;
	
//========================登录功能========================================//

	@RequestMapping("/login") //方法的映射路径
	@ResponseBody	//将返回信息输送到网页端
	public Object login(User user) {
		logger.info("登录:"+user.getUsername());
		try {
			user.setPassword(MD5.md5(user.getPassword(), "nihao"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(user.toString());
		User userResult = userService.login(user);
		JsonResult jsonResult;
		if(userResult!= null) {
			jsonResult = new JsonResult(JsonResult.STATE_SUCCESS,"成功",userResult);
		}else {
			jsonResult = new JsonResult(JsonResult.STATE_ERROR,"用户名或密码错误",null);

		}
		//person.say();
		return jsonResult;

	}
//========================注册功能========================================//
	
	@RequestMapping("/register")
	@ResponseBody
	public Object zhuce(User user) {
		
		System.out.println(user);
		User userResultByusername = userService.getUserByUsername(user);
		try {
			user.setPassword(MD5.md5(user.getPassword(), "nihao"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JsonResult jsonResult = null;
		//第一步,判断用户名是否重复,
		if(userResultByusername == null) {
			
			//第二步,如果不重复,就可以注册信息
		int userResult = userService.addUser(user);
		if(userResult == 1) {
			jsonResult = new JsonResult(JsonResult.STATE_SUCCESS,"注册成功",null);
		}else {
			jsonResult = new JsonResult(JsonResult.STATE_ERROR,"注册失败",null);
		      }
		
		}else {
			jsonResult = new JsonResult(JsonResult.STATE_ERROR,"用户名重复",null);
		}
		return jsonResult;
	}
}

6.不要捉急,先歇一下,解释一下

  • MD5的演讲
    在注册功能上,密码采用了MD5加密方式,保证了用户密码安全性,那他是如何工作的,简单说一下,

    首先,定义一个MD5类,该类中实现加密方法

     public class MD5 {
	// 带秘钥加密
		public static String md5(String text, String key) throws Exception {
			// 加密后的字符串
			String md5str = DigestUtils.md5Hex(text + key);
			return md5str;
		}
}

然后,在控制层注册方法中引用该方法

//其中的‘nihao’为盐值,也就是Key ,这是为了密码的安全性着想,
user.setPassword(MD5.md5(user.getPassword(), "nihao"));

7.注册的信息要保存在数据中,所以项目必须和数据库有所连接,以便传送数据
8.在src/main/resource定义一个名为conf的文件夹,下面写一个名为jdbc.properties 文件

 driver=com.mysql.cj.jdbc.Driver
  url=jdbc:mysql://localhost:3306/noteserver?characterEncoding=utf-     8&serverTimezone=UTC
  username=root
  password=root

9.接着,在写一个名为spring-mybaits.xml

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	<!-- 读取配置文件 -->
	<util:properties id="jdbc" location="classpath:conf/jdbc.properties"></util:properties>
	<!-- 配置数据库连接池 -->
	<bean id="dataSouce" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="#{jdbc.driver}"></property>
		<property name="password" value="#{jdbc.password}"></property>
		<property name="url" value="#{jdbc.url}"></property>
		<property name="username" value="#{jdbc.username}"></property>
		
	</bean>
	
	<!-- 创建sqlsession会话,用于操作数据库 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSouce"></property>
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>
	
	<!-- 配置Mapper接口组件扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.whc.noteserver.dao"></property>
	</bean>
</beans>

10.不要忘了spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	 
<!-- 	<bean id="date" class="java.util.Date"></bean>
	<bean id="person" class="com.whc.noteserver.test.Person">
		name就是person里的fanDian ref就是指向spring-servie.xml中id为fanDian的bean
		<property name="fanDian" ref="fanDian"></property>
	</bean> -->
	
<!-- 	<context:component-scan base-package="com.whc.noteserver.test"></context:component-scan> -->
	<bean id="person" class="com.whc.noteserver.test.Person">
		<property name="personname" value="zhangsan"></property>
	</bean>
	
	<!-- 包扫描 -->
	<context:component-scan base-package="com.whc.noteserver"></context:component-scan>
	<!-- 启动注解版的Spring MVC -->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

11.与conf文件夹同级,取名为mapper,用于sql语句,取名为UseMapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
 
<mapper namespace="com.whc.noteserver.dao.UserDao">
	<select id="login" parameterType="com.whc.noteserver.entity.User" resultType="com.whc.noteserver.entity.User">
		select * from note_t_users where username=#{username} and password=#{password}
	</select>
	
	<select id="getUserByUsername" parameterType="com.whc.noteserver.entity.User" resultType="com.whc.noteserver.entity.User">
		select * from note_t_users where username=#{username} 
	</select>
	
	<insert id="addUser" parameterType="com.whc.noteserver.entity.User" >
	   insert into note_t_users (username,password,name,age) values (#{username},#{password},#{name},#{age})
	</insert>
</mapper>

12.JsonResult 文件

public class JsonResult implements Serializable{
	      /**
	 * 
	 */
	private static final long serialVersionUID = -8479395982393447848L;
	public static String STATE_SUCCESS = "1";
	public static String STATE_ERROR = "-1";
		//状态
         private String state;
         //信息
         private String message;
         //数据
         private Object data;
         
         public JsonResult (String state,String message,Object data) {
        	 this.data = data;
        	 this.message = message;
        	 this.state = state;
         }

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		@Override
		public String toString() {
			return "JsonResult [state=" + state + ", message=" + message + ", data=" + data + "]";
		}
         
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值