频道栏目
首页 > 资讯 > 其他综合 > 正文

ssm框架总体整合

16-07-15        来源:[db:作者]  
收藏   我要投稿

一.整合之后结构如下:

具体配置内容如下:

1.创建springmvc.xml文件配置如下

  
    
   
             
             
   
   
   
  
   
   
  

2.创建文件applicationContext配置如下:

  
    
      
      
          
            oracle.jdbc.driver.OracleDriver  
          
          
            jdbc:oracle:thin:@127.0.0.1:1521:orcl  
          
          
            ErpSystem  
          
          
            123  
          
          
            10  
          
       
       
       
          
          
     
       
    
     
     
        
        
      
     
     
     
     
     
     
     
       
          
      
      
      
          
          
              
              
              
          
      
      
          
          
      
  

3.创建文件mybatis-config.xml配置如下:

  
  
  
    
       
    
  

4.web.xml文件配置如下:

  
  
  ssmmodel1  
    
    index.html  
    index.htm  
    index.jsp  
    default.html  
    default.htm  
    default.jsp  
    
    
    
     springmvc2  
     org.springframework.web.servlet.DispatcherServlet  
       
       contextConfigLocation  
       classpath:springmvc.xml  
       
     1  
    
    
     springmvc2  
     *.action  
    
    
    
    org.springframework.web.context.ContextLoaderListener  
    
    
    contextConfigLocation  
    classpath:applicationContext.xml  
     
  

5.daoimp层里的IDao接口内容如下:

package com.gxa.bj.daoimp;  
  
import java.util.List;  
  
public interface IDaoimp {  
       /** 
     * 向数据库表里添加一条数据的方法 
     */  
    public int  addItem(T t);  
    /** 
     * 从数据库表里删除一条数据的方法 
     * @param objId 
     */  
    public int removeItem(Object objId);  
    /** 
     * 更新数据库表里的一条字段的方法 
     * @param t 
     */  
    public int updateItem(T t);  
      
    /** 
     * 根据主键字段获取该条数据,并转换成实体对象 
     */  
    public T getModel(Object objId) ;  
      
    /** 
     * 根据查询条件获取多条数据,并转换成相应的集合 
     */  
    public List getList(T t);  
}  

6.dao层里Mapper接口内容如下:

package com.gxa.bj.dao;  
  
import com.gxa.bj.daoimp.IDaoimp;  
import com.gxa.bj.model.UserInfo;  
  
/** 
 * 继承了IDaoimp接口就拥有了它的方法,如果需要添加新方法,在下面添加 
 * @author lv 
 * 
 */  
public interface UserInfoMapper extends IDaoimp{  
      

7.model层里的实体类及sql映射xml文件内容如下:

1)userInfo实体类

package com.gxa.bj.model;  
  
public class UserInfo {  
    private Integer userId;//用户编号  
    private String userName;//用户姓名  
    private String userPwd;//用户密码  
    private String flag;//标示  
      
    public Integer getUserId() {  
        return userId;  
    }  
    public void setUserId(Integer userId) {  
        this.userId = userId;  
    }  
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getUserPwd() {  
        return userPwd;  
    }  
    public void setUserPwd(String userPwd) {  
        this.userPwd = userPwd;  
    }  
    public String getFlag() {  
        return flag;  
    }  
    public void setFlag(String flag) {  
        this.flag = flag;  
    }  
}  

2)sql映射文件userInfoMapper.xml:

  
  
  
  
      
        insert into UserInfo(userid,username,userpwd,flag)  
        values(usernext.nextval,#{userName},#{userPwd},#{flag})  
      
      
        delete From UserInfo Where userid=#{id}   
      
      
        update UserInfo set  
          
            userName=#{userName},  
          
          
            userPwd=#{userPwd},  
          
          
            flag=#{flag},  
          
        userId=#{userId} Where userId=#{userId}  
      
      
     
     
  

8.Service层里的业务层内容如下:
加上@Service注解,才能吧Service扫描进Spring IOC容器

package com.gxa.bj.service;  
  
import java.util.List;  
  
import org.springframework.stereotype.Service;  
  
import com.gxa.bj.dao.UserInfoMapper;  
import com.gxa.bj.model.UserInfo;  
@Service  
public class UserInfoService {  
    private UserInfoMapper userInfoMapper;  
    public List getList(UserInfo u){  
        return userInfoMapper.getList(u);  
    }  
    public UserInfoMapper getUserInfoMapper() {  
        return userInfoMapper;  
    }  
    public void setUserInfoMapper(UserInfoMapper userInfoMapper) {  
        this.userInfoMapper = userInfoMapper;  
    }  
    public UserInfo getLogin(String name,String Pwd){  
        UserInfo u = new UserInfo();  
        u.setUserName(name);  
        u.setUserPwd(Pwd);  
        List list=userInfoMapper.getList(u);  
        if(list.size()>0){  
            return list.get(0);  
        }else{  
            return null;  
        }  
    }  
}  

9.action层里的界面层内容如下:
Controller(控制器)主要负责处DispacherServlet分发的请求
定义i@Controller注解标记,然后使用@RequestMapping和@RequestParam等注解来定义url请求和方法直接的映射

package com.gxa.bj.action;  
  
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
import java.util.List;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
import com.gxa.bj.model.UserInfo;  
import com.gxa.bj.service.UserInfoService;  
  
@Controller  
public class UserInfoAction {  
    private UserInfoService userInfoService;  
    public UserInfoService getUserInfoService() {  
        return userInfoService;  
    }  
    public void setUserInfoService(UserInfoService userInfoService) {  
        this.userInfoService = userInfoService;  
    }  
    public List getList(UserInfo u){  
        return userInfoService.getList(u);  
    }  
    @RequestMapping(value="/getalluser.action")  
    public ModelAndView getAllUser(){  
        ModelAndView model = new ModelAndView();  
        List list = userInfoService.getList(null);  
        model.addObject("users", list);  
        model.setViewName("/index.jsp");  
        return model;  
    }  
    @RequestMapping(value="/login.action")  
    public void login(HttpServletRequest req,HttpServletResponse resp){  
        try {  
            req.setCharacterEncoding("UTF-8");  
        } catch (UnsupportedEncodingException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
        String userName=req.getParameter("userName");  
        String userPwd=req.getParameter("userPwd");  
        UserInfo u=userInfoService.getLogin(userName,userPwd);  
        if(u!=null){  
            System.out.println("登陆成功");  
            req.getSession().setAttribute("userInfo",u);  
            try {  
                req.getRequestDispatcher("login.jsp").forward(req, resp);  
            } catch (ServletException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }else{  
            System.out.println("登陆失败");  
        }  
    }  
}  

 

相关TAG标签
上一篇:STATE(状态)模式
下一篇:系统限流实践 - 接入层限流(下*完结)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站