开发者社区> 问答> 正文

MyBatis+spring

MyBatis+Spring 集成完毕。

想实现与数据库用户的姓名密码对比的简单登录

单独写方法用Junit测试可以取到数据库里的数据。

测试读数据库代码如下:

ApplicationContext aContext = 
                 new FileSystemXmlApplicationContext(
                           "WebRoot/WEB-INF/applicationContext.xml");
UserMapper userMapper = aContext.getBean(UserMapper.class);
 //调用userMapper方法 按名字实现数据库查询
 user = userMapper.getUser("beer");
System.out.println("---" +userName );
System.out.println("---" +userPsw );
可以取到数据库的数据 对应 beer    999

但是放在业务逻辑中或(我)放在servlet中 报org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\Program Files\Java\Tomcat 6.0\bin\WebRoot\WEB-INF\applicationContext.xml]; nested exception is java.io.FileNotFoundException: WebRoot\WEB-INF\applicationContext.xml (系统找不到指定的路径。)

servlet中的代码如下:


package com.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.FileSystemXmlApplicationContext;
 
import com.sarnath.entity.User;
import com.sarnath.entity.UserMapper;
 
public class indexServlet extends HttpServlet {
    private User user;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
        ApplicationContext aContext = new FileSystemXmlApplicationContext(
                "WebRoot/WEB-INF/applicationContext.xml");
        UserMapper userMapper = aContext.getBean(UserMapper.class);
        //调用userMapper方法 按名字实现数据库查询
        user = userMapper.getUser("beer");
        String userName = user.getName().toString();
        String userPsw = user.getPsw().toString();
 
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("userName");
        String pwd = request.getParameter("psw");
        // 生成页面的格式
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if (userName.equals(name) && userPsw.equals(pwd)) {
            // out.print("<script>alert('恭喜你');window.location.href='/Mybatis_spring/success.jsp';</script>");
            // new一个输出的对象
            out.print("username:" + name + "<br/>");
            out.print("password:" + pwd + "<br/>");
            //显示从数据库中读取的数据
//          out.print("oralce" + userName);
            out.flush();
            out.close();
        } else {
            // out.print("对不起用户名或密码不正确");
            out.print("<script>alert('对不起');window.location.href='/Mybatis_spring/index.jsp';</script>");
        }
 
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
 
}

很是郁闷,为什么单测并没有路径错误,放在业务逻辑中就找不到路径。

因为刚接触 所以求大虾指点一二。

展开
收起
a123456678 2016-03-13 14:57:22 2488 0
1 条回答
写回答
取消 提交回答
  • 最佳实践是spring配置文件放到classpath里,而不是WEB-INF下。

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");

    2019-07-17 19:02:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多