關於IDEA執行ssm專案的一些坑以及ssm的基本的配置

Timous發表於2020-10-09

匯入依賴

<?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>org.timous</groupId>
    <artifactId>springmvc</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!--spring-mvc  其中包含了很多的  spring-aop等一些列和springmvc的一些jar包-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <!--mybatis-->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--mybatis-spring   spring和mybatis的整合需要的jar包-->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>
        <!--lombok的jar包-->
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <!--c3p0的jar包  這裡我們的資料來源使用c3p0的jar-->
        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <!--jdbc的包-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <!--jdbc  connector-->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

    </dependencies>


    <!--靜態資源匯出,主要是mapper.xml的匯出-->
    <build>

        <resources>
            <resource>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>
</project>

建立一個資料庫

create database ssmproject; 
create table book(
    id int(10) primary key not null auto_increment,
    name varchar(20) ,
    description varchar(255)
);

在這裡插入圖片描述
在這裡插入圖片描述# 編寫基本的配置檔案
這裡我們通過基本三層結構來建立檔案,這樣方便記憶
在這裡插入圖片描述
此外還有一個database.properties的配置資料庫的檔案沒有寫,下面會有其他的配置檔案

database.properties配置檔案

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmproject?serverTimezone=UTC&amp&useSSL=false
jdbc.username=root
jdbc.password=123456  # 你自己的資料庫的密碼

spring-dao配置檔案

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
     http://www.springframework.org/schema/mvc
     https://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置資料庫的一些配置檔案,主要是讀取資料庫的配置-->
    <bean id="propertiesConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>
        
    <!--配置資料來源  dataSource  這裡使用c3p0的資料庫連線池-->
    <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="${jdbc.password}"/>
    </bean>
    
    <!--配置sqlsessionfactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>     
        <property name="configLocation" value="classpath:mybatis-config.xml"/>       
    </bean>
        
    <!--注入配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
                <!--掃描基本的包-->
        <property name="basePackage" value="com.timous.dao"/>
    </bean>
</beans>

spring-service配置檔案

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
     http://www.springframework.org/schema/mvc
     https://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd">


    <!--掃描service的介面,便於註解實現-->
    <context:component-scan base-package="com.timous.service"/>

    <!--配置資料來源,可以實現事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

    <!--aop支援等-->

</beans>

spring-mvc配置檔案

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
     http://www.springframework.org/schema/mvc
     https://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd">
    <!--註解驅動-->
    <mvc:annotation-driven/>
    <!--靜態資源管理-->
    <mvc:default-servlet-handler/>
    
    <!--掃描註解-->
    <context:component-scan base-package="com.timous.controller"/>
    <!--試圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/templates/"/>

        <property name="suffix" value=".html"/>  <!--這裡配置其他的也可以的-->

    </bean>

</beans>

mybatis-config配置

<?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.timous.entity"/>
    </typeAliases>

    <!--註冊我們寫過的mapper檔案-->
    <mappers>

    </mappers>

</configuration>

applicationContext.xml配置檔案

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
     http://www.springframework.org/schema/mvc
     https://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--把他們整合到一個配置檔案中就可以了,便於我們web.xml的配置-->
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
    
</beans>

這樣配置檔案就寫好了,下面我們可以在建立webapp整合web,建立資料夾如下
在這裡插入圖片描述
這裡我自己是建立在main下面的,然後自己添進web的。
在這裡插入圖片描述
在這裡插入圖片描述
然後修改你自己的路徑,注意這裡是你自己新增的路徑,一定要新增web.xml
在這裡插入圖片描述
在這裡插入圖片描述
下面的這個路徑指向webapp就行了,結果是webapp下面新增了一個小藍圓圈
在這裡插入圖片描述

配置web.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>org.timous</groupId>
    <artifactId>springmvc</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!--spring-mvc  其中包含了很多的  spring-aop等一些列和springmvc的一些jar包-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <!--mybatis-->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--mybatis-spring   spring和mybatis的整合需要的jar包-->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>
        <!--lombok的jar包-->
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <!--c3p0的jar包  這裡我們的資料來源使用c3p0的jar-->
        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <!--jdbc的包-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
    </dependencies>


    <!--靜態資源匯出,主要是mapper.xml的匯出-->
    <build>

        <resources>
            <resource>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>
</project>

到這整合的問題基本上市已經完成了,剩下的就是測試的問題了

entity

package com.timous.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {

    int id;
    String name;
    String description;

}

dao層

//介面
package com.timous.dao;

import com.timous.entity.Book;

import java.util.List;
@Component
public interface BookMapper {

    List<Book> queryAllBook();

}
Mapper配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.timous.dao.BookMapper">

    <!--編寫sql語句-->
    <select id="queryAllBook" resultType="Book">
        select * from book
    </select>

</mapper>

注意在寫完mapper配置檔案之後要註冊

在這裡插入圖片描述

service層

//介面
package com.timous.service;

import com.timous.entity.Book;

import java.util.List;

public interface BookService {

    List<Book> queryAllBook();

}


//實現類
package com.timous.service;

import com.timous.dao.BookMapper;
import com.timous.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService{

    @Autowired
    private BookMapper bookMapper;

    public List<Book> queryAllBook() {
        return bookMapper.queryAllBook();
    }
}

controller層

package com.timous.controller;

import com.timous.entity.Book;
import com.timous.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        List<Book> books = bookService.queryAllBook();

        for (Book book:books) {
            System.out.println(book.toString());
        }

        return "hello world";

    }

}

說明專案的型別
在這裡插入圖片描述
在這裡插入圖片描述

配置tomcat

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
然後執行測試訪問
http://localhost:8080/ssmproject/test
看到下面的頁面就成功了
在這裡插入圖片描述
再去看一下後臺的
在這裡插入圖片描述
也能夠訪問到資料

相關文章