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
com.mysql.jdbc.Driver
2.修改Teams.hbm.xml数据库表映射配置文件
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
3.创建实体Bean(映射类)
(1)Teams 类
package com.cimstech.test;(2)TeamsId 类
package com.cimstech.test;(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;
}
}