1.hibernate的必须的包
2.hibernate的二级缓存:
3.hibernate的c3p0包:
4.mysql的驱动包:
5.hibernate的jpa包:
6.Spring和springMVC的包:
7.springData的包:
8.好了:
用idea配置web.xml的spring和springMVC的 配置文件,如果需要修改路径就修改,配置乱码处理过滤,配置springMVC使用resful风格色post转换方法
web.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns=
"http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation=
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version=
"3.1"
>
<!--handle the Chinese garbled-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-
class
>org.springframework.web.filter.CharacterEncodingFilter</filter-
class
>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-
8
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--支持reful风格增删改查-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-
class
>org.springframework.web.filter.HiddenHttpMethodFilter</filter-
class
>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--configure the Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/configuration/spring.xml</param-value>
</context-param>
<listener>
<listener-
class
>org.springframework.web.context.ContextLoaderListener</listener-
class
>
</listener>
<!--configure the springMVC-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/configuration/mvc.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
9.配置springMVC:扫描包,解析器,加载静态功能和注解功能
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
>
<!--自动扫描包-->
<context:component-scan base-
package
=
"com"
use
-
default
-filters=
"false"
>
<context:
include
-filter type=
"annotation"
expression=
"org.springframework.stereotype.Controller"
/>
<context:
include
-filter type=
"annotation"
expression=
"org.springframework.web.bind.annotation.ControllerAdvice"
/>
</context:component-scan>
<!--视图解析器-->
<bean id=
"my"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name=
"prefix"
value=
"/WEB-INF/views/"
/>
<property name=
"suffix"
value=
".jsp"
/>
</bean>
<!--处理静态资源-->
<mvc:
default
-servlet-handler></mvc:
default
-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
|
10.配置spring文件:扫描包,配置dataSource,配饰japEntityFactory等等..jpa的基本配置,事务,jpa管理器,springData
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"
>
<!--加载配置文件-->
<context:property-placeholder location=
"classpath:db.properties"
/>
<!--扫描包,排除springMVC扫描的包,重复扫描会有问题-->
<context:component-scan base-
package
=
"com"
>
<context:exclude-filter type=
"annotation"
expression=
"org.springframework.stereotype.Controller"
></context:exclude-filter>
<context:exclude-filter type=
"annotation"
expression=
"org.springframework.web.bind.annotation.ControllerAdvice"
/>
</context:component-scan>
<!--配置数据源-->
<bean id=
"dataSource"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
>
<property name=
"user"
value=
"${user}"
/>
<property name=
"password"
value=
"${password}"
/>
<property name=
"driverClass"
value=
"${drive}"
/>
<property name=
"jdbcUrl"
value=
"${url}"
/>
</bean>
<!--jpa管理工厂-->
<bean id=
"entityManagerFactory"
class
=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
>
<!--数据源-->
<property name=
"dataSource"
ref=
"dataSource"
/>
<!--适配器-->
<property name=
"jpaVendorAdapter"
>
<bean
class
=
"org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
/>
</property>
<!--扫描包-->
<property name=
"packagesToScan"
value=
"com"
/>
<!--基本配置-->
<property name=
"jpaProperties"
>
<props>
<!--命名策略-->
<prop key=
"hibernate.ejb.naming_strategy"
>org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!--基本配置-->
<prop key=
"hibernate.format_sql"
>
true
</prop>
<prop key=
"hibernate.show_sql"
>
true
</prop>
<prop key=
"hibernate.hbm2ddl.auto"
>update</prop>
<prop key=
"hibernate.dialect"
>org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!--二级缓存-->
<prop key=
"hibernate.cache.use_query_cache"
>
true
</prop>
<prop key=
"hibernate.cache.use_second_level_cache"
>
true
</prop>
<prop key=
"hibernate.cache.region.factory_class"
>org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
<property name=
"sharedCacheMode"
value=
"ENABLE_SELECTIVE"
/>
</bean>
<!--配置事务-->
<bean id=
"transactionManager"
class
=
"org.springframework.orm.jpa.JpaTransactionManager"
>
<property name=
"entityManagerFactory"
ref=
"entityManagerFactory"
/>
</bean>
<!--配置基于注解的事务-->
<tx:annotation-driven transaction-manager=
"transactionManager"
/>
<!--配置spring data-->
<jpa:repositories base-
package
=
"com"
entity-manager-factory-ref=
"entityManagerFactory"
></jpa:repositories>
</beans>
|
11.写一个实体类Employee.java、Department.java,多对一注解
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package
com.domain;
import
javax.persistence.*;
import
java.util.
Date
;
/**
* Created by Anonymous on 2016/4/12.
*/
@Table(name =
"sssp_empoyee"
)
@Entity
public
class
Employee {
private
Integer id;
private
String
lastName;
private
String
email;
private
Date
birth;
private
Date
crateTime;
private
Department department;
@GeneratedValue
@Id
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String
getLastName() {
return
lastName;
}
public
void
setLastName(
String
lastName) {
this
.lastName = lastName;
}
public
String
getEmail() {
return
email;
}
public
void
setEmail(
String
email) {
this
.email = email;
}
@Temporal(TemporalType.DATE)
public
Date
getBirth() {
return
birth;
}
public
void
setBirth(
Date
birth) {
this
.birth = birth;
}
@Temporal(TemporalType.TIMESTAMP)
public
Date
getCrateTime() {
return
crateTime;
}
public
void
setCrateTime(
Date
crateTime) {
this
.crateTime = crateTime;
}
@JoinColumn(name =
"Department_ID"
)
@ManyToOne
public
Department getDepartment() {
return
department;
}
public
void
setDepartment(Department department) {
this
.department = department;
}
}
|
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
|
package
com.domain;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.Table;
/**
* Created by Anonymous on 2016/4/12.
*/
@Table(name =
"sssp_department"
)
@Entity
public
class
Department {
private
Integer id;
private
String
departmentName;
@GeneratedValue
@Id
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String
getDepartmentName() {
return
departmentName;
}
public
void
setDepartmentName(
String
departmentName) {
this
.departmentName = departmentName;
}
}
|
写个测试类运行,则在数据库自动生成表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
org.junit.Test;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
Main {
private
ApplicationContext context;
public
Main() {
context =
new
ClassPathXmlApplicationContext(
"classpath:com/configuration/spring.xml"
);
}
@Test
public
void
testCreateTable() {
}
}
|
效果: