ssm整合:搭建环境

一、pom.xml导入依赖:

 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>

二、搭建spring,配置IOC,在resources文件夹下创建applicationContext.xml,并将log4j.properties加入resources文件夹下

导入约束:

 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>

开启注解扫描:

1     <!--开启注解扫描,只希望处理service和dao,controller不需要Spring框架去处理-->
2     <context:component-scan base-package="cn.flypig666">
3         <!--配置哪些注解不扫描-->
4         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
5     </context:component-scan>

测试spring:

1     @Test
2     public void run1(){
3         // 加载配置文件
4         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
5         //获取对象
6         AccountService as = (AccountService) ac.getBean("AccountService");
7         //调用方法
8         as.findAll();
9     }

三、搭建springmvc环境

1、在web.xml中配置前端控制器,解决中文乱码的过滤器

<!--配置前端控制器-->
  <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>
  </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、配置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">

    <!--开启注解扫描,只扫描Controller注解-->
    <context:component-scan base-package="cn.flypig666">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!--配置的视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--过滤静态资源-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <!--开启SpringMVC注解的支持-->
    <mvc:annotation-driven/>

</beans>

测试:

jsp:

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

AccountController.java

 1 @Controller
 2 @RequestMapping("/account")
 3 public class AccountController {
 4 
 5     @RequestMapping("/findAll")
 6     public String findAll(){
 7         System.out.println("表现层:查询所有账户....");
 8         return "list";
 9     }
10 
11 }

四、spring整合springmvc框架

启动tomcat服务器的时候,需要加载spring的配置文件:(写一个监听器去加载spring配置文件)

  ServletContext域对象:只创建一次,服务器启动的时候创建,服务器关闭才销毁

  一类监听器:监听ServletContext域对象创建和销毁,执行一次,服务器启动时执行

  监听器去加载spring的配置文件,创建WEB版本工厂,存储ServletContext对象

配置applicationContext.xml,在web-app中写入以下内容:

<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.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>
 1 @Controller
 2 @RequestMapping("/account")
 3 public class AccountController {
 4     @Autowired
 5     private AccountService accountService;
 6 
 7     @RequestMapping("/findAll")
 8     public String findAll(){
 9         System.out.println("表现层:查询所有账户....");
10         //调用service(业务层)的方法
11         accountService.findAll();
12         return "list";
13     }
14 }

五、Spring整合mybatis框架

1、搭建和测试mybatis的环境,创建SqlMapConfig.xml(后面可以删掉)

 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个基本信息-->
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>

测试:

 1     @Test
 2     public void run1() throws Exception {
 3         // 加载配置文件
 4         InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
 5         // 创建SqlSessionFactory对象
 6         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
 7         // 创建SqlSession对象
 8         SqlSession session = factory.openSession();
 9         // 获取到代理对象
10         AccountDao dao = session.getMapper(AccountDao.class);
11         //查询所有数据
12         List<Account> list = dao.findAll();
13         for (Account account : list) {
14             System.out.println(account);
15         }
16         //关闭资源
17         session.close();
18         in.close();
19     }

2、在applicationContext.xml配置整合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>

六、配置spring框架声明式事务管理

applicationContext.xml:

 1 <!--配置spring框架声明式事务管理-->
 2     <!--配置事务管理器-->
 3     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 4         <property name="dataSource" ref="dataSource" />
 5     </bean>
 6     <!--配置事务通知-->
 7     <!--引入事务管理器-->
 8     <tx:advice id="txAdvice" transaction-manager="transactionManager">
 9         <tx:attributes>
10             <!--表示find开头的方法,是只读的-->
11             <tx:method name="find*" read-only="true"/>
12             <tx:method name="*" isolation="DEFAULT"/>
13         </tx:attributes>
14     </tx:advice>
15 
16     <!--配置AOP增强-->
17     <aop:config>
18         <!--引入事务通知,pointcut为切入点的表达式-->
19         <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.flypig666.service.impl.*ServiceImpl.*(..))"/>
20     </aop:config>

 

上一篇:08 SSM整合案例(企业权限管理系统):09.用户操作


下一篇:常用模块【三】正则表达式