快速整合搭建SSM框架

weixin_34208185發表於2017-11-12

快速整合搭建SSM框架

附上專案結構圖:

1.新增maven依賴

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.chen</groupId>
  <artifactId>spring-springmvc-mybatis</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring-springmvc-mybatis Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build-sourceEncoding>UTF-8</project.build-sourceEncoding>
    <project.reporting-outputEncoding>UTF-8</project.reporting-outputEncoding>
    <spring.version>4.3.5.RELEASE</spring.version>
    <mybatis.version>3.4.1</mybatis.version>
    <javaee.version>7.0</javaee.version>
    <net.sf.json>2.4</net.sf.json>
    <mysql.version>5.1.41</mysql.version>
    <pagehelper.varsion>4.1.0</pagehelper.varsion>
    <mybatis.spring.version>1.3.1</mybatis.spring.version>
    <commons-dbcp.version>1.2.2</commons-dbcp.version>
    <aspectj.version>1.8.6</aspectj.version>
    <jackson.version>2.9.0</jackson.version>
  </properties>

  <dependencies>

    <!--java ee 依賴-->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>${javaee.version}</version>
    </dependency>

    <!--json 依賴-->
    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>${net.sf.json}</version>
    </dependency>


    <!--mysql 資料庫-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
      <scope>runtime</scope>
    </dependency>
    <!--資料庫連線池-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${commons-dbcp.version}</version>
    </dependency>

    <!--mybatis 依賴-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <!--mybatis 分頁查詢外掛-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>${pagehelper.varsion}</version>
    </dependency>

    <!--整合mybatis和spring-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis.spring.version}</version>
    </dependency>

    <!--spring 依賴-->
    <!--底層核心-提供IOC-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</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-webmvc</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-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</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-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    <!--單元測試-->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>RELEASE</version>
      </dependency>

    <!--jackson 依賴-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>spring-springmvc-mybatis</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>複製程式碼

2.配置資料庫連線資訊jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?userUnicode=true&characterEccording=utf8
jdbc.username=root
jdbc.password=root
#初始連線數
initialSize=0
#最大連線數
maxActive=20
#最大空閒
maxIdle=20
#最小空閒
minIdle=1
#最長等待時間
maxWait=60000複製程式碼

3.配置spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--mybatis整合spring的配置-->

    <!--掃包(service註解)-->
    <context:component-scan base-package="com.chen.logic.service"/>

    <!--jdbc連結資料-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--資料庫連結配置-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${initialSize}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxIdle" value="${maxIdle}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxWait" value="${maxWait}"/>
    </bean>

    <!--sqlSessionFactory配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入資料庫連線池-->
        <property name="dataSource" ref="dataSource"/>
        <!--別名掃包(entity)-->
        <property name="typeAliasesPackage" value="com.chen.logic.entity"/>
        <!--掃描mapper對應配置檔案-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/TestMapper.xml"/>
        <!--mybatis-config日誌輸出-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    </bean>

    <!--掃描mapper型別-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--掃描mapper包-->
        <property name="basePackage" value="com.chen.logic.mapper"/>
    </bean>

    <!--配置事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入資料庫連結池-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--基於註解申明式事務配置-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>複製程式碼

4.配置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: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-3.0.xsd">

    <!--掃描Controller-->
    <context:component-scan base-package="com.chen.logic.controller"/>

    <!--開啟springMVC註解模式-->
    <mvc:annotation-driven/>

    <!--靜態資源預設servlet配置-->
    <mvc:default-servlet-handler/>

    <!--檢視配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/html"/>
        <property name="suffix" value=".html"/>
    </bean>

</beans>複製程式碼

5.配置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>

    <settings>
        <!-- 列印查詢語句 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

</configuration>複製程式碼

6.配置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_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>

  <!--編碼過濾器配置-->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!-- 配置springMVC DispatcherServlet 前端控制器-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
    <!--在啟動時載入-->
    <load-on-startup>1</load-on-startup>
    <!--非同步-->
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


</web-app>複製程式碼

7.測試

7.1.Controller

/**
 * @author 陳梓平
 * @date 2017/11/2.
 */
@RequestMapping
@RestController
public class TestController {
    @Autowired
    private TestService testService;

    @GetMapping(value = "get")
    public List<Test> get(){
        return testService.get();
    }

    @GetMapping(value = "get2")
    public String get2(){
        return "qwhuidhdiuwhqiuw";
    }
}複製程式碼

7.2.Service

/**
 * @author 陳梓平
 * @date 2017/11/2.
 */
public interface TestService {
    List<Test> get();
}
/**
 * @author 陳梓平
 * @date 2017/11/2.
 */
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestMapper testMapper;

    @Override
    public List<Test> get() {
        List<Test> tests = testMapper.queryTestInfo();
        return tests;
    }
}複製程式碼

7.3.mapper

/**
 * @author 陳梓平
 * @date 2017/11/2.
 */
public interface TestMapper {
    List<Test> queryTestInfo();
}複製程式碼

7.4.entity

/**
 * @author 陳梓平
 * @date 2017/11/2.
 */
public class Test {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}複製程式碼

7.5.mybatis對映檔案

<?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.chen.logic.mapper.TestMapper">


    <select id="queryTestInfo" resultType="com.chen.logic.entity.Test">
        SELECT * FROM test
    </select>

</mapper>複製程式碼

8.配置Tomcat測試


9.執行測試


相關文章