7.1 JAVA
<mapper namespace="com.zr.dao.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="user">
select * from db_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from db_user
</select>
</mapper>
package com.zr.bean;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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 + '\'' +
'}';
}
}
package com.zr.controller;
import com.zr.bean.User;
import com.zr.dao.UserDao;
import com.zr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView=new ModelAndView();
List<User> users = userService.findAll();
if(flag){
modelAndView.addObject("users", users);
modelAndView.setViewName("../success");
}else {
modelAndView.setViewName("../failure");
}
return modelAndView;
}
}
package com.zr.dao;
import com.zr.bean.User;
import java.util.List;
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll();
}
package com.zr.service.impl;
import com.zr.bean.User;
import com.zr.dao.UserDao;
import com.zr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUserName(username);
List<User> users = userDao.findAll();
if (user!=null && user.getPassword().equals(password)){
return true;
}
return false;
}
public List<User> findAll() {
List<User> users = userDao.findAll();
return users;
}
}
package com.zr.service;
import com.zr.bean.User;
import java.util.List;
public interface IUserService {
boolean login(String username,String password);
List<User> findAll();
}
<body>
<form action="/user/login.do" method="post">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
</body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
</tr>
<c:forEach var="users" items="${users}">
<tr>
<td>${users.id}</td>
<td>${users.username}</td>
<td>${users.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
转载请注明原文地址:https://ipadbbs.8miu.com/read-53042.html