idea关于hibernate框架的使用
一.mySQL中表的创建
在mySQL中新建一个名为demo的数据库,在该数据库中新建一个customer的客户表
并添加下列测试数据
二.项目创建
然后点击Next,选择好包后创建项目
三.POM.xml文件
首先对环境进行设置:(如下图)
点击File—>Settings—>Build,Execution,Deployment—>Build Tools—>Maven
勾选Always update snapshots
在pom.xml文件中添加下列语句:
添加struts,mysql,hibernate依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-dojo-plugin</artifactId>
<version>2.3.37</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.22.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
添加完之后,点击下图红圈处
对依赖进行刷新
等待jar包的下载
四.Hibernate框架配置
1)首先构建PO类:Customer(此处图中省略了get和set方法)
2)在Resources目录下新建一个名为cn/edu/zjut/po(与上面Cutomer类所在目录同名)的Dictionary,但要注意的是此处是’/’,而不是’.’(cn.edu.zjut.po),否则会找不到该目录
a) 首先是class节点中的name属性表示类名,table属性表示表名,用于类和表对应
b) id节点中name属性表示类的属性,column表示列名,表示类中的“主键”,用于与数据库中的主键对应,若数据库中无主键,在配置文件中也要设置一个唯一标识
c) generator节点class属性值表示主键的生成策略,assigned表示用户赋值,increment表自动递增策略,identity表由数据库根据identity生成主键(数据库要支持identity)
d) property节点name属性表示类的属性,column表示对应的列名
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.edu.zjut.po.Customer" table="customer" catalog="hibernatedb">
<id name="customerId" type="int">
<column name="customerID"/>
<!-- <generator class="assigned"/>-->
<generator class="increment"/>
</id>
<property name="account" type="string">
<column name="account" length="20" unique="true"/>
</property>
<property name="password" type="string">
<column name="password" length="20"/>
</property>
<property name="name" type="string">
<column name="name" length="20"/>
</property>
</class>
</hibernate-mapping>
3)在Resources目录下新建一个名为hibernate.cfg.xml的配置文件
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
//设置数据库的驱动程序,hibernate在连接数据库时需要用到数据库的驱动程序
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai</property>
//设置数据库连接的url
<property name="hibernate.connection.username">root</property>
//连接数据库的用户名
<property name="hibernate.connection.password">********</property>
//连接数据库的密码
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
Hibernate
//使用的数据库语言
<mapping resource="cn/edu/zjut/po/Customer.hbm.xml"/>
//指定映射文件
注意在property name="hibernate.connection.url"属性值中要加上时区信息,不然会出现报错
五.struts框架的配置
首先先写struts.xml文件,注意的是该文件要直接放在Resources目录下,且不是META-INF子目录下
添加过滤器:
在web.xml文件中添加下列代码:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中要注意struts的版本问题,该处用的是struts2.5的版本,2.3的版本< filter-class >要改为
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
六.其余部分的创建
如图为该项目的全部文件:
UserAction:
package cn.edu.zjut.action;
import cn.edu.zjut.po.Customer;
import cn.edu.zjut.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private Customer loginUser;
public Customer getLoginUser() {
return loginUser;
}
public void setLoginUser(Customer loginUser) {
this.loginUser = loginUser;
}
public String login() {
UserService userServ = new UserService();
if (userServ.login(loginUser)) {
return "success";
}
return "fail";
}
}
UserService:
package cn.edu.zjut.service;
import java.util.List;
import cn.edu.zjut.po.Customer;
import cn.edu.zjut.dao.CustomerDAO;
public class UserService {
public boolean login(Customer loginUser) {
String account = loginUser.getAccount();
String password = loginUser.getPassword();
String hql = "from Customer as user where account='" + account + "' and password='" + password + "'";
CustomerDAO dao = new CustomerDAO();
List list = dao.findByHql(hql);
if (list.isEmpty()) return false;
else return true;
}
}
CustomerDAO:
package cn.edu.zjut.dao;
import java.util.List;
import cn.edu.zjut.po.Customer;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CustomerDAO {
private Log log = LogFactory.getLog(CustomerDAO.class);
public List findByHql(String hql) {
log.debug("finding LoginUser instance by hql");
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sf = new MetadataSources(ssr).buildMetadata().buildSessionFactory();
Session session = sf.openSession();
try {
String queryString = hql;
Query queryObject = session.createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by hql failed", re);
throw re;
} finally {
session.close();
}
}
}
七.测试