SSM整合

马果老师整理

 

ssm:(spring,springmvc+mybatis)

步骤:

1、创建web项目

2、导入包:

        spring 4.2

            core,beans,context,expression,aop,aspects,web,webmvc,orm,jdbc,tx

        spring 3.0.2
            logging, aop联盟,织入

        mybatis
            mybatis,mybatis-spring,log4j,cglib

        数据库相关的
            mysql,druid

        jsp:
            jstl,standard

3、编写web.xml文件中的前端控制及转换编码的过滤器

    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

4、编写applicationContext.xml


    <context:component-scan base-package="org.java"/>

    <!--引入属性文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${url}"/>
        <property name="driverClassName" value="${driver}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${pwd}"/>
    </bean>

    <!--配置sqlsessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="org.java.entity"/>
    </bean>

    <!--配置Mapper接口扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.java.dao"/>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--启用注解事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--视图映射器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="suffix" value=".jsp"/>
    </bean>

5、编写实体类

package org.java.entity;

import java.io.Serializable;

public class Inf implements Serializable {

    private Integer id;
    private String name;
    private Integer score;

    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 Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}

6、编写Mapper接口(dao接口)

package org.java.dao;

import org.apache.ibatis.annotations.Param;
import org.java.entity.Inf;

import java.util.List;

public interface InfMapper {

    public List<Inf> getList();

    public void del(int id);

    public void add(Inf f);
}

7、编写Mapper.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="org.java.dao.InfMapper">
    <select id="getList" resultType="inf">
        select * from inf
    </select>

    <delete id="del">
        delete from inf where id=#{id}
    </delete>

    <insert id="add" parameterType="inf">
        insert into inf values(null,#{name},#{score})
    </insert>
</mapper>

8、编写Service接口与实现类

package org.java.service.impl;

import org.java.dao.InfMapper;
import org.java.entity.Inf;
import org.java.service.InfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class InfServiceImpl implements InfService {

    @Autowired
    private InfMapper infMapper;

    @Transactional(readOnly = true)
    @Override
    public List<Inf> getList() {
        return infMapper.getList();
    }


    @Override
    public void del(int id) {
        infMapper.del(id);
    }

    @Override
    public void add(Inf f) {
        infMapper.add(f);
    }
}

9、编写控制类

package org.java;

import org.java.entity.Inf;
import org.java.service.InfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class InfController {

    @Autowired
    private InfService infService;

    @RequestMapping(value = "/init",method = RequestMethod.GET)
    public String init(Model model){

        List<Inf> list = infService.getList();
        model.addAttribute("list",list);

        return "/show";
    }

    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") Integer id){
        infService.del(id);
        return "redirect:/init.do";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(Inf f){
        infService.add(f);
        return "redirect:/init.do";
    }

}

10、编写页面

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/15
  Time: 10:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
      <jsp:forward page="init.do"/>
  </body>
</html>

 add.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/15
  Time: 10:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>添加</h1>
    <form action="${pageContext.request.contextPath}/add.do" method="post">
        姓名:<input type="text" name="name" required placeholder="请输入姓名"><Br>
        成绩:<input type="number" name="score" required placeholder="请输入成绩"><Br>
        <input type="submit" value="添加"/>
    </form>
</body>
</html>

show.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/15
  Time: 10:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
        <Table border="1" width="60%" align="center">
            <Tr>
                <Th>编号</Th><Th>姓名</Th><Th>成绩</Th><Th>操作</Th>
            </Tr>
            <c:forEach items="${requestScope.list}" var="f">
                <Tr align="center">
                    <Td>${f.id}</Td>
                    <Td>${f.name}</Td>
                    <Td>${f.score}</Td>
                    <Td>
                        <a href="${pageContext.request.contextPath}/del/${f.id}.do" onclick="return confirm('你确定要删除吗?')" >删除</a>
                        <a href="${pageContext.request.contextPath}/add.jsp">添加</a>
                    </Td>
                </Tr>
            </c:forEach>
        </Table>
</body>
</html>

框架,一般是在企业级项目中使用,如果只是简单的实现helloworld,用框架反而更麻烦

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页