zoukankan      html  css  js  c++  java
  • SSM整合 Spring+SpringMVC + Mybatis

    SSM整合

    • 项目基本结构

    image

    image

    • pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <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.0</modelVersion>
      
          <groupId>com.luoshen</groupId>
          <artifactId>ssm-book</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <dependencies>
              <!--Junit-->
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
              </dependency>
              <!--数据库驱动-->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>5.1.47</version>
              </dependency>
              <!-- 数据库连接池 -->
              <dependency>
                  <groupId>com.mchange</groupId>
                  <artifactId>c3p0</artifactId>
                  <version>0.9.5.2</version>
              </dependency>
              <!--Servlet - JSP -->
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>servlet-api</artifactId>
                  <version>2.5</version>
              </dependency>
              <dependency>
                  <groupId>javax.servlet.jsp</groupId>
                  <artifactId>jsp-api</artifactId>
                  <version>2.2</version>
              </dependency>
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>jstl</artifactId>
                  <version>1.2</version>
              </dependency>
              <!--Mybatis-->
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis</artifactId>
                  <version>3.5.2</version>
              </dependency>
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis-spring</artifactId>
                  <version>2.0.2</version>
              </dependency>
              <!--Spring-->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>5.1.9.RELEASE</version>
              </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-jdbc</artifactId>
                  <version>5.1.9.RELEASE</version>
              </dependency>
              <!--Lombok-->
              <dependency>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
                  <version>1.18.12</version>
              </dependency>
              <!--事务-->
              <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjweaver</artifactId>
                  <version>1.8.13</version>
              </dependency>
          </dependencies>
      
          <!--在build中配置resources,来防止我们资源导出失败的问题-->
          <build>
              <resources>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>true</filtering>
                  </resource>
                  <resource>
                      <directory>src/main/java</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>true</filtering>
                  </resource>
              </resources>
          </build>
      
    • mybatis-config.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration
              PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-config.dtd">
      <configuration>
          <!--别名-->
          <typeAliases>
              <package name="com.luoshen.pojo"/>
          </typeAliases>
      
          <!--mapper映射-->
          <mappers>
              <package name="com.luoshen.mapper"/>
          </mappers>
      </configuration>
      
    • db.properties

      jdbc.driver=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/luoshen?serverTimezone=CST&useUnicode=true&characterEncoding=utf-8&useSSL=true
      jdbc.username=root
      jdbc.password=root
      
    • spring-dao.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
          <!--导入数据库配置文件-->
          <context:property-placeholder location="classpath:db.properties"/>
      
          <!--配置连接池-->
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource ">
              <property name="driverClass" value="${jdbc.driver}"/>
              <property name="jdbcUrl" value="${jdbc.url}"></property>
              <property name="user" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
      
              <property name="maxPoolSize" value="30"/>
              <property name="minPoolSize" value="10"/>
              <!--关闭连接后不自动提交-->
              <property name="autoCommitOnClose" value="false"/>
              <!--获取连接超时时间-->
              <property name="checkoutTimeout" value="1000"/>
      
              <!--获取连接失败重试次数-->
              <property name="acquireRetryAttempts" value="2"/>
          </bean>
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <!--绑定mybatis配置文件   -->
              <property name="configLocation" value="classpath:mybatis-config.xml"/>
          </bean>
      
              <!--配置dao接口扫描动态实现dao接口注入spring-->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <!--注入sqlSessionFactory-->
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
              <!--扫描dao-->
              <property name="basePackage" value="com.luoshen.mapper"/>
          </bean>
      </beans>
      
    • spring-service.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
          <!--扫描service包 把所以带@Service @Autowired 注解的注册到spring-->
          <context:component-scan base-package="com.luoshen.service"/>
      
          <!--事务-->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"/>
          </bean>
      
      </beans>
      
    • spring-mvc.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:context="http://www.springframework.org/schema/context"
             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
             https://www.springframework.org/schema/context/spring-context.xsd">
      
          <import resource="classpath:spring-dao.xml"></import>
          <import resource="classpath:spring-service.xml"></import>
      
          <!--注解替换原始配置-->
          <!--自动扫描包让包下面的注解生效-->
          <context:component-scan base-package="com.luoshen.controller"/>
          <!--让springmvc不处理静态资源.css .js .mp3等-->
          <mvc:default-servlet-handler/>
          <!--开启注解驱动    默认就配置了HandlerMapping HandlerAdapter-->
          <mvc:annotation-driven/>
      
          <!--配置处理器映射器  -->
          <!-- DispatchServlet把请求发给HandlerMapping-->
          <!--HandlerMapping根据链接找到handler (spring中的每个bean就是一个handler)-->
          <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>-->
          <!--配置处理器适配器-->
          <!--负责执行DispatchServlet发送的handler-->
          <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>-->
      
      
          <!--视图解析器-->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
      
      </beans>
      
    • web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <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_4_0.xsd"
               version="4.0">
          <!--1、DispatcherServlet-->
          <servlet>
              <servlet-name>spring-mvc</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <!--此处不能单绑定spring-mvc.xml的配置文件,这样找不到service层的bean-->
                  <param-value>classpath:spring-mvc.xml</param-value>
              </init-param>
              <!--和tomcat一起启动-->
              <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
              <servlet-name>spring-mvc</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
          <!--2、post乱码过滤-->
          <filter>
              <filter-name>springEncodingFilter</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>springEncodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
      
          <!--session过期时间-->
          <session-config>
              <session-timeout>15</session-timeout>
          </session-config>
      </web-app>
      
    • User.java

      package com.luoshen.pojo;
      
      
      import lombok.AllArgsConstructor;
      import lombok.Data;
      import lombok.NoArgsConstructor;
      
      @Data//会缺少有参构造
      @AllArgsConstructor//有参构造
      @NoArgsConstructor//无参构造
      public class User {
          private int id;
          private String name;
      
      
      }
      
      
    • UserMapper.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper
              PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.luoshen.mapper.UserMapper">
          <select id="findAll" resultType="user">
              select * from user
          </select>
      </mapper>
      
    • UserMapper.java

      package com.luoshen.mapper;
      
      import com.luoshen.pojo.User;
      
      import java.util.List;
      
      public interface UserMapper {
          List<User> findAll();
      }
      
      
    • UserService.java

      package com.luoshen.service;
      
      import com.luoshen.pojo.User;
      
      import java.util.List;
      
      public interface UserService {
          List<User> findAll();
      }
      
      
    • UserServiceImpl.java

      package com.luoshen.service;
      
      import com.luoshen.mapper.UserMapper;
      import com.luoshen.pojo.User;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      
      @Service
      public class UserServiceImpl implements UserService {
      
          @Autowired
          private UserMapper userMapper;
      
          public List<User> findAll() {
              return userMapper.findAll();
          }
      }
      
      
    • UserController.java

      package com.luoshen.controller;
      
      import com.luoshen.pojo.User;
      import com.luoshen.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import java.util.List;
      
      @Controller
      @RequestMapping("index")
      public class UserController {
          @Autowired
          private UserService userService;
          @RequestMapping("find")
          public String findAll(Model model){
              List<User> all = userService.findAll();
              for (User user : all) {
                  System.out.println(user);
              }
      
              model.addAttribute("all",all);
              return "user";
          }
      }
      
      
    代码使世界更精彩
  • 相关阅读:
    E
    CSU 1757 火车进站 1757
    [Unity游戏开发]场景切换
    相机跟随
    [Unity游戏开发]Vector3类
    [Unity游戏开发] 关于向量计算的一些基础
    [Unity游戏开发] MonoBehaviour类常用方法(脚本生命周期)
    C#学习笔记之——new在哪些地方用
    C#——快速排序
    C#学习笔记之——事件(Event)
  • 原文地址:https://www.cnblogs.com/lgx123/p/14773142.html
Copyright © 2011-2022 走看看