天天看點

SSM架構整合(簡單明了)初學者項目環境搭建

項目環境搭建

1、建立Maven的簡單web工程

SSM架構整合(簡單明了)初學者項目環境搭建

2、補全WEB-INF目錄和web.xml檔案

SSM架構整合(簡單明了)初學者項目環境搭建

3、建立包和目錄

SSM架構整合(簡單明了)初學者項目環境搭建

4、在pom.xml檔案中,引入junit、log4j、servlet等依賴包

<dependencies>

    <!-- 單元測試 -->

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>4.10</version>

    </dependency>

    <!-- 整合log4j -->

    <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-log4j12</artifactId>

            <version>1.6.4</version>

    </dependency>

    <!-- Servlet/JSP/JSTL -->

    <dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>servlet-api</artifactId>

        <version>2.5</version>

        <scope>provided</scope>

    </dependency>

    <dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>jsp-api</artifactId>

        <version>2.0</version>

        <scope>provided</scope>

    </dependency>

    <dependency>

        <groupId>jstl</groupId>

        <artifactId>jstl</artifactId>

        <version>1.2</version>

    </dependency>

</dependencies>

 5、在resources目錄下建立log4j.properties檔案,配置内容如下(檔案名必須一緻):

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# 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

6、在resources目錄下建立jdbc.properties檔案,配置内容如下(檔案名必須一緻):

db.driverClassName=com.mysql.jdbc.Driver

db.url=jdbc:mysql:///yonghedb?characterEncoding=utf-8

db.username=root   //這裡使用者名為root

db.password=root    //密碼也是root

整合spring架構

1、在pom.xml檔案中,引入spring的依賴包

<!-- 整合spring架構(包含springmvc)

    這個jar檔案包含springmvc開發時的核心類, 同時也會将依賴的相關jar檔案引入進來(spring的核心jar檔案也包含在内)

 -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>4.1.3.RELEASE</version>

</dependency>

<!--這個jar檔案包含對Spring對JDBC資料通路進行封裝的所有類 -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>4.1.3.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aspects</artifactId>

    <version>4.1.3.RELEASE</version>

</dependency>

 2、在src/main/resources/spring目錄下,建立spring的核心配置檔案applicationContext.xml

SSM架構整合(簡單明了)初學者項目環境搭建

 配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

</beans>

 整合springmvc架構

         前面在添加spring的jar包同時,将springmvc的jar包也引入了,是以這裡不再引入springmvc的jar包

1、在resources/spring目錄下,建立springmvc的核心配置檔案:springmvc-config.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/mvc

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

                        http://www.springframework.org/schema/beans

                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

                        http://www.springframework.org/schema/context

                          http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 1.配置前端控制器放行靜态資源(html/css/js等,否則靜态資源将無法通路) -->

    <mvc:default-servlet-handler/>

    <!-- 2.配置注解驅動,用于識别注解(比如@Controller) -->

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 3.配置需要掃描的包:spring自動去掃描 base-package 下的類,

        如果掃描到的類上有 @Controller、@Service、@Component等注解,

        将會自動将類注冊為bean 

     -->

    <context:component-scan base-package="com.tedu.controller">

    </context:component-scan>

    <!-- 4.配置内部資源視圖解析器

        prefix:配置路徑字首

        suffix:配置檔案字尾

     -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/pages/"/>

        <property name="suffix" value=".jsp"/>

    </bean>

</beans>

2、在web.xml中配置springmvc

<!-- 配置springmvc, 将所有請求交給springmvc來處理 -->

<servlet>

    <servlet-name>springmvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 配置springmvc核心配置檔案的位置,預設Springmvc的配置檔案是在WEB-INF目錄下,預設的名字為springmvc-servlet.xml,如果要放在其他目錄,則需要指定如下配置:

        -->

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring

@Controller

public class DoorController {

    @RequestMapping("/testmvc")

    public String testmvc(){

        return "test";

    }

}

 (3)将項目部署到伺服器中,啟動伺服器:

SSM架構整合(簡單明了)初學者項目環境搭建

 通路效果如下:

SSM架構整合(簡單明了)初學者項目環境搭建

整合mybatis架構

1、在pom.xml檔案中,引入mybatis及相關依賴包

<!-- 整合mybatis架構 -->

<dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis</artifactId>

    <version>3.2.8</version>

</dependency>

<dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis-spring</artifactId>

    <version>1.2.2</version>

</dependency>

<!-- mysql驅動 -->

<dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>5.1.32</version>

</dependency>

<!-- druid連接配接池 -->

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid</artifactId>

    <version>1.1.6</version>

</dependency>

 2、在resources/mybatis目錄下,建立mybatis的核心配置檔案:

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">

<!-- MyBatis的全局配置檔案 -->

<configuration >

    <!-- 1.配置開發環境 -->

    <environments default="develop">

        <!-- 這裡可以配置多個環境,比如develop,test等 -->

        <environment id="develop">

            <!-- 1.1.配置事務管理方式:JDBC:将事務交給JDBC管理(推薦) -->

            <transactionManager type="JDBC"></transactionManager>

            <!-- 1.2.配置資料源,即連接配接池方式:JNDI/POOLED/UNPOOLED -->

            <dataSource type="POOLED">

                <property name="driver" value="com.mysql.jdbc.Driver"/>

                <property name="url" value="jdbc:mysql://localhost:3306/yonghedb?characterEncoding=utf-8"/>

                <property name="username" value="root"/>

                <property name="password" value="root"/>

            </dataSource>

        </environment>

    </environments>

    <!-- 2.加載Mapper配置檔案,路徑以斜杠間隔: xx/xx/../xx.xml -->

    <mappers>

        <mapper resource="mybatis/mapper/DoorMapper.xml"/>

    </mappers>

</configuration>

 3、在src/main/resources/mybatis/mapper目錄下,建立Door實體類的映射檔案:

DoorMapper.xml

SSM架構整合(簡單明了)初學者項目環境搭建

 配置内容如下:

<?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">

<!-- 門店表的映射檔案    namespace值為對應接口的全路徑 -->

<mapper namespace="com.tedu.dao.DoorMapper">

    <!-- 1.查詢所有門店資訊,id值為對應接口中方法的名字

        resultType指定将查詢的結果封裝到哪個pojo對象中

     -->

    <select id="findAll" resultType="com.tedu.pojo.Door">

        select * from tb_door

    </select>

</mapper>

4、在mybatis的全局配置檔案中引入DoorMapper.xml映射檔案

因為第二步中已經引入,這裡就不用引入了

5、建立實體類Door,用于封裝所有的門店資訊

 package com.tedu.pojo;

public class Door {

    private Integer id;    //門店編号

    private String name;    //門店名稱

    private String tel;    //門店電話

    private String addr;    //門店位址

    //getter和setter方法

    public Integer getId() {

        return id;

    }

    public void setId(Integer id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getTel() {

        return tel;

    }

    public void setTel(String tel) {

        this.tel = tel;

    }

    public String getAddr() {

        return addr;

    }

    public void setAddr(String addr) {

        this.addr = addr;

    }

    //重寫toString方法

    @Override

    public String toString() {

        return "Door [id=" + id + ", name=" + name + ", tel=" + tel + ", addr=" + addr + "]";

    }

}

6、建立com.tedu.dao.DoorMapper接口,并根據EmpMapper.xml檔案中的sql語句,提供findAll方法

package com.tedu.dao;

import java.util.List;

import com.tedu.pojo.Door;

public interface DoorMapper {

    public List<Door> findAll();

}

7、建立測試com.tedu.controller.TestMybatis類,對mybatis開發環境進行測試  

package com.tedu.controller;

import java.io.InputStream;

import java.util.List;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.tedu.dao.DoorMapper;

import com.tedu.pojo.Door;

public class TestMybatis {

    public static void main(String[] args) throws Exception {

        //1.讀取mybatis-config.xml核心檔案

        InputStream in = Resources.getResourceAsStream(

                "mybatis/mybatis-config.xml");

        //2.擷取SqlSessionFactory工廠

        SqlSessionFactory factory = 

                new SqlSessionFactoryBuilder()

                .build(in);

        //3.擷取SqlSession對象

        SqlSession session = factory.openSession();

        //4.擷取DoorMapper接口的執行個體

        DoorMapper mapper = session.getMapper(DoorMapper.class);

        //5.調用findAll方法查詢所有門店資訊

        List<Door> list = mapper.findAll();

        //6.周遊所有門店資訊

        for(Door door : list){

            System.out.println(door);

        }

    }

}

執行結果:

SSM架構整合(簡單明了)初學者項目環境搭建

整合spring和mybatis

1、修改mybatis-config.xml檔案,将資料源等配置移除,在spring的配置檔案中配置

<?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">

<!-- MyBatis的全局配置檔案 -->

<configuration>

