Skip to content

Commit 3d039d3

Browse files
committed
1.hibernate初探
2.maven03 archetype=generator生成 3.wedemo web工程 jetty tomcat配置在pom.xml文件中
1 parent bce2bf3 commit 3d039d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2226
-0
lines changed

Hibernate_001/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/
2+
/target/

Hibernate_001/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>Hibernate_001</groupId>
4+
<artifactId>Hibernate_001</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<build>
7+
<sourceDirectory>src</sourceDirectory>
8+
<testSourceDirectory>test</testSourceDirectory>
9+
<resources>
10+
<resource>
11+
<directory>src</directory>
12+
<excludes>
13+
<exclude>**/*.java</exclude>
14+
</excludes>
15+
</resource>
16+
</resources>
17+
<plugins>
18+
<plugin>
19+
<artifactId>maven-compiler-plugin</artifactId>
20+
<version>3.1</version>
21+
<configuration>
22+
<source>1.6</source>
23+
<target>1.6</target>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.hibernate</groupId>
31+
<artifactId>hibernate-core</artifactId>
32+
<version>4.1.0.Final</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.10</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
</project>

Hibernate_001/src/Address.hbm.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-1-29 17:14:45 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="Address" table="ADDRESS">
7+
<id name="postcode" type="java.lang.String">
8+
<column name="POSTCODE" />
9+
<generator class="assigned" />
10+
</id>
11+
<property name="phone" type="java.lang.String">
12+
<column name="PHONE" />
13+
</property>
14+
<property name="address" type="java.lang.String">
15+
<column name="ADDRESS" />
16+
</property>
17+
</class>
18+
</hibernate-mapping>

Hibernate_001/src/Address.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
4+
public class Address {
5+
6+
private String postcode; // Óʱà
7+
private String phone; // µç»°
8+
private String address; // µØÖ·
9+
10+
public Address () {
11+
12+
}
13+
14+
public Address(String postcode, String phone, String address) {
15+
this.postcode = postcode;
16+
this.phone = phone;
17+
this.address = address;
18+
}
19+
20+
public String getPostcode() {
21+
return postcode;
22+
}
23+
24+
public void setPostcode(String postcode) {
25+
this.postcode = postcode;
26+
}
27+
28+
public String getPhone() {
29+
return phone;
30+
}
31+
32+
public void setPhone(String phone) {
33+
this.phone = phone;
34+
}
35+
36+
public String getAddress() {
37+
return address;
38+
}
39+
40+
public void setAddress(String address) {
41+
this.address = address;
42+
}
43+
44+
45+
}

Hibernate_001/src/Junior.hbm.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-1-29 17:14:45 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="Junior" table="JUNIOR">
7+
<id name="sid" type="int">
8+
<column name="SID" />
9+
<generator class="assigned" />
10+
</id>
11+
<property name="sname" type="java.lang.String">
12+
<column name="SNAME" />
13+
</property>
14+
<property name="gender" type="java.lang.String">
15+
<column name="GENDER" />
16+
</property>
17+
<property name="birthday" type="java.util.Date">
18+
<column name="BIRTHDAY" />
19+
</property>
20+
<!--
21+
<many-to-one name="address" class="Address" fetch="join">
22+
<column name="ADDRESS" />
23+
</many-to-one>
24+
-->
25+
<component name="address" class="Address">
26+
<property name="postcode" column="POSTCODE"></property>
27+
<property name="phone" column="phone"></property>
28+
<property name="address" column="address"></property>
29+
</component>
30+
</class>
31+
</hibernate-mapping>

Hibernate_001/src/Junior.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.Date;
2+
3+
public class Junior {
4+
// 1.公有的类
5+
// 2.提供公有的不带参数的默认的构造方法
6+
// 3.属性私有
7+
// 4.属性setter/getter封装
8+
9+
private int sid; // 学号
10+
private String sname; // 姓名
11+
private String gender; // 性别
12+
private Date birthday;
13+
private Address address;
14+
15+
public Junior() {
16+
17+
}
18+
19+
public Junior(int sid, String sname, String gender, Date birthday,
20+
String address) {
21+
// super();
22+
this.sid = sid;
23+
this.sname = sname;
24+
this.gender = gender;
25+
this.birthday = birthday;
26+
}
27+
28+
public int getSid() {
29+
return sid;
30+
}
31+
32+
public void setSid(int sid) {
33+
this.sid = sid;
34+
}
35+
36+
public String getSname() {
37+
return sname;
38+
}
39+
40+
public void setSname(String sname) {
41+
this.sname = sname;
42+
}
43+
44+
public String getGender() {
45+
return gender;
46+
}
47+
48+
public void setGender(String gender) {
49+
this.gender = gender;
50+
}
51+
52+
public Date getBirthday() {
53+
return birthday;
54+
}
55+
56+
public void setBirthday(Date birthday) {
57+
this.birthday = birthday;
58+
}
59+
60+
public Address getAddress() {
61+
return address;
62+
}
63+
64+
public void setAddress(Address address) {
65+
this.address = address;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
71+
+ gender + ", birthday=" + birthday + ", address=" + address
72+
+ "]";
73+
}
74+
}

Hibernate_001/src/Students.hbm.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-1-26 17:47:24 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="Students" table="STUDENTS">
7+
<id name="sid" type="int">
8+
<column name="SID" />
9+
<!--
10+
<generator class="assigned" />
11+
-->
12+
<generator class="native" />
13+
</id>
14+
<property name="sname" type="java.lang.String">
15+
<column name="SNAME" />
16+
</property>
17+
<property name="gender" type="java.lang.String">
18+
<column name="GENDER" />
19+
</property>
20+
<property name="birthday" type="java.util.Date">
21+
<column name="BIRTHDAY" />
22+
</property>
23+
<property name="address" type="java.lang.String">
24+
<column name="ADDRESS" />
25+
</property>
26+
</class>
27+
</hibernate-mapping>

Hibernate_001/src/Students.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.Date;
2+
3+
// 学生类
4+
5+
public class Students {
6+
// 1.公有的类
7+
// 2.提供公有的不带参数的默认的构造方法
8+
// 3.属性私有
9+
// 4.属性setter/getter封装
10+
11+
private int sid; // 学号
12+
private String sname; // 姓名
13+
private String gender; // 性别
14+
private Date birthday;
15+
private String address;
16+
17+
public Students() {
18+
19+
}
20+
21+
public Students(int sid, String sname, String gender, Date birthday,
22+
String address) {
23+
// super();
24+
this.sid = sid;
25+
this.sname = sname;
26+
this.gender = gender;
27+
this.birthday = birthday;
28+
this.address = address;
29+
}
30+
31+
public int getSid() {
32+
return sid;
33+
}
34+
35+
public void setSid(int sid) {
36+
this.sid = sid;
37+
}
38+
39+
public String getSname() {
40+
return sname;
41+
}
42+
43+
public void setSname(String sname) {
44+
this.sname = sname;
45+
}
46+
47+
public String getGender() {
48+
return gender;
49+
}
50+
51+
public void setGender(String gender) {
52+
this.gender = gender;
53+
}
54+
55+
public Date getBirthday() {
56+
return birthday;
57+
}
58+
59+
public void setBirthday(Date birthday) {
60+
this.birthday = birthday;
61+
}
62+
63+
public String getAddress() {
64+
return address;
65+
}
66+
67+
public void setAddress(String address) {
68+
this.address = address;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
74+
+ gender + ", birthday=" + birthday + ", address=" + address
75+
+ "]";
76+
}
77+
78+
}

Hibernate_001/src/hibernate.cfg.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5+
<hibernate-configuration>
6+
<session-factory>
7+
<property name="connection.username">root</property>
8+
<property name="connection.password">root</property>
9+
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10+
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
11+
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12+
13+
<property name="show_sql">true</property>
14+
<property name="format_sql">true</property>
15+
<property name="hibernate.default_schema">hibernate</property>
16+
<property name="hbm2ddl.auto">create</property>
17+
<property name="hibernate.current_session_context_class">thread</property>
18+
19+
<mapping resource="Students.hbm.xml"/>
20+
<mapping resource="Junior.hbm.xml"/>
21+
</session-factory>
22+
</hibernate-configuration>

Hibernate_001/test/SessionTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import org.hibernate.Session;
2+
import org.hibernate.SessionFactory;
3+
import org.hibernate.cfg.Configuration;
4+
import org.hibernate.service.ServiceRegistry;
5+
import org.hibernate.service.ServiceRegistryBuilder;
6+
import org.junit.Test;
7+
8+
9+
public class SessionTest {
10+
11+
@Test
12+
public void testopenSession () {
13+
Configuration config = new Configuration().configure();
14+
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
15+
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
16+
Session session = sessionFactory.openSession();
17+
if(session != null) {
18+
System.out.println("session created successed!");
19+
}
20+
}
21+
22+
@Test
23+
public void testGetCurrentSession () {
24+
Configuration config = new Configuration().configure();
25+
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
26+
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
27+
Session session = sessionFactory.getCurrentSession(); // single
28+
if (session != null) {
29+
System.out.println("session created successed11!");
30+
} else {
31+
System.out.println("session created failed!");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)