欢迎来到刘认真的博客

It's not me that's wrong. It's the whole world

SSM项目整合第一步 注册登陆实现

 SSM项目整合第一步  注册:

 

项目目录:

 

 一、数据库建表:

源码:

 1 SET FOREIGN_KEY_CHECKS=0;
 2 
 3 -- ----------------------------
 4 -- Table structure for t_user
 5 -- ----------------------------
 6 DROP TABLE IF EXISTS `t_user`;
 7 CREATE TABLE `t_user` (
 8   `user_id` int(11) NOT NULL AUTO_INCREMENT,
 9   `user_name` varchar(32) NOT NULL,
10   `user_pass` varchar(36) NOT NULL,
11   `user_tel` int(11) DEFAULT NULL,
12   `user_real_name` varchar(32) DEFAULT NULL,
13   PRIMARY KEY (`user_id`)
14 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
15 
16 -- ----------------------------
17 -- Records of t_user
18 -- ----------------------------
19 INSERT INTO `t_user` VALUES ('1', '123', '123456', null, null);
20 INSERT INTO `t_user` VALUES ('2', 'Lucky', 'admin', '123', 'admin');
View Code

 

二、建立实体类映射字段:

 

后端实体类字段映射:

 

已下配置会将字段uesr_id  自动转换为userId  将_i 转换为大写I

 

 applicationContext.xml:

  <!-- 加载mybatis-config配置文件,里面设置别名 -->
         <property name="configLocation" value="classpath:mybatis-config.xml"/>

 

mybatis-config.xml:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6     <settings>
7         <setting name="mapUnderscoreToCamelCase" value="true"/>
8     </settings>
9 </configuration>

 

前端实体类字段映射:

 

 三、处理流程:

 

 

 

 

 

四、前端页面显示:

 

spring表头:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

spring表单:

 

 

 

 错误信息:

 

 1 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <!-- saved from url=(0049)http://localhost:8080/HouseRent/page/register.jsp -->
 4 <HTML xmlns="http://www.w3.org/1999/xhtml">
 5 <HEAD><TITLE>布谷租房 - 用户注册</TITLE>
 6     <META content="text/html; charset=utf-8" http-equiv=Content-Type>
 7     <LINK
 8             rel=stylesheet type=text/css href="../css/style.css">
 9     <META name=GENERATOR content="MSHTML 8.00.7601.17514">
10 </HEAD>
11 <BODY>
12 <DIV id=header class=wrap>
13     <DIV id=logo><IMG src="../images/logo.gif"></DIV>
14 </DIV>
15 <DIV id=regLogin class=wrap>
16     <DIV class=dialog>
17         <DL class=clearfix>
18             <DT>新用户注册</DT>
19             <DD class=past>填写个人信息</DD>
20         </DL>
21         <DIV class=box>
22             <form:form action="/user/doreg" modelAttribute="userError" method="post">
23                 <DIV class=infos>
24                     <TABLE class=field>
25                         <TBODY>
26                         <TR>
27                             <TD class=field>用 户 名:</TD>
28                             <TD><INPUT class=text type=text name=name><form:errors path="name"></form:errors></TD>
29                         </TR>
30                         <TR>
31                             <TD class=field>密  码:</TD>
32                             <TD><INPUT class=text type=password name=password><form:errors path="password"></form:errors></TD>
33                         </TR>
34                         <TR>
35                             <TD class=field>确认密码:</TD>
36                             <TD><INPUT class=text type=password name=repassword></TD>
37                         </TR>
38                         <TR>
39                             <TD class=field>电  话:</TD>
40                             <TD><INPUT class=text type=text name=telephone><form:errors path="telephone"></form:errors></TD>
41                         </TR>
42                         <TR>
43                             <TD class=field>用户姓名:</TD>
44                             <TD><INPUT class=text type=text name=username></TD>
45                         </TR>
46                         </TBODY>
47                     </TABLE>
48                     <DIV class=buttons>
49                         <INPUT value=立即注册 type="submit">
50                     </DIV>
51                 </DIV>
52             </form:form>
53         </DIV>
54     </DIV>
55 </DIV>
56 <DIV id=footer class=wrap>
57     <DL>
58         <DT>布谷租房 © 2010 布谷租房 京ICP证1000001号</DT>
59         <DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD>
60     </DL>
61 </DIV>
62 </BODY>
63 </HTML>
regs.jsp

 

 如果页面没有css样式 此时处理静态文件才会显示:

1  <!--处理静态文件 -->
2     <mvc:resources location="/scripts/" mapping="/scripts/**"/>
3     <mvc:resources location="/css/" mapping="/css/**"/>
4     <mvc:resources location="/images/" mapping="/images/**"/>

 

 

 五、插入乱码解决:

 

WEB-INF/web.xml:

 1  <!-- 设置编码为UTF-8 -->
 2   <filter>
 3     <description>字符集过滤器</description>
 4     <filter-name>encodingFilter</filter-name>
 5     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 6     <init-param>
 7       <description>字符集编码</description>
 8       <param-name>encoding</param-name>
 9       <param-value>UTF-8</param-value>
10     </init-param>
11   </filter>
12   <filter-mapping>
13     <filter-name>encodingFilter</filter-name>
14     <url-pattern>/*</url-pattern>
15   </filter-mapping>

 

mysql.properties:

1 mysql.driverClassName=com.mysql.jdbc.Driver
2 mysql.url=jdbc:mysql://localhost:3310/house?useUnicode=true&characterEncoding=UTF-8
3 mysql.username=root
4 mysql.password=123456

 

登陆:

登陆需要验证账号是否存在(账号密码统一即可)

 

sql语句:

 

登陆逻辑:

 

 

 

 1 package com.etc.controller;
 2 
 3 import com.etc.Vo.UserVO;
 4 import com.etc.entity.User;
 5 import com.etc.servise.UserService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.ui.Model;
 9 import org.springframework.validation.BindingResult;
10 import org.springframework.web.bind.annotation.ModelAttribute;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.SessionAttributes;
13 
14 import javax.validation.Valid;
15 
16 @Controller
17 @RequestMapping("/user")
18 @SessionAttributes(value = {"user"})
19 public class UserController {
20 
21     @Autowired
22     private UserService userService;
23 
24     @RequestMapping("/reg")
25     public String reg() {
26         return "regs";
27     }
28 
29     @RequestMapping("/doreg")
30     public String hello(@ModelAttribute("userError") @Valid UserVO userVO, BindingResult result){
31         if(result.hasErrors()){
32             return "regs";
33         }
34         userService.reg(userVO);
35         return "login";
36     }
37 
38     @RequestMapping("/login")
39     public String login(UserVO userVO, Model model){
40         User god=userService.login(userVO);
41         if(god!=null){
42             model.addAttribute("user",god);
43             return "fabu";
44         }else {
45             return "login";
46         }
47 
48     }
49 
50 
51 }
UserController.java

 

 1 package com.etc.converter;
 2 
 3 import com.etc.Vo.UserVO;
 4 import com.etc.entity.User;
 5 
 6 public class UserConverter {
 7     public static User convert(UserVO userVO) {
 8         User user = new User();
 9         user.setUserName(userVO.getName());
10         user.setUserPass(userVO.getPassword());
11         user.setUserTel(userVO.getTelephone());
12         user.setUserRealName(userVO.getUsername());
13         return user;
14     }
15 }
UserConverter.java

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.etc.dao.UserDAO">
 4     <insert id="reg">
 5         INSERT INTO `t_user` (
 6     `user_name`,
 7     `user_pass`,
 8     `user_tel`,
 9     `user_real_name`
10 )
11 VALUES
12     (#{userName}, #{userPass}, #{userTel}, #{userRealName});
13     </insert>
14 
15     <select id="login" resultType="user">
16         SELECT
17     *
18 FROM
19     t_user
20 WHERE
21     user_name = #{userName}
22 AND user_pass = #{userPass}
23     </select>
24 </mapper>
UserDAO.XML

 

 1 package com.etc.entity;
 2 
 3 public class User {
 4     private int userId;
 5     private String userName;
 6     private String userPass;
 7     private String userTel;
 8     private String userRealName;
 9 
10     public User() {
11     }
12 
13     public User(int userId, String userName, String userPass, String userTel, String userRealName) {
14         this.userId = userId;
15         this.userName = userName;
16         this.userPass = userPass;
17         this.userTel = userTel;
18         this.userRealName = userRealName;
19     }
20 
21     @Override
22     public String toString() {
23         return "User{" +
24                 "userId=" + userId +
25                 ", userName='" + userName + '\'' +
26                 ", userPass='" + userPass + '\'' +
27                 ", userTel='" + userTel + '\'' +
28                 ", userRealName='" + userRealName + '\'' +
29                 '}';
30     }
31 
32     public int getUserId() {
33         return userId;
34     }
35 
36     public void setUserId(int userId) {
37         this.userId = userId;
38     }
39 
40     public String getUserName() {
41         return userName;
42     }
43 
44     public void setUserName(String userName) {
45         this.userName = userName;
46     }
47 
48     public String getUserPass() {
49         return userPass;
50     }
51 
52     public void setUserPass(String userPass) {
53         this.userPass = userPass;
54     }
55 
56     public String getUserTel() {
57         return userTel;
58     }
59 
60     public void setUserTel(String userTel) {
61         this.userTel = userTel;
62     }
63 
64     public String getUserRealName() {
65         return userRealName;
66     }
67 
68     public void setUserRealName(String userRealName) {
69         this.userRealName = userRealName;
70     }
71 }
User.java

 

 1 package com.etc.servise;
 2 
 3 import com.etc.Vo.UserVO;
 4 import com.etc.converter.UserConverter;
 5 import com.etc.dao.UserDAO;
 6 import com.etc.entity.User;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Service;
 9 
10 @Service
11 public class UserService {
12 
13     @Autowired
14     private UserDAO userDAO;
15 
16     public void reg(UserVO userVO){
17         User user= UserConverter.convert(userVO);
18         userDAO.reg(user);
19     }
20 
21     public User login(UserVO userVO) {
22         User user= UserConverter.convert(userVO);
23         User god=userDAO.login(user);
24         return god;
25     }
26 }
UserService.java

 

 1 package com.etc.Vo;
 2 
 3 import org.hibernate.validator.constraints.Length;
 4 import org.hibernate.validator.constraints.NotEmpty;
 5 
 6 import javax.validation.constraints.Pattern;
 7 
 8 public class UserVO {
 9     @NotEmpty(message = "用户名不能为空")
10     @Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9]{3,15}$",message = "请输入3-15位账号,必须以字母开头")
11     private String name;
12     @NotEmpty(message = "用户名不能为空")
13     @Length(min = 8, max = 18, message = "密码必须在8到18位")
14     private String password;
15 
16     private String repassword;
17     @Pattern(regexp = "^1\\d{10}$",message ="请输入正确的手机号" )
18     private String telephone;
19 //    @DateTimeFormat(pattern = "yyyy-MM-dd")
20     private String username;
21 
22     @Override
23     public String toString() {
24         return "UserVO{" +
25                 "name='" + name + '\'' +
26                 ", password='" + password + '\'' +
27                 ", repassword='" + repassword + '\'' +
28                 ", telephone='" + telephone + '\'' +
29                 ", username='" + username + '\'' +
30                 '}';
31     }
32 
33     public UserVO() {
34     }
35 
36     public UserVO(String name, String password, String repassword, String telephone, String username) {
37         this.name = name;
38         this.password = password;
39         this.repassword = repassword;
40         this.telephone = telephone;
41         this.username = username;
42     }
43 
44     public String getName() {
45         return name;
46     }
47 
48     public void setName(String name) {
49         this.name = name;
50     }
51 
52     public String getPassword() {
53         return password;
54     }
55 
56     public void setPassword(String password) {
57         this.password = password;
58     }
59 
60     public String getRepassword() {
61         return repassword;
62     }
63 
64     public void setRepassword(String repassword) {
65         this.repassword = repassword;
66     }
67 
68     public String getTelephone() {
69         return telephone;
70     }
71 
72     public void setTelephone(String telephone) {
73         this.telephone = telephone;
74     }
75 
76     public String getUsername() {
77         return username;
78     }
79 
80     public void setUsername(String username) {
81         this.username = username;
82     }
83 }
UserVO.java

 

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6     <settings>
7         <setting name="mapUnderscoreToCamelCase" value="true"/>
8     </settings>
9 </configuration>
mybatis-config.xml

 

posted @ 2019-07-26 11:48  刘认真  阅读(791)  评论(0编辑  收藏  举报