SSM框架快速入门
一、Spring
两大核心思想:
1、IOC(Inverse Of Control:反转控制)
就是将对象的创建交由Spring容器去产生,在使用时直接向容器要对象
开发步骤:
1、创建实体类
Controller层用@Controller,Service层用@Service在类上用于交给Spring容器产生对象
Dao层交给Mybatis产生接口的动态代理对象
用@Autowired或set方法进行对象的依赖注入
2、配置applicationContext.xml
<!--组件扫描 扫描service和mapper包里的注解-->
<context:component-scan base-package="com.itheima">
<!--排除controller的扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
3、Spring集成web
//配置web.xml
<!--全局参数
在Web应用启动时,就加载Spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<!--该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、AOP(Aspect Oriented Programming:面向切面编程)
通过预编译和运行期间动态代理实现程序功能的统一维护;实际上就是通过动态代理技术动态地生成代理对象,来实现对目标方法的增强
<!--声明式事务控制 底层就是使用了AOP的思想-->
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配置事务属性信息-->
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
</aop:config>
二、SpringMVC
开发步骤:
1、配置SpringMVC核心控制器DispathcerServlet
//DispathcerServlet负责执行一些Servlet的共有行为
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、 创建Controller类和视图页面
3、 使用注解@RequestMapping配置Controller类中业务方法的映射地址
4、 配置SpringMVC核心文件 spring-mvc.xml
<!--mvc注解驱动-->
<!--自动加载 RequestMappingHandlerMapping(处理映射器)和
RequestMappingHandlerAdapter( 处 理 适 配 器 ),可用在Spring-xml.xml配置文件中使用-->
<!--例如在对对象或集合进行json格式的字符串转换时,需要配置处理器适配器指定使用jackson进行对象或集合的转换
而使用注解驱动可以替代上述配置-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<!--这样可以在用modelAndView返回视图时,不用加视图名称的前后缀
例如:modelAndView.setViewName("user-list"); 系统会自动将路径匹配成/pages/user-list.jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源权限开放-->
<!--当客户端发送请求时,SpringMVC的前端控制器DispatcherServlet会进行虚拟路径匹配,
对静态资源的访问也进行和@RequestMapping的匹配,所以需要开放静态资源-->
<mvc:default-servlet-handler/>
<!--组件扫描 扫描Controller-->
<!--作用是指定哪个包及其子包下的Bean
需要进行扫描以便识别使用注解配置的类、字段和方法。-->
<context:component-scan base-package="com.itheima.controller"/>
<!--配置权限拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置对哪些资源进行拦截操作-->
<mvc:mapping path="/user/*"/>
<mvc:mapping path="/role/*"/>
<!--配置对哪些资源不进行拦截-->
<mvc:exclude-mapping path="/user/login"/>
<!--PrivilegeInterceptor类里面定义了对资源进行拦截后要进行的操作-->
<bean class="com.itheima.interceptor.PrivilegeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
三、Mybatis
开发步骤:
1、创建Mapper接口
public interface AccountMapper {
public void save(Account account);
public List<Account> findAll();
}
2、创建Mpper映射文件(用于定义sql语句)
<mapper namespace="com.itheima.mapper.AccountMapper">
<insert id="save" parameterType="account">
insert into account values(#{id},#{name},#{money})
</insert>
<select id="findAll" resultType="account">
select * from account
</select>
</mapper>
3、编写Mybatis的核心配置文件sqlMapConfig.xml
<!--定义别名-->
<typeAliases>
<!--手动定义别名<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
<!--自动定义别名,默认为类的名称Account或account-->
<package name="com.itheima.domain"></package>
</typeAliases>
4、Spring整合Mybatis,
//实际上就是将Dao层mapper接口的动态代理对象的创建交给Spring容器去产生
<!--加载propeties文件 jdbc.properties文件里存的时数据酷相关信息-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源信息 这里底层使用的c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis核心文件-->
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
</bean>
<!--扫描mapper映射文件所在的包 由sqlSessionFactory产生的sqlsession为对应的mapper接口创建实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>