SSM整合教程(附代码)

SSM整合教程(附代码),第1张

SSM整合教程(附代码) SSM整合 1、引入Maven依赖
  • SpringMVC
  • Spring
  • MyBatis
  • MyBatis整合Spring适配包
  • 数据库连接池 - 数据库连接驱动
  • Servlet - api

    
    
        junit
        junit
        4.12
    
    
    
        mysql
        mysql-connector-java
        5.1.47
    
    
    
        com.mchange
        c3p0
        0.9.5.2
    
 
    
    
        javax.servlet
        servlet-api
        2.5
    
    
        javax.servlet.jsp
        jsp-api
        2.2
    
    
        javax.servlet
        jstl
        1.2
    
 
    
    
        org.mybatis
        mybatis
        3.5.2
    
    
        org.mybatis
        mybatis-spring
        2.0.2
    
 
    
    
        org.springframework
        spring-webmvc
        5.1.9.RELEASE
    
    
        org.springframework
        spring-jdbc
        5.1.9.RELEASE
    

Maven资源过滤设置


    
        
            src/main/java
            
                ***.xml
            
            false
        
        
            src/main/resources
            
                ***.xml
            
            false
        
    

  • 用于避免资源未编译加载,作用是指定需加载的资源
2、编写SSM整合关键配置文件 2.1 Src下包结构
  • controller
  • mapper
  • service
  • pojo
2.2 resource下的SSM配置文件 1.数据源文件:database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
  • 上述jdbc.url 设置了编码与时区
2.mybatis核心文件:mybatis-config.xml



    
    
        
    

  • 仅需要在此核心配置文件中配置扫描别名路径即可,数据源配置留在spring容器中配置
3.Spring容器文件:applicationContext.xml
  • 将spring-dao.xml 、 spring-service.xml 、spring-mvc.xml 串联起来
    
    
  • 此两个配置文件将会导入到同一个文件(applicationContext.xml)中,所以他们作用域共享,类似于本质定义再一个配置文件中
4.Spring数据源文件:spring-dao.xml

*** 作:关联数据源配置文件 + 配置DataSource数据源 + 创建SqlSessionFactory工厂

1.关联数据源配置文件

2.配置DataSource数据源

3.创建SqlSessionFactory

spring文件头部内容:



    

4.1连接池配置

方式一:Spring自带原生配置

	
    
        
            classpath:database.properties
        
    
    
    
        
        
        
        
    

方式二 c3p0连接池配置

  • 自动化 *** 作(自动化加载配置文件,并且自动设置至对象中)



    
	
    
    
    
    
    
    
    
    
    

4.2 配置SqlSessionFactory工厂对象()
  • 注入数据源
  • 绑定配置文件

方式一: sqlSessionFactoryBeanName


	
    



	
    
    
    

5.spring-service.xml
  • 扫描service包,开启注解支持

  • 注入业务类

  • 开启声明式事务

Spring头文件





1.扫描service包

2.将业务类注入到Spring

方式一:xml注入

前提:service实现类中存在实现mapper属性并且存在set方法,类似于工厂模式,为mapper接口定义实现类


	

方式二:注解注入

  • @Service:定义在service实现类上方,可通过(“具体实现接口名”)来进行实现注入

  • @Autowired:定义在mapper属性上方,对mapper进行装配

实现代码如下:

@Service("ArticleService")
public class xxxServiceImpl implements xxxService {

    @Autowired
    private ArticleMapper xxxMapper;
}
3.声明式事务配置

    
	

4.aop事务支持 6.Spring数据源文件:spring-mvc.xml

干mvc干的事

  • 注解驱动
  • 静态资源过滤
  • 扫描包:controller
  • 视图解析器
1.注解驱动

2.静态资源过滤

3.扫描包:controller

4.视图解析器

    
    
    
    

2.3 web.xml的配置
  • 用于配置 SpringMVC 环境
  • 编码过滤
1.配置DispactchServlet
    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        
        1
    
	
    
        springmvc
        /
    
2.乱码过滤

	encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
    	encoding
        utf-8
    



	encodingFilter
    
    
}

StudentMapper.java

public interface StudentMapper {

    List getStudentList();

}

StudentMapper.xml






    
        select * from student
    

StudentService.java

public interface StudentService {

    List getStudentList();

}

StudentServiceImpl.java

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;

    public StudentMapper getStudentMapper() {
        return studentMapper;
    }

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

    public List getStudentList() {
        return studentMapper.getStudentList();
    }
}

StudentController.java

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/getInfoList") // 上下文容器获取bean
    public ModelAndView getStudentList(ModelAndView modelAndView){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = (StudentService)ctx.getBean("serviceImpl");
        List studentList = studentService.getStudentList();
        modelAndView.addObject("students",studentList);
        modelAndView.setViewName("helloSSM");
        return modelAndView;
    }
    
    @Autowired
    private StudentService studentService;
    @RequestMapping("/getInfos")	// 注解获取bean
    public ModelAndView getStudents(ModelAndView modelAndView){
        List studentList = studentService.getStudentList();
        modelAndView.addObject("students",studentList);
        modelAndView.setViewName("helloSSM");
        return modelAndView;
    }

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


    Title


    ${students}


最终测试:

http://localhost:8080/SSM/student/getInfoList

效果如下:

核心概念:我们将dao、service、mvc区分为了三个xml文件,这样更便于理解与应用

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

原文地址: http://outofmemory.cn/zaji/5676587.html

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

发表评论

登录后才能评论

评论列表(0条)

保存