SSM(SpringMVC+Spring+Mybatis)整合

一、导包:
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
   
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
  
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
  
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
  
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>
  
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.2</version>
</dependency>
   
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
</dependency>
   
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
  
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>asm</groupId>
    <artifactId>asm-commons</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.17.1-GA</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.8</version>
</dependency>
二、准备数据库表:
    #创建数据库ssm
       DROP DATABASE IF EXISTS ssm;
       CREATE DATABASE ssm;
       USE ssm;
    #创建书本表
       DROP TABLE IF EXISTS ssm_book;
       CREATE TABLE ssm_book (
         id INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,
         NAME VARCHAR(111),
         detail VARCHAR(256),
         userId INT(22)
      );
    INSERT INTO ssm_book(id, NAME,detail,userId) VALUES
       (1,"javaWeb","JavaWeb从入门到精通",1),
       (2,"PhotoShop","图片图像处理",1),
       (3,"mysql从入门到精通","数据库专业书籍",2);
  
三、创建POJO:Book.java
     public class Book implements Serializable {
         private static final long serialVersionUID = 1L;
         private Integer id;
         private String name;
         private String detail;
         private Integer userId;
      }
   
四、创建jdbc.properties文件
      jdbc.driver=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
      jdbc.username=root
      jdbc.password=admin
 
五、创建applicationContext.xml配置文件 
      <!-- 配置读取properties文件jdbc.properties以连接数据库 -->
       <context:property-placeholder location="classpath:jdbc.properties"/>
       <!-- 配置数据源-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="${jdbc.driver}"/>
          <property name="jdbcUrl" value="${jdbc.url}"/>
          <property name="user" value="${jdbc.username}"/>
          <property name="password" value="${admin}"/>
       </bean>
      <!-- 配置sqlSessionFactory -->
      <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.ssm.pojo"/>
      </bean>
      <!-- 配置mapper扫描器 -->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.ssm.mapper"/>
      </bean>
      <!-- 配置service扫描器 -->
        <context:component-scan base-package="com.ssm.service"/>
      <!-- 配置事务-->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
      </bean>
      <!--配置事务-通知  -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
           <tx:method name="add*" propagation="REQUIRED"/>
           <tx:method name="delete*" propagation="REQUIRED"/>
           <tx:method name="find*" propagation="REQUIRED"/>
           <tx:method name="edit*" propagation="REQUIRED"/>
         </tx:attributes>
     </tx:advice>
     <!-- 配置事务-切面 -->
      <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm.service.*.*(..))"/>
      </aop:config>
六、创建springmvc-config.xml文件
     <!-- 配置Controller扫描 -->
    <context:component-scan base-package="com.ssm.controller"/>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 配置静态资源响应 -->
    <mvc:default-servlet-handler/>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"/>
      <property name="suffix" value=".jsp"/>
    </bean>
 
 七、配置web.xml
    <!-- needed for ContextLoaderListener -->
     <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
     <!-- Bootstraps the root web application context before servlet initialization -->
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     
     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
     <servlet>
          <servlet-name>springDispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc-config.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
     </servlet>
     <!-- Map all requests to the DispatcherServlet for handling -->
     <servlet-mapping>
          <servlet-name>springDispatcherServlet</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
  </filter>
  <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
 
 八、创建映射Mapper:
   BookMapper.java
     public interface BookMapper {
        public List<Book> findAll();
      }
    BookMapper.xml
    <mapper namespace="com.ssm.mapper.BookMapper">
       <select id="findAll" resultType="Book">
         select * from ssm_book
       </select>
     </mapper>
 九、创建服务层:
       IBookService.java
     public interface IBookService {
        public List<Book> findAllBook();
      }
    BookServiceImpl.java
    @Service
    public class BookServiceImpl implements IBookService {
     @Autowired
     private BookMapper bookMapper;
     @Override
     public List<Book> findAllBook() {
          return bookMapper.findAll();
     }
  }
十、创建控制器:BookController.java
   @Controller
  public class BookController {
     @Autowired
     private IBookService bookService;
     @RequestMapping(value="/main")
     public String Main(Model model) {
          List<Book> books = bookService.findAllBook();
          model.addAttribute("books",books);
          return "main";
      }
   }
 十一、创建视图:main.jsp
   
 
posted @ 2019-06-21 16:41  #独狼  阅读(164)  评论(0编辑  收藏  举报