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

Spring-springMVC-Mybatis整合

Java 更新时间:发布时间: 百科书网 趣学号

整合前准备:

在上次的spring-mybatis整合中我们使用的是一个普通的maven项目,在SSM整合的话我们要变成一个web项目(因为SSM整合就是在SM整合的前提下再配置springmvc的配置文件),在idea中你的项目右键点击add frameworks support中添加web Application(注意如果没有的话idea判断你有web支持,但是因为某一些操作导致未完全添加web支持,解决方法就是在file-project Structure 中的Facets,如果看到右边的web(项目名),然后点击这个进行delete删除,然后再回到第一步的添加web Application)

第一步将Spring配置文件(applicationContext.xml)纳入web项目中(在web.xml中添加):

 
        contextConfigLocation
        classpath:applicationContext.xml
    >
    
         
        
        org.springframework.web.context.ContextLoaderListener
    

说一下在这里为什么在web.xml要加监听器:在Java程序中我们想让ioc容器初始化,我们都是在main方法中new ClasaPathXmlApplicationContext(xxx.xml),如果我们在Javaweb程序中我们没有main方法也想用Java程序那种初始化方法,我们用一个servlet,或者service都要写一个初始化一次,一旦servlet特别多我们每一次都要写一个,特别特别的麻烦,那么在web容器是如何进行初始化ioc容器呢,web容器就是提供一个监听器,当服务器启动时,通过监听器将SpringIOC容器初始化一次,(注意:利用listener监听器在eclipse中可以正常运行tomcat,但是在idea中配置监听器会导致服务器启动不了,原因这里就不详细讲,后面会专门讲一下这个问题),因为不能利用监听器,所以我们在Springmvc的配置文件中引入applicationContext.xml

  

第二步在applicationContext.xml中配置数据源,还有获取外部文件db.properties(里面是数据库的信息)

 

    


    
        
            
                classpath:db.properties
            
        
    

    
        
        
        
        
        
        
      

第三步:我们要在applicationContext.xml中创建mybatis重要的SqlSessionFactory,并且把mapper.xml加载到applicationContext.xml中,()



    
 
    
        
            
                classpath:db.properties
            
        
    

    
        
        
        
        
        
        
      


    
       
    

      
        
        

 

第四步: 我们配置studentMapper.xml文件(student类跟数据进行映射的地方)




    
    
        insert into student(stuno,stuname,stuage,garname) values (#{stuNo},#{stuName},#{stuAge},#{garName})
    

第五步:我们要把SqlsessionFactory交给spring,并且要获取sqlsession,mapper 获取到了这些mybatis才能进行映射(在SM中我们使用的是继承SqlSessionDaoSupport 类获取这些重要信息,但是在SSM我们使用第三种扫描包方式获取这些信息),

 


    

    
        
            
                classpath:db.properties
            
        
    

    
        
        
        
        
        
        
      


    
       
    

      
        
        

 
        
               
    

说一下basePackage所在的property的作用:就是将org.lanqiao.mapper(dao层)包中所有的接口产生与之对应的动态代理对象,(对象名就是首字母小写的接口名)这样我们就可以直接提供对象名获取查询(继承SqlSessionDaoSupport获取的加强版)

第六步:在这里我们我们spring-mybatis配置文件就全部配完了,如何我们就开始写dao层跟service层

student类

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;
    private String garName;

    public Student() {
    }

    public Student(int stuNo, String stuName, int stuAge, String garName) {
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.garName = garName;
    }

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public String getGarName() {
        return garName;
    }

    public void setGarName(String garName) {
        this.garName = garName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", stuName='" + stuName + ''' +
                ", stuAge=" + stuAge +
                ", garName='" + garName + ''' +
                '}';
    }
}

dao层 (因为我们在配置文件配了代理对象所以我们不用写dao层实现类)

public interface StudentMapper {
    public  void addStudent(Student student);
    public Student sletcttest(int stuno);
}

service层

public interface StudentService {
    public  void addStudent(Student student);
    public Student sletcttest(int stuNo);
}

 

public class StudentServiceImpl implements StudentService{
    private StudentMapper studentMapper;

    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public void addStudent(Student student) {
        //调用dao层
        studentMapper.addStudent(student);
    }

    @Override
    public Student sletcttest(int stuNo) {
        return studentMapper.sletcttest(stuNo);
    }

}

第七步我们要在spring配置文件中把service类交给spring进行管理 



    
    
        
            
                classpath:db.properties
            
        
    


    


    
    
        
        
        
        
        
        
    
    
    
        
        
        
    
    
        
        
    

然后我们spring-mybatis配置完了我们进行测试一下,看一下能不能获取数据库里的值(这样做的目的是减少SSM整合出现问题的范围,我们能知道问题出现的位置) 如果能获取成功,我们就继续配下去,如果没有就再去查找一下问题(常见问题就是在spring主配置文件获取加载db.properties配置文件,还有加载studentmapper.xml)

第八步:配置springmvc中的web.xml(其实第一步也可以在这里开始进行,前面2-4步都是配置mybatis跟spring整合,现在才是项目加入springmvc支持)我们在web.xml配置中央调度器




  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
  
  
    encodingFilter
    /*
  
  
    springDispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:applicationContext-controller.xml
    
  
  
    springDispatcherServlet
    /
  

因为在这里不用listener监听器(因为会出现问题)所以就注释掉不用,然后我们配置Springmvc.xml文件,在里面加入applicationContext.xml,还有配置视图解析器,还有处理动态资源,和静态资源(后面两个在这里可以不用配置,因为这里不需要)



    

    
        
        
    

    

第九步:就要继续配controller层

@Controller
@RequestMapping("/controller")
public class StudentController {
    @Autowired
    @Qualifier("studentService")
  private   StudentService studentService;

    @RequestMapping("/questStudentByNo/{stuno}")
    public String queryStudentByNo(@PathVariable("stuno") Integer stuNo , Map map){
        System.out.println(stuNo);
        Student student = studentService.sletcttest(stuNo);
        map.put("student",student);
        System.out.println(student.getGarName());

        return "success";
    }
}

在这里可以进行进一步测试,就是看一下前端传的值controller是不是接收到了,studentService是不是获取到了数据库的信息返回到了controller层(在这里我说一下,controller不要通过spring配置文件注入,我们要通过注解注入,不然容易出现问题,切身体会),

最后一步:就是JSP请求页面,跟响应页面(注意:如果controller层获取到了数据,并且返回页面但是通过el表达式没有取到值,要在响应页面中加入isELIgnored="false",因为默认是ture,还有就是不要index.jsp放入到WEB-INF文件夹下面)



Hello World!

 查询一个学生

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


${student}

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

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号