SSM框架快速入门

SSM框架快速入门

一、Spring

两大核心思想:

1、IOC(Inverse Of Control:反转控制)

就是将对象的创建交由Spring容器去产生,在使用时直接向容器要对象
开发步骤:
1、创建实体类

Controller层用@ControllerService层用@Service在类上用于交给Spring容器产生对象
Dao层交给Mybatis产生接口的动态代理对象
用@Autowired或set方法进行对象的依赖注入

2、配置applicationContext.xml



    
    

3、Spring集成web

//配置web.xml

<context-param>
  <param-name>contextConfigLocationparam-name>
  <param-value>classpath:applicationContext.xmlparam-value>
context-param>

 
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
 listener>

2、AOP(Aspect Oriented Programming:面向切面编程)

通过预编译和运行期间动态代理实现程序功能的统一维护;实际上就是通过动态代理技术动态地生成代理对象,来实现对目标方法的增强

 
    
    <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: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>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>


    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

2、 创建Controller类和视图页面
3、 使用注解@RequestMapping配置Controller类中业务方法的映射地址
4、 配置SpringMVC核心文件 spring-mvc.xml


    
    
    <mvc:annotation-driven/>

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

    
    
    <mvc:default-servlet-handler/>

   
    
    <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"/>
            
            <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>
        
        
        <package name="com.itheima.domain">package>
    typeAliases>

4、Spring整合Mybatis,

  //实际上就是将Dao层mapper接口的动态代理对象的创建交给Spring容器去产生
   
    <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>

    
    <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>
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:sqlMapConfig-spring.xml">property>
    bean>
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.mapper">property>
    bean>

你可能感兴趣的:(Spring框架学习,spring,java,后端)