hibernate框架自动创建数据库表格

1.修改hibernate.cfg.xml配置文件


          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">





org.hibernate.dialect.MySQLDialect


jdbc:mysql://127.0.0.1:6789/ucm

root
admin

com.mysql.jdbc.Driver

SQLNAME
org.hibernate.dialect.MySQLDialect
 

create



2.修改Teams.hbm.xml数据库表映射配置文件


"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

  

   
       
           
               
           

           
               
           

       

   
    


3.创建实体Bean(映射类)

(1)Teams 类

package com.cimstech.test;


public class Teams implements java.io.Serializable {


// Fields


private TeamsId id;
// Constructors


/** default constructor */
public Teams() {
}


/** full constructor */
public Teams(TeamsId id) {
this.id = id;
}


// Property accessors


public TeamsId getId() {
return this.id;
}


public void setId(TeamsId id) {
this.id = id;
}


}


(2)TeamsId 类

package com.cimstech.test;


public class TeamsId implements java.io.Serializable {


// Fields


private String player;
private String team;


// Constructors


/** default constructor */
public TeamsId() {
}


/** full constructor */
public TeamsId(String player, String team) {
this.player = player;
this.team = team;
}


// Property accessors


public String getPlayer() {
return this.player;
}


public void setPlayer(String player) {
this.player = player;
}


public String getTeam() {
return this.team;
}


public void setTeam(String team) {
this.team = team;
}


public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TeamsId))
return false;
TeamsId castOther = (TeamsId) other;


return ((this.getPlayer() == castOther.getPlayer()) || (this
.getPlayer() != null && castOther.getPlayer() != null && this
.getPlayer().equals(castOther.getPlayer())))
&& ((this.getTeam() == castOther.getTeam()) || (this.getTeam() != null
&& castOther.getTeam() != null && this.getTeam()
.equals(castOther.getTeam())));
}


public int hashCode() {
int result = 17;


result = 37 * result
+ (getPlayer() == null ? 0 : this.getPlayer().hashCode());
result = 37 * result
+ (getTeam() == null ? 0 : this.getTeam().hashCode());
return result;
}


}


(3)TeamsDAO


package com.cimstech.test;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TeamsDAO extends BaseHibernateDAO {
private static final Logger log = LoggerFactory.getLogger(TeamsDAO.class);


// property constants


public void save(Teams transientInstance) {
log.debug("saving Teams instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}


public void delete(Teams persistentInstance) {
log.debug("deleting Teams instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}


public Teams findById(com.cimstech.test.TeamsId id) {
log.debug("getting Teams instance with id: " + id);
try {
Teams instance = (Teams) getSession().get(
"com.cimstech.test.Teams", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(Teams instance) {
log.debug("finding Teams instance by example");
try {
List results = getSession()
.createCriteria("com.cimstech.test.Teams")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}


public List findByProperty(String propertyName, Object value) {
log.debug("finding Teams instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Teams as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}


public List findAll() {
log.debug("finding all Teams instances");
try {
String queryString = "from Teams";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}


public Teams merge(Teams detachedInstance) {
log.debug("merging Teams instance");
try {
Teams result = (Teams) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}


public void attachDirty(Teams instance) {
log.debug("attaching dirty Teams instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}


public void attachClean(Teams instance) {
log.debug("attaching clean Teams instance");
try {
getSession().buildLockRequest(LockOptions.NONE).lock(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}

4.执行一个查询数据的操作即可

public void test2() throws Exception 
{
try
{

Session session = HibernateSessionFactory.getSession();

String sql = "select * from Teams";
List list = session.createSQLQuery(sql).
addScalar("team", StandardBasicTypes.STRING)
.addScalar("player",StandardBasicTypes.STRING).list();
Iterator iter = list.iterator();
while(iter.hasNext())
{
Object[] objs =(Object[])iter.next();
System.out.println("team="+objs[0]);
System.out.println("player="+objs[1]);
System.out.println("--------------------------");
}
}
catch(Exception e)
{
throw e;
}
}

你可能感兴趣的:(hibernate)