最近的工作需求改了又改,麻烦的狠,就暂停更博,之前一周写的这个框架搭建完成,有点心得,记录一下
之前网上有搭建的这个框架但是是用eclipse,我们今天就用myeclipse搭建一下框架
原文章eclipse搭建链接地址
说明
1.搭建SpringMVC+spring+hibernate的框架,项目结构如图1所示
引用的jar包为Spring3.2.6和Hibernate4,如图2所示
com.lq.entity
org.hibernate.dialect.SQLServerDialect
create
true
true
org.springframework.orm.hibernate4.SpringSessionContext
Spring-MVC-model
login.jsp
contextConfigLocation
classpath*:config/spring/spring-*.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:config/spring/spring-mvc.xml
1
springMVC
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
openSession
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSession
/*
package com.lq.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* User1 entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "User1", schema = "dbo", catalog = "test")
public class User1 implements java.io.Serializable {
// Fields
private Integer id;
private String age;
private String userName;
private String password;
// Constructors
/** default constructor */
public User1() {
}
/** full constructor */
public User1(String age, String userName, String password) {
this.age = age;
this.userName = userName;
this.password = password;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "age", length = 50)
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
@Column(name = "userName", length = 50)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "password", length = 50)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.lq.dao;
import java.util.List;
import com.lq.entity.User1;
public interface UserDao {
public User1 getUser(int id);
public List getAllUser();
public void addUser(User1 user);
public boolean delUser(int id);
public boolean updateUser(User1 user);
public List login(String userName,String password);
}
package com.lq.dao;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.lq.entity.User1;
//注入
@Repository
public class UserDaoImpl implements UserDao {
//注入已在spring-common.xml中配制好的sessionFactory
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Override
public User1 getUser(int id) {
String hql="from User1 u where u.id=?";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger(0,id);
return (User1) query.uniqueResult();
}
@Override
public List getAllUser() {
String hql="from User1";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
}
@Override
public void addUser(User1 user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public boolean delUser(int id) {
String hql="delete User1 u where u.id=?";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger(0, id);
return (query.executeUpdate()>0);
}
@Override
public boolean updateUser(User1 user) {
String hql="update User1 u set u.userName=?,u.age=? where u.id=?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, user.getUserName());
query.setString(1, user.getAge());
query.setInteger(2, user.getId());
return (query.executeUpdate()>0);
}
@Override
public List login(String userName,String password) {
String hql="from User1 u where u.userName=? and u.password=?";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, userName);
query.setString(1, password);
return query.list();
}
}
package com.lq.service;
import java.util.List;
import com.lq.entity.User1;
public interface UserService {
//查询id
public User1 getUser(int id);
//查询所有user表
public List getAllUser();
//添加user表
public void addUser(User1 user);
//删除一个id
public boolean delUser(int id);
//更新user表
public boolean updateUser(User1 user);
public List login(String userName,String password);
}
package com.lq.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lq.dao.CustomerDao;
import com.lq.dao.UserDao;
import com.lq.entity.User1;
//注入service
@Service
public class UserServiceImpl implements UserService{
//注入Dao
@Autowired
private UserDao userDao;
@Autowired
private CustomerDao customerDao;
@Override
public User1 getUser(int id) {
return userDao.getUser(id);
}
@Override
public List getAllUser() {
return userDao.getAllUser();
}
@Override
public void addUser(User1 user) {
userDao.addUser(user);
}
@Override
public boolean delUser(int id) {
return userDao.delUser(id);
}
@Override
public boolean updateUser(User1 user) {
return userDao.updateUser(user);
}
@Override
public List login(String userName, String password) {
return userDao.login(userName, password);
}
}
package com.lq.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lq.entity.User1;
import com.lq.service.UserService;
//注入controller
@Controller
@RequestMapping("/login")
public class loginController {
// 注入service
@Autowired
private UserService userService;
@RequestMapping("/cheklogin")
public void cheklogin(String username, String password,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
List user1s = userService.login(username, password);
if (user1s.size() > 0) {
HttpSession session=request.getSession();
session.setAttribute("user","1");
out.print("1");
} else {
out.print("0");
}
}
}
package com.lq.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lq.entity.User1;
import com.lq.service.CustomarService;
import com.lq.service.UserService;
//注入controller
@Controller
@RequestMapping("/df")
public class UserController {
//注入service
@Autowired
private UserService userService;
@Autowired
private CustomarService customerService;
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request){
request.setAttribute("userList", userService.getAllUser());
return "/selectUser/index";
}
@RequestMapping("/getUser")
public String getUser(int id, HttpServletRequest request) {
request.setAttribute("user", userService.delUser(id));
return "/editUser";
}
@RequestMapping("/toAddUser")
public String toAddUser() {
return "/addUser";
}
@RequestMapping("/addUser")
public String addUser(User1 user, HttpServletRequest request) {
try {
userService.addUser(user);
return "redirect:/user/getAllUser";
} catch (Exception e) {
return "/error";
}
}
@RequestMapping("/delUser")
public void delUser(int id, HttpServletResponse response) {
String result = "{\"result\":\"error\"}";
if (userService.delUser(id)) {
result = "{\"result\":\"success\"}";
}
response.setContentType("application/json");
try {
PrintWriter out = response.getWriter();
out.write(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/updateUser")
public String updateUser(User1 user, HttpServletRequest request) {
if (userService.updateUser(user)) {
user = userService.getUser(user.getId());
request.setAttribute("user", user);
return "redirect:/user/getAllUser";
} else {
return "/error";
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
管理员登陆
用户名:
密 码:
<%-- --%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
Welcome, Admin
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam eget ligula eu lectus lobortis condimentum. Aliquam nonummy auctor massa. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Pending Users Approve or Reject
ID
Name
E-mail
Phone
City
Role
Status
Actions
564
John S. Schwab
[email protected]
402-xxx-xxxx
Bassett, NE
User
Inactive
Approve Reject
565
Juliana M. Sheffield
[email protected]
803-xxx-xxxx
Columbia, SC
User
Inactive
Approve Reject