请教一下各位:
试了一下spring2.5.6 MVC + hibernate3.3.2的annotation组合,在持久化实体时,没有报错,日志也显示调用到了相应的操作,但是数据库中却没有数据...,不知道是哪里有问题 ,代码如下:
applicationContext.xml
<context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="minPoolSize"><value>10</value></property> <property name="maxPoolSize"><value>100</value></property> <property name="maxIdleTime"><value>1800</value></property> <property name="acquireIncrement"><value>2</value></property> <property name="maxStatements"><value>100</value></property> <property name="initialPoolSize"><value>10</value></property> <property name="idleConnectionTestPeriod"><value>1800</value></property> <property name="acquireRetryAttempts"><value>30</value></property> <property name="breakAfterAcquireFailure"><value>true</value></property> <property name="testConnectionOnCheckout"><value>false</value></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="annotatedClasses"> <list> <value>com.zy.sa.test.entity.Userinfo</value> </list> </property> <!-- <property name="packagesToScan" value="com.zy.sa.test.entity.*" /> --> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.provider_class">com.mchange.v2.c3p0.ComboPooledDataSource</prop> <prop key="hibernate.dialect">${hibernate_dialect}</prop> <prop key="hibernate.show_sql">${hibernate_show_sql}</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.transcation.flush_before_completion">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/> <!-- 支持 @Transactional --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
annomvc-servlet.xml
<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.zy.sa.test" /><context:annotation-config/> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /></pre><br /><br /><strong>web.xml</strong><br /><br /><pre name="code" class="java"><!-- Spring 服务层的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 日志配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEF-INF/classes/log4j.properties</param-value> </context-param> <!-- Spring 容器启动监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 要负责处理由JavaBeans Introspector的使用而引起的缓冲泄露 --> <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> <!-- Hibernate Open Session In View --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <!-- Hibernate Open Session In View --> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 编码 --> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的配置文件,以启动Spring MVC模块--> <servlet> <servlet-name>annomvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>annomvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/WEB-INF/common/uncaughtException.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/WEB-INF/common/403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/common/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/common/500.jsp</location> </error-page></pre><br /><br /><strong>MyController.java</strong><br /><br /><pre name="code" class="java">@Controller
@RequestMapping("/my.do")
public class MyController {@Autowired private UserinfoService userinfoService; @RequestMapping public String my(Userinfo user) { System.out.println("my.do"); System.out.println("userinfoService: " + userinfoService); System.out.println(user); userinfoService.createUserInfo(user); return "userinfo_create"; }
}
UserinfoServiceImpl.java
@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {@Autowired private UserinfoDao userinfoDao; @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) public void createUserInfo(Userinfo user) { Userinfo userinfo = new Userinfo(); userinfo.setId("ww"); userinfo.setUserName("wangwu"); userinfo.setPassword("111"); userinfo.setEmail("ww@aaa.com"); userinfoDao.save(userinfo); userinfoDao.getSession().save(userinfo); } @Override @Transactional(readOnly=true) public List<Userinfo> getAllUser() { return userinfoDao.getAll(); }</pre><br /><br /><strong>UserinfoDao.java</strong><br /><pre name="code" class="java">@Repository
public class UserinfoDao extends HibernateDao<Userinfo, String> {
@Override @Autowired public void setSessionFactory(SessionFactory sessionFactory) { logger.info("sessionFactory: " + sessionFactory); super.setSessionFactory(sessionFactory); }
}
HibernateDao.java
public Session getSession() {
return sessionFactory.getCurrentSession();
}public void save(final T entity) { Assert.notNull(entity, "entity不能为空"); getSession().saveOrUpdate(entity); logger.debug("save entity: {}", entity); }
通过:http://localhost:8080/springannotation/my.do?id=lisi&userName=ls&password=111&email=ls@aaa.com访问,控制台打印出了实体信息,但数据库无数据,不知道问题出在哪里?
问题补充
没人帮忙看一下啊