1.Spring
1.IOC容器,装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,就不用在每次使用这个类的时候为它初始化,很少看到关键字new。
2.spring的aop
3.配置文件:applicationContext.xml
2.SpringMvc
1.配置文件:-servlet.xml
3.Mybatis
1.使用官方网站的Mapper自动生成工具:mybatis-generator-core1.3.2,来生成po类和Mapper映射文件
2.配置文件:SqlMapConfig.xml
3.dao:接口
mapping:相当于原来的dao
动态sql: if,choose,when,otherwise,where,trim,set,foreach
d:
cd F:\框架\mybatis-generator-core-1.3.2\lib
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
ssm整合:
1.applicationContext:
(1)web.xml中:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
(2)applicationContext中:
A.扫描包:
<context:component-scan base-package="com.tentact.student1"></context:component-scan>
B.载入jdbc字符串:
(PropertyPlaceholderConfigurer: spring管理属性配置文件properties,对于属性配置,一般采用的是键值对的形式,如:key=value
属性配置文件一般使用的是XXX.properties,当然有时候为了避免eclipse把properties文件转码,放到服务器上认不出中文,可以采用XXX.conf的形式管理属性配置。
spring对于属性文件有自己的管理方式,通过spring的管理,可以直接使用@Value的方式直接得到属性值。
使用org.springframework.beans.factory.config.PropertiesFactoryBean对属性配置文件进行管理。)
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:spring-jdbc.properties</value>
</list>
</property>
</bean>
C.数据源 c3p0:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<!--链接池中保留的最小连接数-->
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<!--链接池中保留的最大连接数-->
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<!--当连接池中的连接耗尽的时候,c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
*D:sessionFaction将spring和mybatis整合: *
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mapping1/*.xml" /> <!-- 加载mapper文件 -->
</bean>
E:dao mapping接口扫描
```c
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value=" com.tentact.student1.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
**F**:事务
```c
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.tentact.student1.service.*.*(..))"
id="pointCut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
</aop:config>
2.SqlMapConfig.xml
<mappers>
<mapper resource="com/web/mapping/DepartmentMapper.xml" />
<mapper resource="com/web/mapping/EmployeeMapper.xml" />
<mapper resource="com/web/mapping/MeetingEmployeeMapper.xml" />
<mapper resource="com/web/mapping/MeetingMapper.xml" />
<mapper resource="com/web/mapping/MeetingRoomMapper.xml" />
</mappers>
3.springMVC-servlet.xml
**A:**注解方式
<mvc:annotation-driven validator=“validator”/>
**B:**扫描包
<context:component-scan base-package=“com.tentact.student1”></context:component-scan>
C:验证jsr303
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
</bean>
<bean id="validatemessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validatemessages" />
<property name="fileEncodings" value="utf-8" />
<property name="cacheSeconds" value="120" />
</bean>