通过hibernate访问postgreSQL的搭建过程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了通过hibernate访问postgreSQL的搭建过程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3940字,纯文字阅读大概需要6分钟。
内容图文
![通过hibernate访问postgreSQL的搭建过程](/upload/InfoBanner/zyjiaocheng/480/3ac45212c4774602a03771607e69d406.jpg)
postgreSQL jdbc jar包引入 在pom.xml中写依赖:
<dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901-1.jdbc4</version> </dependency>
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "src/resource/schema/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <!-- Database connection settings --> <property name="connection.driver_class"> org.postgresql.Driver </property> <property name="connection.url"> jdbc:postgresql://10.21.132.19:5432/test </property> <property name="connection.username">postgres</property> <property name="connection.password">88075998</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect"> org.hibernate.dialect.PostgreSQLDialect </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">false</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <!-- <mapping resource="com/hik/gss/sys/domain/User.hbm.xml" />--> <mapping class="model.User"></mapping> </session-factory> </hibernate-configuration>
- 实体类的定义,如下:
package model; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; @Entity @Table(name="public.user") public class User { private Integer userId; private String userName; private String passWord; @Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") @Column(name = "user_id") public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } @Column(name = "username") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name = "password") public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
注解说明:@Entity实体类标注, @Table标注表名,注意表名前面要写模式名(public),被坑过. @Id 表示主键 @Column列名 - 查询数据库代码Demo:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() .build(); SessionFactory sessionFactory = null; try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } Session session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from model.User" ).list(); for ( User user : (List<User>) result ) { System.out.println( "User (" + user.getUserName() + ") : " + user.getPassWord() ); if (this.passWord.equals(user.getPassWord()) && this.userName.equals(user.getUserName())) { return "SUCCESS"; } } session.getTransaction().commit(); session.close();
总结一下查询的步骤:- 注册
- 创建会话工厂
- 会话工厂生产会话
- 创建查询语句
- 会话执行查询语句
- 获取结果
通过hibernate访问postgreSQL的搭建过程
标签:dea lis 文件 startup exce resources org depend 查询语句
本文系统来源:http://www.cnblogs.com/XD-thinker/p/6722023.html
内容总结
以上是互联网集市为您收集整理的通过hibernate访问postgreSQL的搭建过程全部内容,希望文章能够帮你解决通过hibernate访问postgreSQL的搭建过程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。