Hibernate 入门
工具 idea
maven + web
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.2.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.15.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.4.2.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.5.6-Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>5.4.3.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies>
<!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 > <!-- 1、配置数据库连接的4个参数 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day1?serverTimezone=GMT%2B8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 是否显示sql语句 --> <property name="show_sql">true</property> <!-- 是否格式化sql语句 --> <property name="format_sql">true</property> <!-- 是否自动提交事务 --> <property name="hibernate.connection.autocommit">true</property> <!-- 2、配置JavaBean与表的映射文件 --> <!--<mapping resource= "cn/edu/aynu/User.hbm.xml"/>--> <mapping resource="User.hbm.xml"></mapping> <!--<mapping class="cn/edu/aynu/User.hbm.xml"></mapping>--> <!--<mapping package="cn/edu/aynu/User.hbm.xml"></mapping>--> </session-factory> </hibernate-configuration>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.edu.aynu.User" table="t_user"> <!-- 1、主键,如果属性与表字段不一至,要指定column --> <id name="uid" column="id"> <!-- 主键生成策略 --> <generator class="native"></generator> </id> <!-- 2、用户字段 --> <property name="username"></property> <property name="password"></property> </class> </hibernate-mapping>
虽然报红但是不影响使用
public class User { public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } private int uid; private String username; private String password; public User() { } public User(String username, String password) { this.username = username; this.password = password; } 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; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password=" + password + '}'; } }
运行结果