栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring-整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-整合mybatis

第一步 导入jar包

在后面的学习中我们会接触到maven,那个时候就不用我们自己这么费劲的导入jar包了

导入springIOC相关jar包、mybatis包、mybatis整个包和jdbc

第二步 在src下创建MVC模式的结构 第三步 配置applicationcontext.xml文件


       
    
    
        
        
        
        
    
    
     
    
        
    
    
    
    
        
        
        
    
	
	
    
        
    


第四步 在web.xml中配置applicationcontext.xml的路径和spring监听器


    
    
        contextConfigLocation
        classpath:applicationcontext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

第五步 在servlet的init()中获取serviceImpl的bean
public class LoginServlet extends HttpServlet {
    private UserService service;
    @Override
    public void init() throws ServletException {
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        service = (UserServiceImpl) ac.getBean("userService");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        String pwd = req.getParameter("pwd");

        User user1 = service.doLogin(user, pwd);
        if(user1!=null){
            resp.getWriter().print("success");
        }else{
            resp.getWriter().print("false");

        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号