一、准备工作

1.创建maven-java web项目

2.创建数据库

 
create database ssm;
​
create table account(
    id int primary key auto_increment,
    name varchar(20),
    money double
);

3.导入依赖。在pom.xml中添加依赖

  <properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>
​
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
​
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</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-web</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-test</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-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
​
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
​
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
​
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
​
        <dependency>
            <groupId>javax.servlet.jsp</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>
​
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
​
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
​
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
​
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
​
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
​
    </dependencies>
    
<build>
        <finalName>ssm</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

3.编写实体类,提供和数据库中字段相同的属性、以及有参、无参构造方法、get set方法,tostring方法。

二、表现层spring mvc框架

1.部署tomcat服务器

2.配置web.xml

设置前端控制器,dispatcherServlet。配置启动加载Spring mvc的配置文件。配置过滤器。配置spring配置文件的监听器,加载spring框架的配置文件,applicationContext.xml。(spring整合springmvc)

<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动加载-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--解决post请求中文乱码的过滤器-->
  <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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置Spring的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
</web-app>

3.springmvc配置文件

开启对表现层对应的包的注解扫描,配置视图解析器组件,配置spring mvc组件有效的声明。

(spring mvc基于组件开发,需要其他的组件再配置即可)

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

    <!--开启注解扫描  扫描contorller-->
    <context:component-scan base-package="com.qcby.controller"/>
    <!--异常处理器,文件上传解析器,拦截器,哪些资源不拦截-->
    <!-- 设置静态资源不过滤
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
       -->
    <!--配置视图解析器  /pages/suc.jsp-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--开启mvc支持-->
    <!-- 开启对SpringMVC注解的支持 -->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven/>
</beans>

4.编写表现层的类

controller包下的Accountcontroller。调用业务层的方法。

这里使用modelandvive作为返回类型,可以添加携带信息、设置返回界面的名字(视图解析器实现跳转)

package com.qcby.controller;
import com.qcby.model.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
//调用业务层
@Controller
public class AccountController {
    /**
     * 查询所有
     * @return
     */
    // 注入
    @Autowired
    private AccountService accountService;
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有的账号...");
        // 调用service对象的方法
        List<Account> list = accountService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("suc");
        return mv;
    }
}

三、业务层 

1.applicationContext.xml配置文件

开启对业务层包的扫描、配置连接池,配置会话工厂(将连接池注入、加载持久层的mappers下面的子配置文件)、将DAO对象存在IOC容器中(注入会话工厂、开启对持久层下包dao/mapper的扫描)这里是spring整合mybatis

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 开启注解扫描,要扫描的是service -->
    <context:component-scan base-package="com.qcby.service"></context:component-scan>
    <!--&lt;!&ndash;配置文件+注解方式&ndash;&gt;-->
    <!--<context:component-scan base-package="com.qcby.mapper" />-->
    <!--配置druid连接池-->
    <!--配置开源连接池 Druid连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///ssm" />
        <property name="username" value="root" />
        <property name="password" value="2020" />
    </bean>
    <!--Spring框架整合mybatis框架-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <!--注入连接池对象-->
        <property name="dataSource" ref="dataSource" />
        <!--加载映射配置文件-->
        <property name="mapperLocations" value="classpath:mappers/*.xml" />
    </bean>
    <!--把dao对象存入到IOC容器中-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sessionFactory" />
        <!--扫码dao所在的包,把包中接口的代码对象存入到IOC容器中-->
        <property name="basePackage" value="com.qcby.mapper" />
    </bean>
</beans>

2.编写业务层的接口以及实现类

接口:

package com.qcby.service;

import com.qcby.model.Account;

import java.util.List;

public interface AccountService {
    public List<Account> findAll();
}

实现类: 调用持久层的方法,实现对数据库的操作。

package com.qcby.service.impl;
import com.qcby.mapper.AccountMapper;
import com.qcby.model.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
    @Service
    public class AccountServiceImp implements AccountService {
        @Autowired
        private AccountMapper accountDao;

        @Override
        public List<Account> findAll() {
            System.out.println("业务层:查询所有的账号...");
            return accountDao.findAll();
        }
}

四、持久层 mybatis框架

1.编写mapper接口

package com.qcby.mapper;
import com.qcby.model.Account;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface AccountMapper {
    public List<Account> findAll();
}

 2.在子配置文件中,编写接口的sql语句

<?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.qcby.mapper.AccountMapper" >
    <!--查询所有-->
    <select id="findAll" resultType="com.qcby.model.Account">
        select * from account
    </select>
</mapper>

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