Struts2+Hibernate JPA+MariaDB数据库实现登录功能

Struts2+Hibernate JPA+MariaDB数据库实现登录功能



MariaDB和MySQL数据库最大的不同就是:

     驱动不同。

     mysql驱动:mysql-connector-java-commercial-5.1.25-bin.jar

     MariaDB驱动:mariadb-java-client-1.1.3.jar

    剩下的完全一样



项目源码链接:



了解下项目大概部署:

Struts2+Hibernate JPA+MariaDB数据库实现登录功能_第1张图片


再了解导入的JAR:


首页index.jsp:


代码如下:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	version="2.4"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<!-- 配置struts2的过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    
	<persistence-unit name="qs" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<class>com.bean.User</class>
  		<properties>
  			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
			<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
			<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/TestMariaDB"/>
			<property name = "hibernate.connection.username" value = "root"/>
			<property name="hibernate.connection.password" value="123456" />  
			<property name="hibernate.show_sql" value="true" />  
			<property name="hibernate.format_sql" value="true"/>
    		<property name="hibernate.hbm2ddl.auto" value="update"/>  
	  	</properties>
	</persistence-unit>
  
</persistence>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.i18n.encoding" value="gbk" />
	<constant name="struts.devMode" value="true" />
	<!--Struts2中Action对象的创建交给spring完成 -->
	<package name="name" extends="struts-default">
		<action name="login" class="com.action.loginAction" method="Login" >
		    <result name="success">success.jsp</result>
		    <result name="fail">fail.jsp</result>
		</action>
	</package>



</struts>

EntityManagerFactorys.java

package com.hibernate.Util;

import javax.persistence.*;
import javax.persistence.Persistence;

public class EntityManagerFactorys {

	public static EntityManagerFactory emf = Persistence
			.createEntityManagerFactory("qs");

	public static EntityManager getEntityManager() {
		EntityManager em = emf.createEntityManager();

		return em;
	}
}

User.java
package com.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String username;
	private String password;

	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;
	}

}

loginAction.java

package com.action;

import java.util.List;

import com.bean.User;
import com.dao.LoginDao;

public class loginAction {
	
	private User user = new User();
	

	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	public String Login() {
		LoginDao login = new LoginDao();
		if (login.adminLogin(user) > 0)
			return "success";
		else
			return "fail";
	}
}

LoginDao.java

package com.dao;

import java.sql.ResultSet;
import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Query;

import com.bean.User;
import com.hibernate.Util.EntityManagerFactorys;

public class LoginDao {

	//登录
	public int adminLogin(User user) {
		EntityManager em = EntityManagerFactorys.getEntityManager();
		int flag = 0;
		try {
			em.getTransaction().begin();
		//	System.out.println("--------2---------");
			String sql = "select id from user where username =:username  and password=?1 ";
			flag = (Integer) em.createNativeQuery(sql)
					.setParameter(1, user.getPassword())
					.setParameter("username", user.getUsername())
					.getSingleResult();
			//System.out.println("++++++++++3+++++++++");
			em.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			em.getTransaction().rollback();
		}
		return flag;
	}
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<table align="center">
		<form action="login">
			用户名:<input type="text" name="user.username"><br /> 密码:<input
				type="password" name="user.password"><br /> <input
				type="submit" value="提交"> <input type="reset" value="重置">
		</form>
	</table>
</body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   welcome to Struts +JPA+MySQL
  </body>
</html>


fail.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   welcome to Struts +JPA+MySQL
  </body>
</html>

sql表中的数据:

Struts2+Hibernate JPA+MariaDB数据库实现登录功能_第2张图片


首页:


登录成功页面:


登录失败:


Struts2+Hibernate JPA+MariaDB数据库实现登录功能_第3张图片



你可能感兴趣的:(Hibernate,struts,DB,jpa,maria)