天天看点

SSH配置(二)-hibernate配置

一、数据库建表

MySql数据库下:

1、创建ssh数据库

2、创建user表,并设置字段uid,name,chname,password

如图:(navicat-mysql数据库界面管理软件)

SSH配置(二)-hibernate配置
SSH配置(二)-hibernate配置

二、Hibernate配置

hibernate包:hibernate-release-4.2.1.Final.zip

1、在WebContent->WEB_INF->lib下导入hibernate所需jar包

      路径:hibernate-release-4.2.1.Final\lib\required

SSH配置(二)-hibernate配置
另:还要添加连接mysql的数据库jar包:mysql-connector-java-5.1.25-bin.jar
SSH配置(二)-hibernate配置
2、在src下添加PO实体类文件
SSH配置(二)-hibernate配置
TaUser.java类代码:
package com.jjh.ssh.model;

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="ta_user")
public class TaUser {
	
	private String uid;
	private String name;
	private String chname;
	private String password;
	
	public TaUser() {
		super();
	}
	public TaUser(String uid, String name, String chname, String password) {
		super();
		this.uid = uid;
		this.name = name;
		this.chname = chname;
		this.password = password;
	}
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "uid", unique = true, nullable = false, length = 40 )
	public String getUid() {
		return uid;
	}
	
	public void setUid(String uid) {
		this.uid = uid;
	}
	
	@Column(name = "name", length = 20)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "chname", length = 20)
	public String getChname() {
		return chname;
	}
	
	public void setChname(String chname) {
		this.chname = chname;
	}
	
	@Column(name = "password", length = 100)
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
}
           
3、在src下添加hibernate.cfg.xml文件
SSH配置(二)-hibernate配置
hibernate.cfg.xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="mysql5">
        <property name="connection.datasource">java:comp/env/jdbc/ssh</property>
  		<property name="show_sql">true</property>
  		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="com.jjh.ssh.model.TaUser" />
    </session-factory>
</hibernate-configuration>
           

4、在tomcat下的context.xml中配置数据源

      路径:apache-tomcat-7.0.40\conf

在context.xml下的<Context></Context>标签中增加如下内容:

<Resource auth="Container" driverClassName="org.gjt.mm.mysql.Driver"
		maxActive="20" maxIdle="5" maxWait="10000" name="jdbc/ssh" password="root"
		type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/ssh"
		username="root" />
           

说明:后面紧接 SSH配置(三)-Spring配置

继续阅读