• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
鼎盛工作室
每天提高一点点,每天积累一点点,每天一点进步,有目标有计划的奋斗一生,每天追逐梦想,软件人生,人生软件。
博客园    首页    新随笔    联系   管理    订阅  订阅
昨天患的错误 想当然
spring当中配置Dao的文件
 1<?xml version="1.0" encoding="UTF-8" ?>
 2
 3<beans xmlns="http://www.springframework.org/schema/beans"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xmlns:jee="http://www.springframework.org/schema/jee"
 6    xmlns:p="http://www.springframework.org/schema/p"
 7    xmlns:context="http://www.springframework.org/schema/context"
 8    xmlns:tx="http://www.springframework.org/schema/tx"
 9    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
11                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
12                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13
14    <!-- 数据源 -->
15    <jee:jndi-lookup id="guestbookDS" jndi-name="java:/comp/env/jdbc/guestbook" />
16
17    <!-- Hibernate配置 -->
18    <bean id="sessionFactory"
19        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
20        <property name="dataSource" ref="guestbookDS"></property>
21        <property name="annotatedClasses">
22            <list>
23                <value>com.guestbook.entity.User</value>
24            </list>
25        </property>
26        <property name="hibernateProperties">
27            <props>
28                <prop key="hibernate.dialect">
29                     org.hibernate.dialect.MySQL5Dialect
30                </prop>
31                <prop key="hibernate.show_sql">true</prop>
32            </props>
33        </property>
34    </bean>
35<!-- 
36    <bean id="messageDao" class="com.guestbook.dao.MessageDao">
37         <property name="sessionFactory" ref="sessionFactory"></property>
38    </bean>
39 -->    
40    
41    <bean id="userDao" class="com.guestbook.dao.UserDao">
42        <property name="sessionFactory" ref="sessionFactory"></property>
43    </bean>
44
45
46    <!-- 事务管理器配置,单数据源事务 -->
47    <bean id="transactionManager"
48        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
49        <property name="sessionFactory" ref="sessionFactory"></property>
50    </bean>
51
52    <!-- 使用annotation定义事务 -->
53    <tx:annotation-driven transaction-manager="transactionManager" />
54
55</beans>

在Controller中
 1package com.guestbook.web;
 2
 3import java.io.IOException;
 4import java.io.PrintWriter;
 5
 6import javax.servlet.http.HttpServletRequest;
 7import javax.servlet.http.HttpServletResponse;
 8
 9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.stereotype.Controller;
11import org.springframework.web.bind.annotation.RequestMapping;
12
13import com.guestbook.dao.UserDao;
14import com.guestbook.entity.User;
15
16@Controller
17@RequestMapping("/user.htm")
18public class UserController {
19    
22    
23    @RequestMapping(params = "method=login")
24    public void login(String username, String password, HttpServletResponse response) throws IOException{
25        
26        System.out.println(username);
27        UserDao userDao = new UserDao();
28//        PrintWriter out =  response.getWriter();
29          User user =    userDao.findByGUID("l");    
30        System.out.println(user.getUsername()+user.getPassword()+user.getPersonalmsg());        
31    }

32    
33    
34    @RequestMapping(params = "method=findAllUser")
35    public void findAllUser(HttpServletRequest req, HttpServletResponse rep) throws IOException{
36        
37    }

38}

39
能看出有什吗错吗 一步 一步来看
一直出现 HibernateTemplate 为空  我在想 肯定是 sessionFactory DataSource 没配置好 还是没加进Dao 里面进去
首先 我在想 是不是数据库连接 错了 我去做了个数据源连接测试 在我 blog 中间件模块中有测试方法 结果是没错的
       我又在想 是不是我实体Dao写错了 又一个个对过去 还是没错
       我又在想 是不是在spring中的配置错了 又去看了 数据库中配置sessionFactory中 的方言 改了好几回 还是错误
       为什么取出来会一直为空呢  我就在想了   这肯定是跟sessionFacroty 有关 ,我就在 spring中的  Dao配置中 看 它跟谁有关联 一级一级的找
终于 在controller中 找到了   UserDao user = new UserDao();
我人太懒  在建项目的时候  都没有用到面向接口编程 因为只是在测试用的  在这边  Sping提供的 DI 依赖注入  而我这边 直接 实例化Dao 就等于 把Spring提供的
DI 给抛弃了

        所以说啊  做人不能太懒 ,也不要想当然 一些常常出现的错误 其实 都 可以避免的 呵呵

通过 这次 我学到了  要认真 ,不能想当然  如果 程序 你以为 你认为 你觉得  这是很致命的想法  得改掉

从这次 我就在想了Sping中的依赖注入  给我们提供了这吗好的服务 怎嘛实现的呢  很好的一个思想 有空去研究研究它的源码 先这样子了
posted on 2009-09-11 07:14  鼎盛工作室  阅读(169)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3