一、准备jar包
(1)Spring4的所有jar包
(2)hibernate的所有jar包
(3)c3p0的jar包
(4)MySQL的jar包
(5)jackson的所有jar包
二、新建package
这里写图片描述
三、新建db.properties
user=root
password=
jdbcUrl=jdbc:mysql:///ssh
driverClass=com.mysql.jdbc.Driver
四、修改web.xml
五、新建springmvc.xml
六、新建applicationContext.xml
七、类
User.Java
package com.dobeyue.spsh.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Email;import org.hibernate.validator.constraints.NotEmpty;import org.springframework.stereotype.Component;@Table(name="users")
@Entity
@Component
public class User{
private Integer id;
@Email
private String username;
@NotEmpty
private String password;
public User() {
// TODO Auto-generated constructor stub
}
@GeneratedValue
@Id
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;
}
public User(Integer id, String username, String password) {super();
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";}
}
UserDao.java
package com.dobeyue.spsh.dao;
import com.dobeyue.spsh.entities.User;
public interface UserDao {
public User findUser(User user);
}
UserDao.java
package com.dobeyue.spsh.daoImpl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;import com.dobeyue.spsh.dao.UserDao;
import com.dobeyue.spsh.entities.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao {@Autowired
private SessionFactory sessionFactory;
@Transactional(readOnly=true)
public User findUser(User user) {
return (User) sessionFactory.getCurrentSession().createQuery("FROM User u WHERE u.username = ? AND u.password = ? ").setString(0, user.getUsername()).setString(1, user.getPassword()).uniqueResult();}
}
UserHandler.java
package com.dobeyue.spsh.handler;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.dobeyue.spsh.entities.User;
import com.dobeyue.spsh.service.UserService;@Controller("userHandler")
public class UserHandler {
@Autowired
private UserService userService;
@RequestMapping("/index")
public void index(){}
// @ResponseBody
@RequestMapping(value="/doLogin",method=RequestMethod.POST)public String login(@Valid @RequestParam("username") String username,@RequestParam("password") String password,Map
user = userService.findUser(user);
if(user!=null){
map.put("user", user);
model.addAttribute("user", user);
return "success";
}else{
map.put("tip", "帐号或者密码错误");
return "success";
}
}
}
UserService.java
package com.dobeyue.spsh.service;
import com.dobeyue.spsh.entities.User;
public interface UserService {
public User findUser(User user);
}
UserServiceImpl.java
package com.dobeyue.spsh.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.dobeyue.spsh.dao.UserDao;
import com.dobeyue.spsh.entities.User;
import com.dobeyue.spsh.service.UserService;@Service("userService")
public class UserServiceImpl implements UserService {@Autowired
private UserDao userDao;
@Override
public User findUser(User user) {
return userDao.findUser(user);
}
}
八、新建index.jsp
clean下
clean下。。