ssm Integration: build environment

A, pom.xml dependency introduced:

 1   <properties>
 2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3     <maven.compiler.source>1.8</maven.compiler.source>
 4     <maven.compiler.target>1.8</maven.compiler.target>
 5     <spring.version>5.0.2.RELEASE</spring.version>
 6     <slf4j.version>1.6.6</slf4j.version>
 7     <log4j.version>1.2.12</log4j.version>
 8     <shiro.version>1.2.3</shiro.version>
 9     <mysql.version>5.1.6</mysql.version>
10     <mybatis.version>3.4.5</mybatis.version>
11   </properties>
  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!-- log end -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>

  </dependencies>

Second, build a spring, configured IOC, created applicationContext.xml in resources folder and add log4j.properties resources folder

Import restrictions:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:context="http://www.springframework.org/schema/context"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8        http://www.springframework.org/schema/beans/spring-beans.xsd
 9        http://www.springframework.org/schema/tx
10        http://www.springframework.org/schema/tx/spring-tx.xsd
11        http://www.springframework.org/schema/aop
12        http://www.springframework.org/schema/aop/spring-aop.xsd
13        http://www.springframework.org/schema/context
14        http://www.springframework.org/schema/context/spring-context.xsd">
15 </beans>

Open comments Scan:

1      <! - Open comments scanning, and only want to deal with service dao, controller does not need to handle the Spring Framework ->
 2      <context: Scan-base- Component Package = "cn.flypig666">
 . 3          <- configure what! scan an annotation ->
 . 4          <context: the exclude filter-type = "annotation" = expression The "org.springframework.stereotype.Controller" />
 . 5      </ context: Component-scan>

Test spring:

. 1      @Test
 2      public  void runl () {
 . 3          // load configuration file 
. 4          the ApplicationContext AC = new new the ClassPathXmlApplicationContext ( "CLASSPATH: the applicationContext.xml" );
 . 5          // get the object 
. 6          the AccountService AS = (the AccountService) ac.getBean ( "the AccountService " );
 7          // call the method 
. 8          as.findAll ();
 . 9      }

Third, build springmvc environment

1, the front end of the controller configuration in web.xml solution Chinese distortion filter

<!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</ URL-pattern> 
  <-! solve Chinese distortion filter ->
  </ Mapping the servlet->

  <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, the configuration springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context 
       HTTP: // www.springframework.org/schema/context/spring-context.xsd ">
 
    <- Open comments scans, only scans Controller Notes ->! 
    <context: component- base- Scan Package = "cn.flypig666"> 
        <context: the include filter-type = "Annotation" = expression the "org.springframework.stereotype.Controller" /> 
    </ context: Component-Scan> 
    
    <- configuration view! parser object -> 
    <the bean ID = "InternalResourceViewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <Property name = "prefix" value = "/ the WEB-INF / Pages /" /> 
        <property name="suffix" value=".jsp"/>
    </bean> 
    <-! Filter Static Resources ->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <!--开启SpringMVC注解的支持-->
    <mvc:annotation-driven/>

</beans>

test:

jsp:

    <a href="account/findAll">测试</a>

AccountController.java

. 1  @Controller
 2 @RequestMapping ( "/ Account" )
 . 3  public  class the AccountController {
 . 4  
. 5      @RequestMapping ( "/ the findAll" )
 . 6      public String the findAll () {
 . 7          System.out.println ( "presentation layer: Discover all accounts .. .. " );
 . 8          return " List " ;
 . 9      }
 10  
. 11 }

Four, spring springmvc integration framework

Start tomcat server when you need to load the spring configuration file :( wrote one listener to load the spring configuration file)

  ServletContext domain objects: Create only once, create a server startup, the server shuts down before destruction

  A class listener: listening ServletContext domain objects are created and destroyed, once, when the server starts execution

  Listeners to load the spring configuration file, create version WEB factories, storage ServletContext object

Configuration applicationContext.xml, writes the following in the web-app:

<! - Configure Spring listeners, applicationContext.xml default configuration file to load only the WEB-INF directory -> 
  <listener> 
    <listener- class > org.springframework.web.context.ContextLoaderListener </ listener- class > 
  </ listener> 
  <-! path setting configuration file -> 
  <context-param> 
    <param-name> the contextConfigLocation </ param-name> 
    <-! profile class path under load -> 
    <param- value> CLASSPATH: the applicationContext.xml </ param-value> 
  </ context-param>
