SSH+MySql+BootStrap实现简单用户管理

SSH+MySql+BootStrap实现简单用户管理

本实例用Spring+SpringMVC+Hiberate+MySql+BootStrap实现一个简易的用户管理后台

效果如图:






工程目录:



首先验证管理员登录的接口:

package AdminDao;

public interface AdminDao {
	public String check(String name,String password);
}

验证登录实现类:

package AdminDao;

import java.util.List;

import org.hibernate.SessionFactory;

public class AdminDaoImpl implements AdminDao{
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public String check(String name, String password) {
		String hql = "select admin.name ,admin.password from Admin as admin where admin.name='"+name+"' and admin.password ='"+password+"'";
		org.hibernate.Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List list = query.list(); 
		 if (list.size() > 0) {
			return "ok";
		}
		return "no";
	}
	}

管理员实体类:

package com.admin;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="myadmin")
public class Admin {
	@Id
	@GeneratedValue(generator="system-uuid")
	@GenericGenerator(name = "system-uuid",strategy="uuid")
	@Column(length=32)
	private String id;
	
	@Column(length=32)
	private String name;
	
	@Column(length=32)
	private String password;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

用户实体类:

package com.person;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="mypersons")
public class Person {
	@Id
	@GeneratedValue(generator="system-uuid")
	@GenericGenerator(name = "system-uuid",strategy="uuid")
	@Column(length=32)
	private String id;
	
	@Column(length=32)
	private String name;
	
	@Column(length=32)
	private String age;
	
	@Column(length=32)
	private String address;
	

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

用户操作接口:

package com.PersonDao;

import java.util.List;

import com.person.Person;

public interface PersonDao {
		public String addPerson(Person person);
		public List<Person> getAllPerson();
		public Person getPerson(int id);
		public String delPerson(Person person); 
		public String updatePerson(Person person);
		
}

用户操作实现类:

package com.PersonDao;

import java.util.List;

import javax.management.Query;

import org.hibernate.SessionFactory;

import com.person.Person;

public class PersonDapImpl implements PersonDao{

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public String addPerson(Person person) {
		sessionFactory.getCurrentSession().save(person);
		return "add_OK";
		
			}

	@Override
	public List<Person> getAllPerson() {
		String hql = "from Person";	
		org.hibernate.Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	

	@Override
	public Person getPerson(int id) {
		String hql = "from Person p where p.id=?";
		org.hibernate.Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, id);
		return (Person) query.uniqueResult();
	}

	@Override
	public String delPerson(Person person) {
		sessionFactory.getCurrentSession().delete(person);
		return "del_OK";
	}

	@Override
	public String updatePerson(Person person) {
		String hql = "update Person p set p.name = ?,p.age=?,p.address=? where p.id = ?";
		org.hibernate.Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0,person.getName());
		query.setString(1, person.getAge());
		query.setString(2, person.getAddress());
		query.setString(3, person.getId());
		return "update_OK";
	}
	
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载所有的配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:config/spring/spring-*.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置SpringMVC -->
  <servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>*.jpg</url-pattern>  
</servlet-mapping>  
<servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>*.js</url-pattern>  
</servlet-mapping>  
<servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>*.css</url-pattern>  
</servlet-mapping>  
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:config/spring/spring-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 配置字符集 -->
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Session -->
  <filter>
  	<filter-name>openSession</filter-name>
  	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

spring-beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="personDaoImpl" class="com.PersonDao.PersonDapImpl">
			<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="adminDaoImpl" class="AdminDao.AdminDaoImpl">
			<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
		
	<bean name="adminDao" parent="transactionProxy">
		<property name="target" ref="adminDaoImpl"></property>
	</bean>	
	
		
	<bean name="personDao" parent="transactionProxy">
		<property name="target" ref="personDaoImpl"></property>
	</bean>	
	
	
	
</beans>

spring-common.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="personDaoImpl" class="com.PersonDao.PersonDapImpl">
			<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="adminDaoImpl" class="AdminDao.AdminDaoImpl">
			<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
		
	<bean name="adminDao" parent="transactionProxy">
		<property name="target" ref="adminDaoImpl"></property>
	</bean>	
	
		
	<bean name="personDao" parent="transactionProxy">
		<property name="target" ref="personDaoImpl"></property>
	</bean>	
	
	
	
</beans>

spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.web" />

	<!-- 开启注解 -->
	<mvc:annotation-driven />
	
	<!-- 静态资源(js/image)的访问 -->
	<mvc:resources location="/js/" mapping="/js/**"/>

	<!-- 定义视图解析器 -->	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

登录实现层:

package com.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.admin.Admin;

import AdminDao.AdminDao;

@Controller
@RequestMapping("/manager")
public class Login {

	@Resource
	private AdminDao adminDao;

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

	@RequestMapping("/check")
	public String check(Admin admin,Model model) {
		String string = adminDao.check(admin.getName(),admin.getPassword());
		if (string.equals("ok")) {
			model.addAttribute("admin",admin.getName());
			return "redirect:/manager/getAllPerson";
		}
		  return "redirect:/manager/getAllPerson"; 
	}
	
}

管理实现层:

package com.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.PersonDao.PersonDao;
import com.person.Person;
import com.sun.org.apache.regexp.internal.recompile;




@Controller
@RequestMapping("/manager")
public class Manager {
		
	@Resource
	private PersonDao personDao;

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}
	
	@RequestMapping("/addPerson")
	public String addPerson(Person person) {
		personDao.addPerson(person);
		return "add_OK";
	}
	
	@RequestMapping("/getAllPerson")
	public String getAllPerson(Model model){
		List<Person> persons = personDao.getAllPerson();
		model.addAttribute("lists",persons);
		return "showPerson";
	}
	
	public String delPerson(Person person) {
		personDao.delPerson(person);
		return "index";
	}
	
	
	@RequestMapping("/getPerson")
	public String getPerson(int id,Model model) {
		Person person = personDao.getPerson(id);
		model.addAttribute("Person",person);
		return "detail_person";
	}
	
	public String updatePerson(Person person){
		personDao.updatePerson(person);
		return "";
	}
}

代码下载地址: http://download.csdn.net/detail/coder_py/9813242

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值