    <!-- 1.配置開發環境 -->

    <!-- 1.1.配置事務管理方式:JDBC:将事務交給JDBC管理(推薦) -->

    <!-- 1.2.配置資料源,即連接配接池方式:JNDI/POOLED/UNPOOLED -->

    <!-- 2.加載Mapper配置檔案,路徑以斜杠間隔: xx/xx/../xx.xml -->

</configuration>

這裡強調,删除之後就不能運作TestMybatis這個類,否者會報錯。

2、在applicationContext.xml中配置 druid連接配接池、SqlSession工廠等

          由spring負責建立mybatis在運作過程中,所需要的一些對象。比如: SqlSession對象、SqlSession工程對象、連接配接池對象、Mapper接口的子類執行個體等都會交給spring容器建立

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 1.加載jdbc.properties檔案的位置 -->

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

    <!-- 2.配置druid連接配接池 ,id是固定值,class是druid連接配接池類的全路徑 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

        <!-- 配置連接配接資料庫的基本資訊 -->

        <property name="driverClassName" value="${db.driverClassName}"></property>

        <property name="url" value="${db.url}"></property>

        <property name="username" value="${db.username}"></property>

        <property name="password" value="${db.password}"></property>

    </bean>

    <!-- 3.整合spring和mybatis架構    

        将SqlSession等對象的建立交給Spring容器

        id值(sqlSessionFactory)是固定值

     -->

    <bean id="sqlSessionFactory" 

        class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-- 3.1.指定mybatis核心配置檔案的位置 -->

        <property name="configLocation" 

                value="classpath:mybatis/mybatis-config.xml"></property>

        <!-- 3.2.配置連接配接池(資料源) ref指向連接配接池bean對象的id值 -->

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

        <!-- 3.3、掃描所有的 XxxMapper.xml映射檔案,讀取其中配置的SQL語句 -->

        <property name="mapperLocations" value="classpath:mybatis/mapper

    @Autowired

    DoorMapper mapper;

    @RequestMapping("/testssm")

    public String testSSM(){

        //1.調用findAll方法查詢所有門店資訊

        List<Door> list = mapper.findAll();

        //2.周遊所有門店資訊

        for(Door door : list){

            System.out.println(door);

        }

        return "test";

    }

運作項目,執行結果如下: 

SSM架構整合(簡單明了)初學者項目環境搭建

本人這裡用到的項目名是spring01,是以通路路徑試着樣的。具體問題具體分析。

本人這裡用到了資料庫:現在将資料庫檔案放到底下:

-- 建立yonghedb庫、tb_door、tb_order表并插入記錄

-- --------------------------------------------

-- 删除yonghedb庫(如果存在)

-- drop database if exists yonghedb;

-- 重新建立yonghedb庫

create database if not exists yonghedb charset utf8;

-- 選擇yonghedb庫

use yonghedb;

-- 删除門店表(需要先删除訂單表)

drop table if exists tb_order;

drop table if exists tb_door;

-- 建立門店表

create table tb_door(

    id int primary key auto_increment,    -- 門店id

    name varchar(100),                    -- 門店名稱

    tel varchar(100),                    -- 聯系電話

    addr varchar(255)                    -- 門店位址

);

-- 往門店表中插入記錄

insert into tb_door values ('1', '永和大王(北三環西路店)', '010-62112313', '北三環西路甲18号院-1号大鐘寺中坤廣場d座');

insert into tb_door values ('2', '永和大王(知春路店)', '010-82356537', '知春路29号大運金都');

insert into tb_door values ('3', '永和大王(東直門)', '010-84477746', '東直門外大街48号東方銀座b2-08');

insert into tb_door values ('4', '永和大王(北京站)', '010-65286602', '毛家灣胡同甲13号北京站候車大廳2層');

insert into tb_door values ('5', '永和大王(學院路店)', '010-62152539', '學院南路37号超市發四道口店四道口西北角');