1.右击main->new->Directory,命名为java,右击main->new->Directory,命名为resources
2.点中java右击,->mark Directory as->Sourse root,只是java文件夹就会变成蓝色。右击java文件夹->package,命名为com.zhuangruan.web_demo.。在这个文件夹下新建 4个包,分别为controller,dao,entity,service
方法:然后选中这个文件,右击->new->package
3.右击resources,->mark Directory as->Resources root,在resources下新建3个package文件,分别命名为mapper,properties,spring。方法同上。
4..1在entity下新建一个User(方法:new->java class-> 命名)代码:
package com.zhongruan.web_demo.entity;
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
4.2在dao下新建IUserDao(方法:new->java class)代码如下
package com.zhongruan.web_demo.dao;
import com.zhongruan.web_demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
public interface IUserDao {
ListgetUsers();
User getUserByName(@Param("name") String username,@Param("pwd") String password);
}
5.在mapper下新建UserMapper.xml文件(方法:右击->new->file)代码如下
select * from tb_user where username = #{name}and password=#{pwd}
6.在properties下新建db.properties(方法:右击->new->file)代码如下
(记得修改数据库名字people_manage,密码password)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/people_manage?characterEncoding=utf8
jdbc.username=root
jdbc.password=12345
7.在spring下新建applicationContext.xml文件(方法:右击->new->file)
8.在spring下新建spring-mvc.xml文件(方法:右击->new->file)
9.在webapp下WEB-INF下新建一个jsp文件(方法:右击->new->file),右击jsp->new->JSP/JSP命名为login,同样再新建一个index和taglibs
***taglibs.jsp下的代码***
***login.jsp下的代码***
${basePath}
账号:
密码:
${msg}
index.jsp下的代码
恭喜你登陆成功
10.pom.xml下代码(直接复制就好)
4.0.0
com.zhuangruan
web_demo
1.0-SNAPSHOT
war
web_demo Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.mchange
c3p0
${c3p0.version}
taglibs
standard
${taglibs.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
11.在controller下新建一个UserController(方法:new->java class-> 命名)代码如下
package com.zhongruan.web_demo.controller;
import com.zhongruan.web_demo.dao.IUserDao;
import com.zhongruan.web_demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class UserController {
@Autowired
IUserDao userDao;
@RequestMapping("/logintest")
public String loginTest(Model model,@RequestParam("username") String name,@RequestParam("password") String password) {
User user = userDao.getUserByName(name,password);
if (user != null) {
model.addAttribute("msg", "不存在该用户");
return "login";
}
if (user.getPassword()!=password) {
model.addAttribute("msg", "账号或密码错误");
return "login";
}
return "index";
}
发布的方式可以参考一下这篇博客。https://blog.csdn.net/weixin_43801836/article/details/94054693
创建的数据库名为people_manage,表名为tb_user,表内有三列,id,username,password