概述
package com_dao;
import com_entity.TUesrEntity;
import com_utils.HibernateUtils;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import sun.net.www.protocol.http.HttpURLConnection;
import java.util.List;
public class LoginDao {
public TUesrEntity get() {
Configuration config = new Configuration();
config.configure("/hibernate.cfg.xml");
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Query query=session.createQuery("from TUesrEntity ");
TUesrEntity tUesrEntity= (TUesrEntity) query.uniqueResult();
return
tUesrEntity;
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/myblog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false</property>
</session-factory>
</hibernate-configuration>
import com_dao.LoginDao;
import com_entity.TUesrEntity;
import org.junit.Test;
import java.util.List;
public class testLoginDao {
@Test
public void test(){
LoginDao loginDao=new LoginDao();
TUesrEntity tUesrEntities=loginDao.get();
System.out.println(tUesrEntities.getUserName());
}
}
最后
以上就是拼搏裙子为你收集整理的hibernate连接MySQL的全部内容,希望文章能够帮你解决hibernate连接MySQL所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复