SSM整合-初

SSM框架整合

1,创建web项目,导包,项目分层

在这里插入图片描述

2,编辑web.xml

1)前端控制器:配置servlet节点:DispatcherServlet
2)上下文参数+监听器加载spring配置文件
<?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">
<!--上下文参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
<!--    监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!--    前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.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>
</web-app>

3,配置spring配置文件(applicationContext-mybatis.xml)

1)数据源:BasicDatasource
2)Session工厂:SqlSessionFactoryBean
3)扫描dao层的接口和映射文件:MapperScannerConfigurer
4)扫描service层注解
5)声明式注解
<?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:aop="http://www.springframework.org/schema/aop"  
        xmlns:p="http://www.springframework.org/schema/p"  
        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-3.2.xsd   
            http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
            http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  

<!--    引入数据源-->
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location" value="classpath:database.properties"/>
    </bean>
<!--    数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
<!--sqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--    扫描dao层接口。mapper文件-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.dao"/>
    </bean>
<!--    扫描service-->
    <context:component-scan base-package="com.demo.service"/>
<!--    声明式事务-->
    
</beans>

4,配置spring mvc的配置文件(springmvc-servlet.xml)

1)扫描controller层的注解
2)一键式配置
3)前端视图解析器
4)其他:统一的异常处理,上传文件的解析器,静态资源的路径配置
<?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:p="http://www.springframework.org/schema/p" 
	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.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!--	扫描controller层注解-->
	<context:component-scan base-package="com.demo.controller"/>
<!--	一键式配置-->
	<mvc:annotation-driven/>
<!--	视图解析器-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/demo/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>  

5,组层编写代码

1,dao:接口,映射文件
package com.demo.dao;

import com.demo.pojo.SmbmsUser;

import java.util.List;
  /**
     * User数据访问层接口
     * 
     */
public interface UserMapper {
    /**
     * 查询全部
     * @return
     */
    List<SmbmsUser>getList();
}

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.demo.dao.UserMapper">
<select id="getList" resultType="com.demo.pojo.SmbmsUser" >
    select * from smbms_user
</select>

</mapper>
2,service:注解的使用,注入dao对象
package com.demo.service.impl;

import com.demo.dao.UserMapper;
import com.demo.pojo.SmbmsUser;
import com.demo.service.UserService;
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;

    @Override
    public List<SmbmsUser> getAll() {

        return userMapper.getList();
    }
}
3,controller:注解的使用,注入service对象
package com.demo.controller;

import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 控制器
 */
@Controller
@RequestMapping("/home")
public class UserController {
    //声明UserService对象并使用注解注入
    @Autowired
    private UserService userService;

    //接受请求后跳转此方法,并在获取到数据后跳转至index页面
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("list",userService.getAll());
        return "index";
    }

}

4,jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>地址</td>
    </tr>
    <c:forEach items="${list}" var="u">
        <tr>
            <td>${u.userCode}</td>
            <td>${u.userName}</td>
            <td>${u.address}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

5,执行路径,直接从控制器的index方法进入
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值