ssm整合(简单的增删改查)

1 创建maven web项目;

2 添加jar包(所有的依赖)

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--json依赖-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
    <!--添加springwebmvc依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <!--mybatis依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!--mybatis的spring接口实现依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!--数据源依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <!--数据库驱动依赖-->
    <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.0.26</version>
    </dependency>
    <!--jstl依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!---->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.4.0.Final</version>
    </dependency>
  </dependencies>

添加最主要的Tomact目录

<plugins>
      <!--添加tomcat-plugin插件用于web发布执行-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path>
          <port>8080</port>
        </configuration>
      </plugin>
    </plugins>

3 补充目录及配置文件

我们用的是idea所以目录是不完整的,需要我们自己来配置,
在这里插入图片描述

4 整合spring与mybatis

spring-mybatis.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:context="http://www.springframework.org/schema/context"
       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">
    <!--4.1加载数据库参数文件-->
    <context:property-placeholder location="classpath:res/db.properties"/>
    <!--4.2配置数据库连接池druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--4.3配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:mapping/*.xml"/>
    </bean>
    <!--4.4 配置mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--配置dao扫描接口-->
        <property name="basePackage" value="mapper"/>
    </bean>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mmmm
jdbc.username=root
jdbc.password=root

mapper路径下的DeptController

public interface DeptMapper {
    public List<Dept> findAll();

    public Dept findById(int deptno);

    public boolean updateDept(Dept dept);

    public boolean insertDept(Dept dept);

    public boolean deleteDept(int deptno);

}

resources资源目录下的mapping文件夹的DeptMapper.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.zyzy.day54.mapper.DeptMapper">
    <select id="findAll" resultType="com.zyzy.day54.po.Dept">
        SELECT *
        FROM DEPT
    </select>
    <select id="findById" parameterType="int" resultType="com.zyzy.day54.po.Dept">
        SELECT *
        FROM DEPT
        WHERE deptno = #{deptno}
    </select>
    <update id="updateDept" parameterType="com.zyzy.day54.po.Dept">
        UPDATE DEPT
        SET dname=#{dname},loc=#{loc}
        WHERE deptno=#{deptno}
    </update>
    <insert id="insertDept" parameterType="com.zyzy.day54.po.Dept">
        INSERT INTO
        DEPT
        (dname,loc)
        VALUES (#{dname},#{loc})
    </insert>
    <delete id="deleteDept" parameterType="int" >
        DELETE FROM
        DEPT
        WHERE deptno=#{deptno}
    </delete>
</mapper>

po里面我们封装的实体类

public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;

    public Dept(Integer deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public Dept() {
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

5 整合web容器与spring

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--5整合spring和web容器 begin-->
  <!--配置全局参数上下文-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-*.xml</param-value>
  </context-param>
  <!--7.1 配置编码过滤器-->
  <filter>
    <filter-name>characterFilter</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>characterFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--配置侦听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--整合spring和web容器 end-->
  <!--7 整合springmvc+web容器begin-->
  <!--因为web.xml书写规范必须要求过滤器在监听前所以提到前面-->
  <!--7.2配置核心处理器-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <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>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

先完成service和mapper层的解耦

public interface DeptService {
    public List<Dept> findDepts();
    public Dept findById(int deptno);
    public boolean updateDept(Dept dept);
    public boolean insertDept(Dept dept);
    public boolean deleteDept(int deptno);
}
service的实现类
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> findDepts() {
        List<Dept> all = deptMapper.findAll();
        return all;
    }

    @Override
    public Dept findById(int deptno) {
        Dept dept = deptMapper.findById(deptno);
        return dept;
    }

    @Override
    public boolean updateDept(Dept dept) {
        boolean b = deptMapper.updateDept(dept);

        return b;
    }

    @Override
    public boolean insertDept(Dept dept) {
        boolean b = deptMapper.insertDept(dept);

        return b;
    }

    @Override
    public boolean deleteDept(int deptno) {
        boolean b = deptMapper.deleteDept(deptno);

        return b;
    }
}

配置spring-service.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:context="http://www.springframework.org/schema/context"
       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">
    <context:component-scan base-package="com.zyzy.day54.service"/>
</beans>

完成controller和service解耦
DeptController

@Controller
public class DeptController {
    @Autowired
    public DeptService deptService;

    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    public String findAll(Model model){
        List<Dept> depts = deptService.findDepts();
        model.addAttribute("depts",depts);
        return "list";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String findById(@RequestParam("deptno") int deptno ,Map map){
        Dept dept = deptService.findById(deptno);
        map.put("dept",dept);
        return "edit";
    }

    @RequestMapping( value = "/update" , method = RequestMethod.POST)
    public String updateDept( Dept dept){
            boolean b = deptService.updateDept(dept);
            if (b){
                return "redirect:/depts";
            }
        
            return null;
    }

    @RequestMapping(value = "/add" , method = RequestMethod.POST)
    public String insertDept(Dept dept){
            boolean b = deptService.insertDept(dept);
            if (b){
                return "redirect:/depts";
            }
        return null;

    }

    @RequestMapping(value = "/delete" , method = RequestMethod.GET)
    public String deleteDept(@RequestParam("deptno") int deptno){
        boolean b = deptService.deleteDept(deptno);
        if (b){
            return "redirect:/depts";
        }else
            return null;

    }
}

6 完善springmvc配置

springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--6.1 配置controller(c处理器)扫描包路径-->
    <context:component-scan base-package="com.zyzy.day54.controller"/>
    <!--6.2 配置注解驱动-->
    <mvc:annotation-driven/>
    <!--6.3 配置静态资源处理器-->
    <mvc:default-servlet-handler/>
    <!--5.4 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

7 整合web容器与springmvc

web.xml(看第五步)

8 检验整合效果

附上所用的前端页面
add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="add" method="post">
    部门名称<input type="text" name="dname" >
    <br>
    部门地址<input type="text" name="loc" >
    <br>
    <input type="submit" value="add">
</form>
</body>
</html>

edit.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="update" method="post">
    部门id<input type="text" name="deptno" value="${dept.deptno}" readonly><br>
    部门名称<input type="text" name="dname" value="${dept.dname}">
    <br>
    部门地址<input type="text" name="loc" value="${dept.loc}">
    <br>
    <input type="submit" value="update">
</form>
</body>
</html>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/depts">查看全部信息</a>
<br>
<a href="/add.jsp">添加</a>
</body>
</html>

list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:forEach items="${depts}" var="dept">
        ${dept.deptno}
        ${dept.dname}
        ${dept.loc}
        <a href="/edit?deptno=${dept.deptno}">修改</a>
        <a href="/delete?deptno=${dept.deptno}">删除</a>
        <br>
    </c:forEach>
</body>
</html>
    水平有限,有什么错误希望大家指出!!!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值