java学习记录(8)
ssh工程实践之struts2&hibernate篇,ssh采用的版本分别是(struts-2.3.16.1, spring-framework-4.3.9.RELEASE, hibernate-release-5.2.10.Final)。从基础使用的角度来指示stuts2的使用。工程将从三个部分进行集成struts2, struts2和hibernate,struts2,hibernate和spring最终集成。
本篇是SSH集成系列记录的第二篇,在上一章中记录了struts2工程的集成。主要是一个简单的表单提交,然后后台判断给出返回的view。本篇是将struts2和hibernate集成起来访问数据库。
行文结构
- hibernate是什么
- hibernate 集成所需jar包。
- 工程目录结构及相关代码
- 代码下载路径
1 hibernate是ORM的实现,将关系型数据库的操作转化为对对象的操作。将一个数据库的表作为一个实体类,数据库表格之前的一些关系一对一,一对多和多对多也存在与实体类之间的关系中。
2 jar作用,下载hibernate-release-5.2.10.Final版本。其中jar包被分成个几个文件夹,而我们需要的是lib下面的required目录下的jar包。如果涉及到数据库连接池的运用则还需要使用optional/c3p0下载jar包。
3 工程目录与上个struts2工程的类似。
4 代码部分;
该实例主要是配置从页面接收登录的参数,并判断数据库中是否有相应的人员,从而定位到相应的结果页面中。
中间涉及①到实体类的配置,②Hibernate.cfg.xml文件的配置。④数据库的配置。
①配置实例类
package com.ziling.bean;
public class User {
private String username;
private String password;
private int id;
private int usertype;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public int getUsertype() {
return usertype;
}
public void setUsertype(int usertype) {
this.usertype = usertype;
}
public String toString() {
return this.username + " " +this.password;
}
}
hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.ziling.bean">
<class name="User" table="t_user">
<id name="id" column="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username" type="java.lang.String"
not-null="true" length="20"></property>
<property name="password" column="password" type="java.lang.String"
not-null="true" length="20" />
<property name="usertype" column="usertype" type="java.lang.Integer"
/>
</class>
</hibernate-mapping>
②数据库的配置及hibernate配置
首先需要自己安装好数据库。
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">ziling</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/ziling/bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
③数据库的连接及访问
package com.ziling.daoImpl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.ziling.bean.User;
import com.ziling.dao.LoginDao;
public class LoginDaoImpl implements LoginDao {
User user;
//hibernate读取对象所定义的对象
private static final String CFG_FILE_LOCATION = "/Hibernate.cfg.xml";
Configuration cfg;
StandardServiceRegistryBuilder serviceRegistry;
SessionFactory sessionFactory;
Session session;
Transaction ts;
@SuppressWarnings("unchecked")
@Override
public User getUser(String username) {
user = new User();
//用hibernate从数据库获取user
cfg = new Configuration().configure(CFG_FILE_LOCATION).addClass(com.ziling.bean.User.class);
//5.*以前用ServiceRegistryBuilder
//5.*以后用StandardServiceRegistryBuilder
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(serviceRegistry.build());
session = sessionFactory.openSession();
ts = session.beginTransaction();
//hql查询
String hql = "from User";
@SuppressWarnings({"rawtypes"})
List result = session.createQuery(hql).list();
for(User user2 : (List<User>)result){
System.out.println(user2.toString());
if(username.equals(user2.getUsername())){
user.setUsername(user2.getUsername());
user.setPassword(user2.getPassword());
}
}
//测试用
System.out.println("可以连接数据库");
ts.commit();
session.close();
if(user.getUsername()!=null){
return user;
}else{
return new User();
}
}
}
4 代码下载路径
代码下载路径