springmvc + hibernate+ postgresql 遇到的问题解决方法和总结

    xiaoxiao2021-03-25  118

    springmvc+ hibernate+ postgresql 遇到的问题解决方法和总结 界面-->  JS-->请求-->controller 拦截-->业务处理-->数据库操作 java工程中工作的路径问题: 默认:工程名/src/  src是source的缩写 hibernate-数据库 ①配置数据源。 driverClassName以数据库为准 ②配置SeesionFactory 。 其中hibernate.show_sql属性设为true可在执行时输出sql语句,方便查错 hibernate.dialect也是以数据库为准 mappingResourse中存放数据库和实体的映射 数据库和实体中的属性一一对应 主键的映射中 generatorclass 要注意 assigned 就是以数据库中的主键为主键,不考虑增长,无需hibernate参与。 naive  Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。 sequence 主键自增长。(better not use) ③配置事务  ④配置DAO SERVICE CONTROLLER DAO层与数据库相连的使用注意事项: ①sessionFactory的getCurrentSession()不好用时可换成openSession();注意此时增删改都需要flush()才能写入数据库。 ②不想使用sql语句可使用session中的get函数得到数据库的数据对应的对象,然后save(),delete(),update(), 此处save的实体可以是自己创建的,but update的实体必须是用get函数得到的实体然后通过setter系列方法写入。 springmvc相关问题 一、关于注解@Autowired **当 Spring容器启动时,AutowiredAnnotationBeanPostProcessor将扫描Spring容器中所有Bean,当发现Bean中拥有@Autowired 注释时就找到和其匹配(默认按类型匹配)的Bean,并注入到对应的地方中去。 ①<context:component-scan />这个标签,是隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor这两个bean。 ②也可以使用<context:annotation- config/>隐式地向 Spring容器注册了AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。 因此可选择<context:component-scan/>或者<context:annotation-config/>之一。 二、关于使用json传递 It's OK! <!-- 返回值支持json格式 -->       <bean             class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">             <property name="messageConverters">                   <list>                         <bean                               class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                               <property name="supportedMediaTypes">                                     <list>                                           <value>application/json;charset=UTF-8</value>                                     </list>                               </property>                         </bean>                   </list>             </property>       </bean> (很久以前找到的,每次用这个都可以处理好json的数据问题) 三、关于注解@responsebody *@responsebody表示该方法的返回结果直接写入HTTP response body中 一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。 *来自  http://www.cnblogs.com/guodefu909/p/4216327.html ps:当前台传来的数据的数据类型与实体的数据类型不一致的时候,可通过HttpServletRequest request的getParameter() 方法将数据获取到,然后处理成实体的数据类型。
    转载请注明原文地址: https://ju.6miu.com/read-18757.html

    最新回复(0)