springMVC、spring与Hibernate结合(具体需要hibernate的jar包还没有测试):
网上的是:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包
首先在web.xml中设置如下:
- <!-- Spring MVC配置 -->
- <!-- ====================================== -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
- </init-param>
- -->
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- Spring配置 -->
- <!-- ====================================== -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:config/applicationContext.xml</param-value>
- </context-param>
- <!-- 配置字符集 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Session
OpenSessionViewFilter的主要功能是用来将一个Hibernate Session和一次完整的请求过程相绑定
-->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在spring-servlet.xml中设置如下(这儿不够完整):
<!-- 静态资源(js/image)的访问 js在webcontent下-->
<mvc:resources location="/js/" mapping="/js/**"/>
<!-- 直接指定视图名,无需自己编写controller -->
<bean name="/" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="login"/>
</bean>
- <!-- 启用spring mvc 注解 -->
- <context:annotation-config />
- <!-- 设置使用注解的类所在的jar包,要扫描的controller所在包-->
- <context:component-scan base-package="controller"></context:component-scan>
- <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
在applicationContext.xml中设置如下(这儿不够完整):
- <!--切面配置,重点说一下环绕切面-->
- <!--切面类-->
<bean id="beforeAspect" class="com.neusoft.AOPTest"></bean>
<aop:config>
<!--声明一个切点, expression是个表达式-->
<aop:pointcut id="beforePointCut" expression="execution (* com.neusoft.service.impl.UserserviceImol.queryUserinfo())" />
<!--将切面与切点关联起来,下面的表明,环绕切面,执行切面类中的around方法 <aop:around表明是一个环绕切面-->
<!--常见的有<aop:beford <aop:after <aop:around相当于将前两者结合-->
<aop:aspect ref="beforeAspect">
<aop:around pointcut-ref="beforePointCut" method="around"/>
</aop:aspect>
</aop:config>
java代码:环绕切面
public void around(ProceedingJoinPoint joinpoint) {
try {
System.out.println("oooooooo环绕通知开始oooooooo");
joinpoint.proceed();
System.out.println("oooooooo环绕通知结束oooooooo");
} catch (Throwable t) {
System.out.println("Boo!Wewantourmoneyback!");
}
] - 对数据库的配置:可以写到另一个配置文件,在服务器启动的时候加载
- <!-- 配置数据源 oracle.jdbc.driver.OracleDriver com.p6spy.engine.spy.P6SpyDriver-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="username"></property>
<property name="password" value="password"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--通过扫描-->
<property name="packagesToScan">
<list>
<value>com.tgb.entity</value>
</list>
</property>
<!--指定<property name="annotatedClasses">
<list>
<value>com.tgb.entity.User</value>
<value>com.tgb.entity.Child</value>
<value>com.tgb.entity.IDCard</value>
<value>com.tgb.entity.UserKey</value>
<value>com.tgb.entity.Friend</value>
</list>
</property> - -->
</bean>
<!-- 配置一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
这样三个框架基本就结合在了一起,到这里会出现几个配置文件:
web.xml-------------------------------------------------每个web项目都有,里面将sping MVC和sping两个框架整合到web项目中
spring-servlet.xml-----------------------------------对springMVC框架的配置
applicationContext.xml-----------------------------对spring框架的配置,这里面整合了hibernate框架,
hibernate.cfg.xml-----------------------------------对hibernate框架的配置。(如果如同上面一样配置了数据库,可以没有)
一些基本的注解:
spingMVC:
@Controller表示这是控制器。
@RequestMapping(params = "", method=RequestMethod.POST) 表示POST请求
@ResponseBody 表示放回值不进行视图映射,直接作为响应体中的内容返回,放回json或xml等时会用到
@RequestParam(value="username")在参数前面加,是指请求url地址映射中必须含有的参数(除非属性required=false)
spring:
@Resource(name = "")放在属性前面一种自动注入方式
hibernate:(一个例子)
@Entity
@Table(name="T_USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="user_seq")
@SequenceGenerator(name="user_seq", sequenceName="user_seq")
private Long id;
@Column
private String userName;
@Column
private String age;
@OneToOne(mappedBy = "user",cascade = {CascadeType.ALL},fetch = FetchType.LAZY)
private Child child;
@OneToMany(mappedBy = "user",cascade = CascadeType.ALL,fetch = FetchType.LAZY)//双向关联
private List<UserKey> userKeyList=new ArrayList<UserKey>();
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(name="t_user_friend",//自定义表??
joinColumns={@JoinColumn(name="userid")},//自定义列??
inverseJoinColumns={@JoinColumn(name="friendid")})//反转,和Teacher对应的那个表的列名,
private List<Friend> friendList=new ArrayList<Friend>();