SSM Application (6)--SSM Integration

integrated analysis

  First of all, let's review what the three frameworks of Spring, SpringMVC, and MyBatis have done;

  Spring: used to implement bean instantiation and management between beans and beans;

    In SSM integration, each layer is integrated to manage MyBatis, Service, SpringMVC;

  SpringMVC: Instead of servlet, it is used to distribute requests to instances of beans;

    In SSM integration, beans used to manage request dispatch;

  MyBatis: replaces JDBC and is used to implement the dao layer and operate on data;

    In SSM integration, it is recommended to use the proxy mode, and Spring will generate the proxy implementation class in the MyBatis interface;

 

Integration steps

  1. Turn the classes of each layer into instances in Spring;

  2. Establish the relationship between the classes in each layer;

  

    Project directory structure:

  

  3. Integrate the dao layer: scan the mapper interface to generate a bean of the proxy implementation class;

   (1) Turn the proxy implementation class of MyBatis into a bean in Spring;

    The MapperScannerConfigurer class will be used to scan the package, and the interface in the package will be associated with the mapper mapping file to generate a proxy object;

   (2), requires Spring to manage the SqlSessionFactory object and the DataSource object

<!-- Introduce property configuration file --> 
< context:property-placeholder location ="classpath:db.properties" />

<!-- 
    Configure the data source: bean, Spring will instantiate the object through the configuration file, Apache provides the dataSource support class
    Spring can instantiate the DataSource data source object
 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driver}"></property>
    <property name="username" value="${username}"></property>
    <property name="password" value="${password}"></property>
    <property name="url" value="${url}"></property> 
    < property name ="maxActive" value ="10" ></ property > <!-- Maximum number of connections --> 
    < property name ="maxIdle" value ="5" ></ property > <!-- Maximum Simultaneous Active Count --> 
</ bean >
    
<!-- Configure SqlSessionFactory: The management provided by Spring provides a class --> 
< bean id = "sqlSessionFactory"   class = "org.mybatis.spring.SqlSessionFactoryBean" > 
    <!-- Data source configuration --> 
    < property name ="dataSource" ref ="dataSource" ></ property > 
    < property name ="configLocation" value ="classpath:mybatis/SqlMapConfig.xml" ></ property > 
</ bean >
    
<!-- Using package scanning to generate proxy classes for all interfaces under the specified package, the functions of a class provided by Spring and MyBatis integration package --> 
< bean class ="org.mybatis.spring.mapper .MapperScannerConfigurer" > 
    < property name ="basePackage" value ="dao" ></ property > 
    < property name ="sqlSessionFactoryBeanName" value ="sqlSessionFactory" ></ property > 
</ bean >

  

  4. Integrate the service layer: scan the service package, turn the annotated service implementation class into a bean, and realize transaction control;

   (1) Turn the implementation class of the service into a bean in Spring; we can implement it through configuration files and annotations;

    Configure beans based on configuration files

<!-- 基于配置文件 -->
<bean id="authorService" class="service.Impl.AuthorServiceImpl">
    <property name="authorDao" ref="authorDao"></property>
</bean>

      In the service layer, pay attention to implement the get and set methods of the dao layer

public class AuthorServiceImpl implements AuthorService {

    AuthorDao dao;

    public AuthorDao getDao() {
        return dao;
    }

    public  void setDao(AuthorDao dao) {
         this .dao = dao;
    }
}

    Implementation based on annotations:

<!-- Annotation-based: configure which package to scan --> 
< context:annotation-config ></ context:annotation-config > 
< context:component-scan base-package ="service" ></ context:component -scan >

      Similarly, to implement the get and set methods of the dao layer;

@Service("authorService")
public class AuthorServiceImpl implements AuthorService {
    
    @Resource
    AuthorDao dao;

    public AuthorDao getDao() {
        return dao;
    }

    public  void setDao(AuthorDao dao) {
         this .dao = dao;
    }
}

  

  5. Integrate SpringMVC: The essence is a Spring, which is the existence of a servlet, and turns the controller class into an instance it manages;

   (1), configure SpringMVC as a servlet in the web.xml file

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/springMVC.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping> 

   (2), configure the core configuration file of SpringMVC: the instance bean of the controller, the bean of the view resolution;

<!-- Enable annotations --> 
< mvc:annotation-driven ></ mvc:annotation-driven > 
<!-- Package to scan: --> 
< context:component-scan base-package ="controller" > </ context:component-scan >
     
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

  

  6. Here, we divide the core configuration files into different configuration files according to the relationship of the layers, so how can we associate them. What about simultaneous interpretation while the program is running?

    So we need to use the listener provided by Spring to manage all the configured beans and the relationship between beans

    We need to configure the listener in web.xml;

<!-- 
    Spring provides a listener: when Tomcat starts, it reads the Spring configuration file and creates a Spring context
    Create instances and establish relationships between instances
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  This is the end of the SSM integration, I hope it can be helpful to everyone;

 

PS: Due to the limited ability of the author, please forgive me if there are any mistakes;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326104294&siteId=291194637