SSM的整合

SSM的整合

  1. pom文件中新增依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.qfedugroupId>
        <artifactId>Days26SSM02artifactId>
        <version>1.0-SNAPSHOTversion>
        <packaging>warpackaging>
    
        <properties>
            
            <spring-version>4.3.6.RELEASEspring-version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring-version}version>
            dependency>
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.8.10version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.44version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.4.4version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>1.3.0version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring-version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring-version}version>
            dependency>
            
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>com.springsource.org.aspectj.weaverartifactId>
                <version>1.6.8.RELEASEversion>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.0.28version>
            dependency>
            
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>1.2.17version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.6version>
                <scope>providedscope>
            dependency>
        dependencies>
    
    
        <build>
            
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.6.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                    configuration>
                plugin>
    
                
                <plugin>
                    <groupId>org.apache.tomcat.mavengroupId>
                    <artifactId>tomcat7-maven-pluginartifactId>
                    <version>2.2version>
                    <configuration>
                        <path>/path>
                        <port>8888port>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
  2. Web.xml

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        
        <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>
        servlet>
        
        <servlet-mapping>
            <servlet-name>dispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>encodefilter-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>encodefilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    
  3. resources下的db.properties

    url=jdbc:mysql://localhost:3306/hello2
    driver=com.mysql.jdbc.Driver
    user=root
    pass=123456
    maxActive=50
    minIdle=1
    
  4. resources下的spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        
        <context:annotation-config />
    
        
        <context:component-scan base-package="com.qfedu.controller" />
        <context:component-scan base-package="com.qfedu.service" />
    
        
        <mvc:annotation-driven />
        
        
        <mvc:default-servlet-handler />
    
        
        <import resource="classpath:spring-mybatis.xml" />
    beans>
    
  5. resource下的spring-mybatis.xml


<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

  
  <context:property-placeholder location="classpath:db.properties" />

  
  <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
      
      
      
      
      <property name="url" value="${url}">property>
      <property name="driverClassName" value="${driver}" />
      <property name="username" value="${user}" />
      <property name="password" value="${pass}" />
  bean>

  
  
  <bean id="sf" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="typeAliasesPackage" value="com.qfedu.pojo" />
      <property name="mapperLocations" value="classpath:com/qfedu/dao/mapper/*Mapper.xml" />
      <property name="dataSource" ref="ds">property>
      <property name="configLocation" value="classpath:mybatis-config.xml" />
  bean>

  
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.qfedu.dao" />
      <property name="sqlSessionFactoryBeanName" value="sf" />
  bean>

  
  <bean id="dtx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="ds">property>
  bean>

  <tx:advice id="tx" transaction-manager="dtx">
      
      <tx:attributes>
          <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" />
          <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" />
          <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" />
          <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" />
      tx:attributes>
  tx:advice>

  
  <aop:config>
      <aop:pointcut id="mpt" expression="execution(* com.qfedu.service.*.*(..))" />
      <aop:advisor advice-ref="tx" pointcut-ref="mpt" />
  aop:config>
beans>

  1. resources下的mybatis-config.xml

    
    
    <configuration>
       <settings>
          <setting name="logImpl" value="LOG4J" />
       settings>
    configuration>
    
  2. resource下的log4j.properties

    # Global logging configuration
    log4j.rootLogger=ERROR, stdout, F
    # MyBatis logging configuration...
    log4j.logger.com.qfedu.dao=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.F.File =myproj.log
    log4j.appender.F.Append = true
    log4j.appender.F.Threshold = DEBUG
    log4j.appender.F.layout=org.apache.log4j.PatternLayout
    log4j.appender.F.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss}-[%p %F\:%L]  %m%n
    
  3. 配置文件结束,环境搭建完毕,开始编写代码, User.java

    package com.qfedu.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
    
        private int uid;
        private String username;
        private String password;
        private int age;
        private String addr;
    }
    
  4. UserController.java

    package com.qfedu.controller;
    
    import com.qfedu.pojo.User;
    import com.qfedu.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    public class UserController {
    
        /**
         * 动态注入service对象
         */
        @Resource
        private IUserService userService;
    
        @GetMapping("/Users")
        public List<User> getUsers(){
            return  userService.getAllUsers();
        }
    }
    
  5. IUserService.java

    package com.qfedu.service;
    
    import java.util.List;
    
    import com.qfedu.pojo.User;
    
    public interface IUserService {
    
       List<User> getAllUsers();
    }
    
  6. UserServiceImpl.java

    package com.qfedu.service.impl;
    
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import com.qfedu.dao.IUserDao;
    import com.qfedu.pojo.User;
    import com.qfedu.service.IUserService;
    import org.springframework.stereotype.Service;
    
    /**
     * 注意Service的实现类之上一定要添加service注解
     */
    @Service
    public class UserServiceImpl implements IUserService {
    
    	/**
    	 * 动态注入dao组件
    	 */
    	@Resource
    	private IUserDao userDao;
    
    	@Override
    	public List<User> getAllUsers() {
    		return userDao.getAllUsers();
    	}
    }
    
  7. IUserDao.java

    package com.qfedu.dao;
    
    import com.qfedu.pojo.User;
    
    import java.util.List;
    
    public interface IUserDao {
    
       List<User> getAllUsers();
    }
    
  8. dao/mapper下新增UserMapper.xml

    
    
    
       <mapper namespace="com.qfedu.dao.IUserDao">
       
       <select id="getAllUsers" resultType="user">
          select * from user
       select>
    mapper>
    
  9. 启动tomcat服务器插件,访问http://localhost:8888/Users,得到结果
    SSM的整合_第1张图片

你可能感兴趣的:(SSM的整合)