. 1  @Controller
 2 @RequestMapping ( "/ Account" )
 . 3  public  class the AccountController {
 . 4      @Autowired
 . 5      Private the AccountService AccountService;
 . 6  
. 7      @RequestMapping ( "/ the findAll" )
 . 8      public String the findAll () {
 . 9          System.out.println ( " Presentation layer: Discover all accounts .... " );
 10          // method call service (business layer) of 
11          accountService.findAll ();
 12          return " List " ;
 13      }
 14 }

Five, Spring Framework integration mybatis

1, build and test mybatis environment, create SqlMapConfig.xml (can be removed later)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!--配置环境-->
 7     <environments default="mysql">
 8         <environment id="mysql">
 9             <transactionManager type="JDBC"></transactionManager>
10             <dataSource type="POOLED">
11                 <!- 4 Basic Configuration database connection ->
12                 <property name="driver" value="com.mysql.jdbc.Driver"/>
13                 <property name="url" value="jdbc:mysql://localhost:3307/ssm"/>
14                 <property name="username" value="root"/>
15                 <property name="password" value="flypig"/>
16             </dataSource>
17         </environment>
18     </environments>
19     <!--引入映射配置文件-->
20     <mappers>
21         <!--<mapper class="cn.flypig666.dao.AccountDao"/>-->
22         <package name="cn.flypig666.dao" />
23     </mappers>
24 
25 </configuration>

test:

. 1      @Test
 2      public  void runl () throws Exception {
 . 3          // load configuration file 
. 4          the InputStream in Resources.getResourceAsStream = ( "the SqlMapConfig.xml" );
 . 5          // Create a SqlSessionFactory 
. 6          SqlSessionFactory Factory = new new . The SqlSessionFactoryBuilder () Build ( in);
 . 7          // Create the SqlSession 
. 8          SqlSession the session = factory.openSession ();
 . 9          // acquired proxy object 
10          AccountDao DAO = session.getMapper (AccountDao. class );
 . 11         // Search data 
12 is          List <the Account> List = dao.findAll ();
 13 is          for (the Account Account: List) {
 14              System.out.println (Account);
 15          }
 16          // Close the resource 
. 17          Session.close () ;
 18 is          in.close ();
 . 19      }

2, in the integration configuration applicationContext.xml frame MyBatis

 1 <!--Spring整合MyBatis框架-->
 2     <!--配置连接池-->
 3     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 4         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
 5         <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/ssm"/>
 6         <property name="user" value="root"/>
 7         <property name="password" value="flypig"/>
 8     </bean>
 9     <!--配置SqlSessionFactory工厂-->
10     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
11         <property name="dataSource" ref="dataSource"/>
12     </bean>
13 
14     <!--配置AccountDao接口所在包-->
15     <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
16         <property name="basePackage" value="cn.flypig666.dao"/>
17     </bean>

Sixth, configure spring framework for declarative transaction management

applicationContext.xml:

1 <! - arranged spring framework declarative transaction management ->
 2      <! - Configure the transaction manager ->
 . 3      <the bean ID = "the transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
 . 4          <Property name = "the dataSource" REF = "the dataSource" />
 . 5      </ the bean>
 . 6      <-! configuration transaction notification ->
 7      <- - introduced transaction manager!>
 . 8      <TX: the advice ID = " txAdvice "Transaction Manager-=" the transactionManager ">
 . 9          <TX: Attributes>
 10              ! <- find a method to indicate the beginning, is read-only ->
 . 11              <TX: method name ="
find*" read-only="true"/>
12             <TX: Method name = "*" Isolation = "the DEFAULT" />
 13 is          </ TX: Attributes>
 14      </ TX: the advice>
 15  
16      <-! AOP enhanced configuration ->
 . 17      <AOP: config>
 18 is          < ! - the introduction of the transactional advice, pointcut as the starting point of expression ->
 19          <AOP:. Advisor advice-ref = "txAdvice" pointcut = "Execution (* cn.flypig666.service.impl ServiceImpl * * (... )) "/>
 20 is      </ AOP: config>

 

Guess you like

Origin www.cnblogs.com/flypig666/p/11518550.html