ssm整合步骤

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


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




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

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



       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连接池的依赖


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


       b.写到层的接口
       c.写spring的配置文件applicationContext.xml:
         1.引入连接数据库的资源文件db.properties
           <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> 


         2.配置c3p0数据源(class:combopooleDataSource)


         3.配置sqlsessionfactory(class:sqlsessionfactorybean)
           a.引入数据源
           <property name="dataSource" ref="dataSource"></property>
           b.引入mybatis配置文件
           <property name="configLocation" value="classpath:mybatis-config.xml"></property>
           c.映入mybatis映射文件
           <property name="mapperLocations" value="classpath:com/entity/*.xml"></property>


         d.配置整个dao包(class:MapperScannerConfigurer)
           <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
           <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
           <property name="mapperInterface" value="com.dao.PersonDao"></property>
           </bean>
         e.在web.xml中通过listener加载spring(clas:ContextLoaderListener)
         
         f.写个测试类测试spring是否可以使用
           
           //读取spring配置文件
           ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
           //得到spring中解析的dao包中对应的dao接口
           PersonDaos personDaos= (PersonDaos) applicationContext.getBean("personDaos");
           //调用方法




springmvc:引入jar包


        a.写springmvc的配置文件springmvc.xml
          1.配置扫描器
          <context:component-scan base-package="com.controller"></context:component-scan>


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


           <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="prefix" value="/"></property>
                  <property name="suffix" value=".jsp"></property>
           </bean>

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值