版权声明:Dream_dog专属 https://blog.csdn.net/Dog_dream/article/details/83180936
首先导入struts+Hibernate 的jar包
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2_002</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/user/login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- package name="student"和java的包一样,命名唯一,将要执行的类同一进行管理 ,可以有多个包 -->
<!-- namespace="/"代表命名空间为根空间;命名空间就是我们在发起一个请求的时候输入的地址
比如:http://localhost:8080/web_Struts2/user.action;如果是"/"为根空间;
如果为http://localhost:8080/web_Struts2/随意一个名字/user.action;
那么namespace="随意一个名字"
-->
<!-- ction name="user" user和servlet类似有一个名称,用于调用时候输入地址;后缀默认为.action!! 且后缀名任意更改在struts.properties文档中可任意更改名字 -->
<!-- class="com.ygr.struts.action.UserAction"所在的包地址,和在xml文档中配置servlet一样 -->
<!-- method="add" 这里如果不写,默认执行的方法就是execute();如果写了就是执行所写的方法,这里就是执行add方法 -->
<!-- result name="success" 这里和对应的java,action类调用的方法相对应-->
<constant name="struts.action.excludePattern" value="/static/.*?" /><!-- 对静态资源的放行 /static 代表静态资源的存放路径 -->
<package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" class="com.dream.action.LoginAction" method="execute">
<result name="success">/WEB-INF/jsp/user/home.jsp</result>
<result name="error">/WEB-INF/jsp/user/error.jsp</result>
</action>
</package>
</struts>
配置hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 数据库连接池的大小 -->
<property name="connection.pool_size">5</property>
<!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
<property name="jdbc.fetch_size">50 </property>
<!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
<property name="jdbc.batch_size">23 </property>
<!-- SQL 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 在控制台输出sql语句 -->
<property name="show_sql">true</property>
<!-- 在启动时根据配置更新数据库 -->
<property name="hbm2ddl.auto">update</property>
<!--指定映射文件为“hibernate/ch1/UserInfo.hbm.xml”-->
<mapping resource="com/test/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
配置 User.hbm.xml 这个映射文件要放到对应的映射类的包下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.domain">
<!-- 1 配置类和表对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="User" table="user">
<!-- 2 配置“主键”的映射
id标签
name属性:实体类里面id属性名称
column属性:生成的表字段名称
type属性:该字段的数据类型
-->
<id name="id" column="id" type="java.lang.Integer">
<!-- 设置主键的增长方法
increment(递增)
identity (标识)
sequence (序列)
hilo (高低位)
seqhilo(使用序列的高低位)
native(本地)
-->
<generator class="increment"></generator>
</id>
<!-- 配置其他属性和表字段对应
name属性:实体类属性名称
column属性:生成表字段名称
type属性:该字段的数据类型
-->
<property name="username" column="name"></property>
<property name="password" column="password"></property>
<property name="date" column="date"></property>
</class>
</hibernate-mapping>
UserData.java 用户数据的提取类与hbm.xml放在同一个包下
package com.dream.data;
import java.util.Date;
public class UserData {
private Integer id;
private String password;
private String username;
private Date date;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
UserDao.java 从数据库提取数据进行比对的类
package com.dream.dao;
import org.hibernate.Query;
import org.hibernate.Session;
public class UserDao {
public boolean login(String username,String possword) {//查找用户是否存在
Session session=new HibernateUtls().getSession();
Query query=session.createQuery("from UserData u where u.username=? and u.password=?");
query.setParameter(0, username);
query.setParameter(1, possword);
return query.list().isEmpty()?false:true;
}
}
HibernateUtls.java打开数据库会回话返回session
package com.dream.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtls {
public Session getSession() {
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;
try {
cfg=new Configuration().configure();
ServiceRegistry registry=new ServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.buildServiceRegistry();
sf=cfg.buildSessionFactory(registry);
session=sf.openSession();
}catch(Exception e){
e.printStackTrace();
}
return session;
}
public void closeSession(Session session)
{
if(session!=null&&session.isOpen()){
session.close();
}
}
}
数据库的建立:
只是初步的入门理解:完整项目点击下载