ssm整合步骤
ssm整合
1.导包
2.写配置文件(mybatis--spring--springmvc)
a.写mybatis-config.xml配置文件 里面包含数据的连接信息和映射文件(这里的连接信息也可以删掉 后面会写一个连接数据库的资源文件 在spring里面引用)
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连接池的依赖


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>
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)
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
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