ssm整合步骤

                      ssm整合
1.导包
2.写配置文件(mybatis--spring--springmvc)


mybatis: 首先需要在pom.xml中导入mybatis3.4.5的依赖,导入mysql-connector-java-5.1.43

ssm整合步骤_第1张图片



       a.写mybatis-config.xml配置文件 里面包含数据的连接信息和映射文件(这里的连接信息也可以删掉 后面会写一个连接数据库的资源文件 在spring里面引用)

       b.实体类和对应的映射文件(person,person.xml)

ssm整合步骤_第2张图片


       c.dao接口
       d.写个测试类测试Mybatis是否可以使用:
          1.加载配置文件
          SqlSessionFactory sqlSessionFactory=SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis配置文件名"))


          2.通过sqlSessionFactory打开sesssion
          SqlSession sqlSession = sqlSessionFactory.openSession();


          3.拿到接口
          PersonDao pesonDao =sqlSession.getMapper(接口名.class)
          4.调用方法
          5.提交事务,关闭连接
 
spring:引入Stirng对于连接池c3p0的支持Spring-jdbc4,引入mybatis对于Spring的依赖mybatis-spring1,引入c3p0连接池的依赖
ssm整合步骤_第3张图片 ssm整合步骤_第4张图片

       a.写一个连接数据库的资源文件db.properties

ssm整合步骤_第5张图片

       b.写到层的接口
       c.写spring的配置文件applicationContext.xml:
         1.引入连接数据库的资源文件db.properties
            


         2.配置c3p0数据源(class:combopooleDataSource)
ssm整合步骤_第6张图片

         3.配置sqlsessionfactory(class:sqlsessionfactorybean)
           a.引入数据源
           
           b.引入mybatis配置文件
           
           c.映入mybatis映射文件
           


         d.配置整个dao包(class:MapperScannerConfigurer)
           
           
           
           

         e.在web.xml中通过listener加载spring(clas:ContextLoaderListener)
          ssm整合步骤_第7张图片
         f.写个测试类测试spring是否可以使用
           
           //读取spring配置文件
           ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
           //得到spring中解析的dao包中对应的dao接口
           PersonDaos personDaos= (PersonDaos) applicationContext.getBean("personDaos");
           //调用方法




springmvc:引入jar包
ssm整合步骤_第8张图片

        a.写springmvc的配置文件springmvc.xml
          1.配置扫描器
         


          2.视图解析器(class:InternalResourceViewResolver)


           
                 
                 
           

         b.在web.xml中通过servlet加载springmvc

ssm整合步骤_第9张图片

你可能感兴趣的:(ssm整合步骤)