加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring4 MVC Hibernate4集成

发布时间:2020-05-24 18:35:39 所属栏目:Java 来源:互联网
导读:Spring4 MVC Hibernate4集成

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

摘要 事物管理
packagecom.lei.demo.entity;
importjavax.persistence.*;
@Entity(name="users")
publicclassUsers{

publicUsers(){
super();
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
privateIntegerid;

@Column(name="user_name",length=32)
privateStringuser_name;

@Column(name="age")
privateIntegerage;

@Column(name="nice_name",length=32)
privateStringnice_name;

publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetUser_name(){
returnuser_name;
}
publicvoidsetUser_name(Stringuser_name){
this.user_name=user_name;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetNice_name(){
returnnice_name;
}
publicvoidsetNice_name(Stringnice_name){
this.nice_name=nice_name;
}
}
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"version="3.0">
<display-name>ArchetypeCreatedWebApplication</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--定义DispatcherServlet-->
<servlet>
<servlet-name>lei-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--默认/WEB-INF/[servlet名字]-servlet.xml加载上下文,
如果配置了contextConfigLocation参数,
将使用classpath:/lei-dispatcher-servlet.xml加载上下文
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/lei-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截匹配的请求,这里所有请求采用名字为lei-dispatcher的DispatcherServlet处理-->
<servlet-mapping>
<servlet-name>lei-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

<!--启动自动扫描该包下所有的Bean(例如@Controller)-->
<context:component-scanbase-package="com.lei.demo"/>

<!--定义视图解析器-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix">
<value>/WEB-INF/user/</value>
</property>
<propertyname="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!--Hibernate4-->
<!--加载资源文件其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
<context:property-placeholderlocation="classpath:persistence-mysql.properties"/>

<beanid="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="packagesToScan">
<list>
<!--可以加多个包-->
<value>com.lei.demo.entity</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<propkey="hibernate.dialect">${hibernate.dialect}</prop>
<propkey="hibernate.show_sql">${hibernate.show_sql}</prop>
<!--<propkey="hibernate.current_session_context_class">thread</prop>-->
</props>
</property>
</bean>

<!--数据库映射-->
<!--class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"-->
<!--class="org.springframework.jdbc.datasource.DriverManagerDataSource"-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.user}"/>
<propertyname="password"value="${jdbc.pass}"/>
</bean>

<!--配置Hibernate事务管理器-->
<beanid="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<!--配置事务异常封装-->
<beanid="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<!--声明式容器事务管理,transaction-manager指定事务管理器为transactionManager-->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="get*"propagation="REQUIRED"/>
<tx:methodname="*"read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:configexpose-proxy="true">
<!--只对业务逻辑层实施事务-->
<aop:pointcutid="txPointcut"expression="execution(*com.lei.demo.service..*.*(..))"/>
<!--Advisor定义,切入点和通知分别为txPointcut、txAdvice-->
<aop:advisorpointcut-ref="txPointcut"advice-ref="txAdvice"/>
</aop:config>

</beans>
#jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://yourServerIP:3306/yourDatabase?createDatabaseIfNotExist=true
jdbc.user=user
jdbc.pass=password
#hibernate.X
hibernate.connection.driverClass=org.gjt.mm.mysql.Driver
hibernate.connection.url=jdbc:mysql://yourServerIP:3306/yourDatabase
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.username=user
hibernate.connection.password=password
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读