sping mvc 结合 hibernate 实现用户登录功能(二)!
第一步:写个实体类:
package com.spring.entity;
import javax.persistence.Column;
//import javax.persistence.GeneratedValue;
//import javax.persistence.GenerationType;
import javax.persistence.Id;
@javax.persistence.Entity
@javax.persistence.Table(name = "user")
public class User {
@Id
/*@GeneratedValue(strategy=GenerationType.IDENTITY)*/
@Column(name="id")
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
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;
}
}
第二步:Dao层UserDaoImpl :注意(接口未写上,请自己添加)
package com.spring.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.spring.entity.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Transactional
public boolean login(String username, String password) {
// TODO Auto-generated method stub
if(username==null){
username="";
}
if(password==null){
password="";
}
List<User> list = null;
String hql="select user.username ,user.password from User as user where user.username='"+username+"' and user.password = '"+password+"'";
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Query query =session.createQuery(hql);
System.out.println("@@@@@@@@@");
list = query.list();
} catch (DataAccessException e) {
// TODO: handle exception
return false;
}
if(list.isEmpty()){
return false;
}
return true;
}
}
第三步:Service层UserServiceImpl :注意(接口未写上,请自己添加)
package com.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.spring.dao.UserDao;
public class UserServiceImpl implements UserService {
@Autowired
UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
public boolean login(String username, String password) {
// TODO Auto-generated method stub
boolean login = userdao.login(username, password);
if(login){
return true;
}else{
return false;
}
}
}
第四步:写controller:
package com.spring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.spring.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userservice;
@RequestMapping("/welcome.do")
public String login(String username , String password){
System.out.println(username);
if(userservice.login(username, password)){
return "/success";
}else{
return "/error";
}
}
}
至此后台的java代码全部写完。 jsp页面请看:sping mvc 结合 hibernate 实现用户登录功能(三)!