ssm框架整合流程

1.拿到相应的包放在lib下。

2构建相应包路径

3.做好上一步之后,编写spring-mybatis.xml

(以属性注入的方式配置数据源:数据库连接属性、session工厂以及动态生成dao的实现)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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/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">
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/djr" />
            <property name="username" value="root"></property>
            <property name="password" value="djrroot"></property>
        </bean>
        <!-- 配置session工厂 -->
        <bean id="sesstionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="mapperLocations" value="classpath*:com/yc/Sys/dao/*.xml"></property>
        </bean>
        <!-- 动态生成"dao"实现类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sesstionFactory"></property>
            <property name="basePackage" value="com.yc.Sys.dao"></property>
        </bean>
    
</beans>

4.编写具体modle,dao以及对应mapping

注意mapping 的编写标准:放在dao层目录下

5.编写springMvc-servlet.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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/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">
        <!-- 打开注解 
        <mvc:annotation-driven></mvc:annotation-driven>-->
        <!-- 扫描包 -->
        <context:component-scan base-package="com.yc"></context:component-scan>
        <!-- 视图解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 前缀 --><property name="prefix" value="/WEB-INF/webpage/"></property>
            <!-- 后缀 --><property name="suffix" value=".jsp"></property>
        </bean>
</beans>

编写web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 
     <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
     <!-- 解决乱码问题 -->
     <filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
     
 <!-- 用参数方式配置文件位置 --> 
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring.xml</param-value>
 </context-param>
 <!-- 监听器:启动容器时加载spring   ioc -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 用参数方式配置spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  
  
  <!-- 前端控制器 -->
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

6.编写controller.java

package com.yc.Sys.controller;

import java.io.IOException;
import java.util.List;

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 com.sun.org.apache.regexp.internal.recompile;
import com.yc.Sys.dao.GroupDao;
import com.yc.Sys.dao.StudentDao;
import com.yc.Sys.modle.GroupModle;
import com.yc.Sys.modle.StudentModle;

@Controller
@RequestMapping("controller")
public class controller{
    //用注解动态生成DAO的实现
    @Autowired
    GroupDao gpdao;
    @Autowired
    StudentDao stdao;
    //
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable int id){
        System.out.println(id);
        stdao.delete(id);
        return "redirect:/controller/studentList";
    }
    //
    @RequestMapping("update/{id}")
    public String update(@PathVariable int id,Model m){
        System.out.println(id);
        StudentModle sm=stdao.selectById(id);
        GroupModle gm=gpdao.selectById(sm.getGroupid());
        sm.setGroupmodle(gm);
        m.addAttribute("sm",sm);
        List<GroupModle> Agm=gpdao.selectAll();
        m.addAttribute("Agm",Agm);
        return "update";
    }
    //
    @RequestMapping(value="doupdate",method=RequestMethod.POST)
    public String doupdate(StudentModle modle){
        
        System.out.println("xiugaile ");
        stdao.update(modle);
        
        return "redirect:/controller/studentList";
    }
    //
    @RequestMapping("studentList")
    public String list(Model m){
        List<StudentModle> stuList=stdao.selectAll();
        for (int i = 0; i < stuList.size(); i++) {
            GroupModle gm = gpdao.selectById(stuList.get(i).getGroupid());
            stuList.get(i).setGroupmodle(gm);
        }
        m.addAttribute("stuList", stuList);
        return "studentList";
    }
}

编写测试即studentList.jsp和update.jsp

studentList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
th {
    color: #fff;
}

td {
    color: #fff;
}

mess {
    color: #fff;
}
</style>
</head>
<body>
    <table border="1" cellspacing="1" cellpadding="1" align="center"
        width="600px" height="680px" bgcolor="black">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>班级</th>
            <th>修改</th>
            <th>删除</th>
        </tr>
        <c:forEach items="${stuList }" var="stu">
            <tr>
                <td>${stu.id}</td>
                <td>${stu.name}</td>
                <td>${stu.sex}</td>
                <td>${stu.age}</td>
                <td>${stu.groupmodle.groupname}</td>
                <!-- 显示班级怎么显示 -->
                <td><a id="sc" href="${pageContext.request.contextPath}/controller/update/${stu.id }">修改</a></td>
                <td><a href="${pageContext.request.contextPath}/controller/delete/${stu.id }">删除</a></td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div class="cont">
        <form action="${pageContext.request.contextPath}/controller/doupdate" method="post">
            编号:<input type="text" name="id" value="${sm.id}" readonly="readonly"/> 
            姓名:<input type="text" name="name" value="${sm.name}" />
            性别:<input type="text" name="sex" value="${sm.sex}" /> 
            年龄:<input type="text" name="age" value="${sm.age}" />
            班级:<select name="groupid">
                <c:forEach items="${Agm}" var="g">
                    <option value="${g.groupid }"
                    <c:if test="${g.groupid==sm.groupid }">selected="selected"</c:if>>
                    ${g.groupname }
                    </option>
                    
                </c:forEach>
            </select>
                <button type="submit" name="m" value="doupdate">提交</button>
        </form>


    </div>
</body>
</html>

index.jsp欢迎页,起跳转作用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="controller/studentList"></jsp:forward>
</body>
</html>

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值