Spring中使用JdbcTemplate访问数据库时出现NullPointerException

发布于 2024-12-16 23:04:05 字数 2201 浏览 2 评论 0原文

我是 Spring 新手,我正在遵循 Spring 网站上的书籍和教程中的一些示例,但我无法让我的代码工作。

我正在尝试使用 JdbcTemplate 访问数据库。这是我的 root-context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db" />
    <property name="username" value="userr" />
    <property name="password" value="password" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
<bean id="myDao" class="com.me.myproj.persistence.JdbcMyaDao">
    <property name="dataSource" ref="dataSource"/>
</bean>

这是我的 JdbcMyDao(所有导入都可以,我不会粘贴它们):

public class JdbcMyDao implements MyDao{

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
 }

public String getOpcionById(){
    String SQL_Q="select name from options where id=35";

    return (String)this.jdbcTemplate.queryForObject(SQL_Q, String.class);
}

}

最后,我的控制器:

@Controller
public class myController {

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(Model model){

        JdbcMyDao daoP=new JdbcMyDao();
        String op=daoP.getOpcionById();

        model.addAttribute("op",op);
        return "show";
    }
}

好的,然后,show.jsp 只是打印返回字符串。

所以,这是对数据库的一个非常简单的访问,但是当使用 JdbcTemplate (在函数 getOpcionById 中)时我得到一个 nullPointerException

我认为这是因为函数 setDataSource 从未运行过,我认为 spring 通过 IoC 运行它,我尝试将 @Autowired 放入 JdbcMyDao 的 jdbcTemplate 属性中,但它也不起作用。有人能告诉我 Spring 执行的步骤以及我应该执行哪些步骤吗?或者我应该做出哪些改变以及为什么?

I'm a Spring newbie, I'm following some examples from books and tutorials in the spring website and I can't get my code to work.

I'm trying to access to a DataBase using JdbcTemplate. This is how my root-context.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db" />
    <property name="username" value="userr" />
    <property name="password" value="password" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
<bean id="myDao" class="com.me.myproj.persistence.JdbcMyaDao">
    <property name="dataSource" ref="dataSource"/>
</bean>

This is my JdbcMyDao (all imports are ok, I won't paste them):

public class JdbcMyDao implements MyDao{

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
 }

public String getOpcionById(){
    String SQL_Q="select name from options where id=35";

    return (String)this.jdbcTemplate.queryForObject(SQL_Q, String.class);
}

}

Finally, my controller:

@Controller
public class myController {

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(Model model){

        JdbcMyDao daoP=new JdbcMyDao();
        String op=daoP.getOpcionById();

        model.addAttribute("op",op);
        return "show";
    }
}

Ok, then, show.jsp just prints the return string.

So, this is a very simple access to a DataBase, but I get a nullPointerException when using JdbcTemplate (in function getOpcionById)

I think this is because function setDataSource is never runned, I thought spring runs it by IoC, I tried to put @Autowired to jdbcTemplate property i JdbcMyDao, but it didn't work either. Can someone tell me the steps Spring does and which steps should I do? Or what changes should I make and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-12-23 23:04:05

您需要在控制器中自动装配 DAO,而不是实例化它:

@Autowired
private JdbcMyDao dao;

当您使用 new JdbcMyDao() 实例化它时,它不受 spring 管理,并且不会注入其依赖项。

在我看来,这是依赖注入初学者最常见的错误。查看这篇文章,看看它是否让您更清楚。

You need to autowire the DAO in your controller, rather than instantiating it:

@Autowired
private JdbcMyDao dao;

When you instantiate it with new JdbcMyDao() it is not managed by spring, and its dependencies are not injected.

This is, in my opinion, the most common mistake with dependency injection beginners. Check this post and see if it makes things clearer for you.

眼眸里的快感 2024-12-23 23:04:05

您的控制器正在实例化自己的 JdbcMyDao 并调用它,而不是使用您在 root-context.xml 中声明的 bean。您需要将 myDao bean 注入控制器并调用它。

另外,您是否有理由定义 JdbcTemplate bean,然后不对它执行任何操作? DAO 的 setDataSource 方法正在实例化它自己的 JdbcTemplate (这很好),因此没有理由声明 bean。

我有一种感觉,您还没有完全掌握依赖注入的概念 - 您似乎使用了直接实例化声明的bean,而不仅仅是后者。

Your controller is instantiating its own JdbcMyDao and calling that, rather than using the bean you declared in root-context.xml. You need to inject the myDao bean into your controller and invoke that.

Also, is there a reason you're defining a JdbcTemplate bean, and then not doing anything with it? The setDataSource method of the DAO is instantiating its own JdbcTemplate (which is fine), so there's no reason to declare a bean.

I have a feeling you've not quite grasped the concept of dependency injection - you seem to have used direct instantiation and declared beans, rather than just the latter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文