hibernate基本框架及配置

来源:互联网 发布:以太网地址和mac地址 编辑:程序博客网 时间:2024/05/18 01:01

1.hibernate要用到的jar包:




2.hibernate的基本框架(jdk用1.7版本的,不能用1.8版本的):


2.applicationContext.xml的配置:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.2.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"><!-- 引入properties文件 --><context:property-placeholder location="classpath*:/db.properties" /><context:annotation-config /><!-- 对指定的包进行组件扫描 --><context:component-scan base-package="com.courseManagement" /><!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --><context:component-scanbase-package="com.courseManagement.hibernate,com.courseManagement.Controller"><context:exclude-filter type="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan><!-- 定义数据库连接池数据源bean --><context:annotation-config /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${hibernate.driverName}" /><property name="url" value="${hibernate.url}" /><property name="username" value="${hibernate.username}" /><property name="password" value="${hibernate.password}" /></bean><!-- 定义Hibernate Session工厂 --><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="current_session_context_class">thread</prop></props></property><property name="packagesToScan" value="com.courseManagement.hibernate" /><!-- 如果多个,用“,”分隔 --></bean><!-- 定义事务 --><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --><tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true" /></beans>

3.db.properties配置:

hibernate.driverName=com.mysql.jdbc.Driverhibernate.url=jdbc:mysql://localhost:3306/coursemanagementhibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.username=roothibernate.password=roothibernate.maxIdle=255hibernate.minIdle=2hibernate.maxWait=120000hibernate.show_sql=truehibernate.format_sql=true

4.spring-mvc.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"xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.2.xsd      http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- 自动扫描该包,Spring MVC 会将包下用 @Controller 注解的类注册为 Spring 的 controller --><context:component-scan base-package="com.courseManagement.Controller" /><!-- 设置默认配置方案 --><mvc:annotation-driven /><!-- 视图解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 执行完action后会返回xxx,xxx会和下面的property组合,形成跳转页面的路径 --><property name="prefix" value="" /><property name="suffix" value=".jsp" /></bean></beans>

5.web.xml配置:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>courseManagerment</display-name><!-- 配置 Spring 核心监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 指定 Spring 的配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 定义 Spring MVC 前端控制器 --><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- 为 DispatcherServlet 建立映射 --><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- 编码过滤器 --><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的开关 --><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><!-- 设置首页 --><welcome-file-list><welcome-file>managerLogin.jsp</welcome-file></welcome-file-list></web-app>

6.com.courseManagement.hibernate包里面写的实际上是entity。

阅读全文
0 0
原创粉丝点击