Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
<display-name>Three</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。 -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射交给SpringMVC的dispatcherServlet来处理 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器 /*表示拦截所以请求 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
当hibernate+spring配合使用的时候,如果设置了lazy=true,那么在读取数据的时候,当读取了父数据后,
hibernate会自动关闭session,这样,当要使用子数据的时候,
系统会抛出lazyinit的错误,这时就需要使用spring提供的 OpenSessionInViewFilter,
用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。
-->
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注:
/: 会匹配到 /springmvc 这样的路径型url,而不会匹配到像 .jsp 这样的后缀型的url。
/*:会匹配到所有的url:路径型url 和后缀型的url (包括/springmvc,.jsp,.js,和.html等)。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- c3p0连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/book"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描实体类包: -->
<property name="packagesToScan">
<list>
<value>entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect </prop>
<!-- <prop key="hibernate.show_sql">true</prop> 是否显示SQL语句 -->
<!-- <prop key="hibernate.format_sql">true</prop> 是否格式化SQL语句 -->
<prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 自动创建|更新|验证数据库表结构。这里为更新-->
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
<!-- 加载映射文件,通过XML方式
<property name="mappingResources" >
<list>
<value>com/shin/entity/User.hbm.xml</value>
</list>
</property> -->
<!-- 加载映射文件,通过注解的方式
<property name="annotatedClasses">
<list>
<value>com.shin.entity.User</value>
</list>
</property> -->
</bean>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
**注:**需要c3p0的jar包。c3p0-0.9.2.1.jar、hibernate-c3p0-5.0.7.Final.jar、mchange-commons-java-0.2.3.4.jar。数据库驱动包就更不用说了。没有的话可以去Maven仓库下。
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置自动扫描包 -->
<context:component-scan base-package="test" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--使用默认的Servlet来响应静态文件-->
<mvc:default-servlet-handler/>
<!--
<mvc:annotation-driven>会自动注册
RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,
这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,
-->
<mvc:annotation-driven/>
</beans>
配置文件目录:
注:
在Spring的配置里,最好不要配置xsd文件的版本号。Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者网络不好,就会出现问题。为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。所以当我们没有配置xsd文件的版本号的时候,用的就是当前版本的XSD文件。
至此基本配置都配好了。我们来测试一下:
testDao.java
package test.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import entity.Admin;
@Repository
public class testDao {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
public Admin getAdmin(int id) {
Admin admin = (Admin)this.getSession().get(Admin.class, id);
return admin;
}
}
testService.java
package test.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import entity.Admin;
import test.dao.testDao;
@Service
@Transactional
public class testService {
@Autowired
private testDao testDao;
public Admin getAdmin(int id) {
return testDao.getAdmin(id);
}
}
testController.java
package test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import entity.Admin;
import test.service.testService;
@Controller
public class testController {
@Autowired
private testService testService;
@RequestMapping("/test")
public String test()
{
int id = 2;
Admin admin = testService.getAdmin(id);
System.out.println(admin.getAdminname());
return "success";
}
}
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
查询成功
</body>
</html>
跳转的成功页面
查询出的Adminname
最终项目目录结构
Spring推荐使用接口编程解耦,不过我这里没有用。因为感觉要创建好多个类啊。。偷懒喽