Hibernate的搭建与配置
1、新建项目,引入junit4,记得使用javaee7.0
buildpath==》addlibraries==>myeslipse library==>javaEE7.0 library
2、在src目录下建立hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 |
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <property name="show_sql">true</property> </session-factory> </hibernate-configuration> |
3、在src目录下建立log4j.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL #log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### #log4j.logger.org.hibernate.cache=debug ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace |
4、配置数据源,在第二步建好的hibernate.cfg.xml中写入以下配置内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<property name="myeclipse.connection.profile">hibernate</property> <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 </property> <property name="connection.username">你的数据库用户名</property> <property name="connection.password">你的数据库密码</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 以下的这些值你都可以根据自己的需求进行改动 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- dbcp c3p0 proxy --> <!-- c3p0数据库链接,指定连接池里最大连接数 线程池--> <property name="hibernate.c3p0.max_size">20</property> <!-- c3p0数据库链接,指定连接池里最小连接数 --> <property name="hibernate.c3p0.min_size">5</property> <!-- c3p0数据库链接,指定连接池里连接超时的时长 --> <property name="hibernate.c3p0.timeout">5000</property> <!-- c3p0数据库链接,指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.c3p0.acquire_increment">2</property> |
5、建立一个测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
@Entity @Table(name = "user")//这里的name是指你要和这个类对应的数据表的名字 public class TzUser implements java.io.Serializable { private Integer id; private String username; private String password; //主键必须配置这两个注解,否则会报错 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
6、在你的hibernate.cfg.xml中配置你要操作的类,包路径要全
1 |
<mapping class="com.User" /> |
7、写一个测试方法,进行查询操作(假如你的表中有数据)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Test { //查询 @org.junit.Test public void handle(){ //默认就是hibernate.cfg.xml Configuration configuration = new Configuration().configure(); //创建SessionFactory SessionFactory factory = configuration.buildSessionFactory(); //创建session(封装jdbc) Session session = factory.openSession(); //查询不需要事物,因为这里的查询语句没有别名,所以类的属性名和表里的属性名应一模一样,否则查询失败 List<User> users = session.createQuery("from User").list(); for (User user : users) { System.out.println(user.getAddress()+"======"+user.getAge()); } } } |
发表评论