IDEA整合SSM

IDEA整合SSM,第1张

IDEA整合SSM

目录
  • 1. 创建maven工程并引入依赖
  • 2. 创建Student实体类和数据库student表
  • 3. 编写mapper接口和映射xml文件
  • 4. 创建service层
  • 5. 创建配置文件`applicationContext.xml`
  • 6. 配置web.xml文件
  • 7. 创建SpringMVC配置文件
  • 8. 创建controller层
  • 9. 测试

项目基本目录结构:log4j.properties打印日志,可不要。

1. 创建maven工程并引入依赖
    
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            org.junit.jupiter
            junit-jupiter-api
            ${junit.version}
            test
        
        
            org.junit.jupiter
            junit-jupiter-engine
            ${junit.version}
            test
        
        
            org.springframework
            spring-context
            4.3.18.RELEASE
        
        
            org.springframework
            spring-aop
            4.3.18.RELEASE
        
        
            org.springframework
            spring-beans
            4.3.18.RELEASE
        
        
            org.springframework
            spring-core
            4.3.18.RELEASE
        
        
            org.springframework
            spring-expression
            4.3.18.RELEASE
        
        
            commons-logging
            commons-logging
            1.1.1
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
            commons-pool
            commons-pool
            1.6
        
        
            org.springframework
            spring-jdbc
            4.3.18.RELEASE
        
        
            aopalliance
            aopalliance
            1.0
        
        
            org.springframework
            spring-tx
            5.1.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.9.6
        
        
            org.springframework
            spring-web
            4.3.18.RELEASE
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
            org.springframework
            spring-context-support
            4.3.18.RELEASE
        
        
            org.mybatis
            mybatis
            3.4.6
        

        
            org.springframework
            spring-orm
            4.3.18.RELEASE
        

        
            log4j
            log4j
            1.2.17
        
        
            org.springframework
            spring-webmvc
            4.3.18.RELEASE
        
    

    
        
            
                org.apache.maven.plugins
                maven-war-plugin
                3.3.0
            
        

        
            
                src/main/java
                
                    ***.xml
                
                false
            
        

    


2. 创建Student实体类和数据库student表
package com.yhr.entity;


public class Student {
    private int sno;
    private String sname;
    private int sage;

    public Student(String sname, int sage) {
        this.sname = sname;
        this.sage = sage;
        System.out.println("有参");
    }

    public Student() {
        System.out.println("无参");
    }

    public Student(int sno, String sname, int sage) {
        this.sno = sno;
        this.sname = sname;
        this.sage = sage;
        System.out.println("有参");
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", sname='" + sname + ''' +
                ", sage=" + sage +
                '}';
    }
}

在数据库中添加数据


3. 编写mapper接口和映射xml文件

相当于三层中的DAO层,添加crud方法。
StudentMapper接口:

package com.yhr.mapper;

import com.yhr.entity.Student;

public interface StudentMapper {
    Student queryStudentByNo(int sno);
}

StudentMapper.xml:




    
        select * from student where sno = #{sno}
    


4. 创建service层

StudentService接口:

package com.yhr.service;
import com.yhr.entity.Student;

public interface StudentService {
    Student queryStudentByNo(int sno);
}

实现接口类StudentServiceImpl

package com.yhr.service.impl;

import com.yhr.entity.Student;
import com.yhr.mapper.StudentMapper;
import com.yhr.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;

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

    @Override
    public Student queryStudentByNo(int sno) {
        Student student = studentMapper.queryStudentByNo(sno);
        return student;
    }
}

5. 创建配置文件applicationContext.xml

Spring整合MyBatis的本质就是将MyBatis的SqlSessionFactory交给Spring管理。




    
        
    

    
    
    
        
        
        
        
    
    
        
        
    

    
    
        
        
    


6. 配置web.xml文件


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

	
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext-controller.xml
        
        1
    
    
        springDispatcherServlet
        /
    


7. 创建SpringMVC配置文件



    

    
    
        
        
    
	
    


8. 创建controller层
package com.yhr.controller;

import com.yhr.entity.Student;
import com.yhr.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class StudentController {
	//自动装配,从ioc中获取
    @Autowired
    private StudentService studentService;
	//查询结果放到map中,也就是放到request域中传到success页面
    @RequestMapping("/QueryStudentByNo/{sno}")
    public String QueryStudent(@PathVariable("sno") Integer sno, Map map){
        Student student = studentService.queryStudentByNo(sno);
        map.put("student",student);
        return "success";
    }
}

9. 测试

在index.jsp中手动传值进行测试

查询结果传到success.jsp页面中

得到查询结果

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5563724.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存