SSM框架整合现学现卖,自己留点笔记吧!

废话- spring、spring-mvc、mybaties

    2018/5/22,自己接触java一年多了,对于流行框SSM,该总结总结了,以备自己查阅资料。(我会整理ssm单独框架使用,对于单独使用某个框架,会加深你对单个框架的理解,很重要。今天就把工作中需要的框架整合整理出来,有工作才能有革命本钱,哈哈,编码不规范哈)。

1 切入正题,一下就是我搭建过程

   ssm框架需要的jar包,如果你有疑问,每个jar包都有什么作用,请自己查(别问为什么,先得会搭建,会搭建再刨根问底,jar包我会上传,要是新手自己下载吧)。

2 编写web.xml加载spring和springmvc配置文件

<?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"
    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">
    <display-name>SSM2018_5_22</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <!-- 防止乱码得过滤器,粘贴过来使用就可以 -->
    <filter>
        <filter-name>characterEncoding</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>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--.................................................... -->
    <!-- 加载spring配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <!--contextConfigLocation是 org.springframework.web.context.ContextLoaderListener类固定属性名字 -->
        <param-name>contextConfigLocation</param-name>
        <!--就是注入 org.springframework.web.context.ContextLoaderListener 类属性 -->
        <param-value>classpath:config/ApplicationContext.xml</param-value>
    </context-param>
    <!--.................................................... -->
    <!-- spring mvc配置 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--就是注入org.springframework.web.servlet.DispatcherServlet 类属性 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/ApplicationContext-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

3 编写spring和springmvc配置文件

    3.1spring配置文件

    <?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: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-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <context:component-scan base-package="cn.wazh"></context:component-scan>
    <!-- 第一步:配置数据源 -->
    <context:property-placeholder location="classpath:config/jdbc.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 第二步:创建sqlSessionFactory。生产sqlSession -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:config/sqlMapConfig.xml"></property>
        <property name="mapperLocations" value="classpath:com/wazh/dao/*Mapper.xml"></property>
    </bean>
    <!-- 配置mybatis接口代理开发 * 接口类名和映射文件必须同名 * 接口类和映射文件必须在同一个目录下 * 映射文件namespace名字必须是接口的全类路径名
        * 接口的方法名必须和映射Statement的id一致 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wazh.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 第三步:事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>

    </tx:advice>

    <!-- 配置拦截service -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.wazh.dao.*.*(..))" />
    </aop:config>

</beans>

3.2 springmvc配置文件

    <?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: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-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:component-scan base-package="com.wazh"></context:component-scan>

    <!-- annotation-driven:默认创建了多个对象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
        也就提供对json格式支持 -->
    <mvc:annotation-driven />

    <!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000"></property>
    </bean>

</beans>

3.3创建一个空的mybaties文件,这里面可以写别名,很有用,我们只是搭建简单得可以用就可以

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

4 创建dao domain service control包


 创建一个数据库,一张表(简单点,实现增删改查就可以)

4.1domain层

package com.wazh.domain;

public class Person {
   private String name;
   private String sex;
   private int id;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
@Override
public String toString() {
    return "Person [name=" + name + ", sex=" + sex + ", id=" + id + "]";
}

}

4.2dao层

package com.wazh.dao;

import java.util.List;

import com.wazh.domain.Person;

public interface PersonDao {
       List<Person> GetAllDao();
       int insertPersonDao(Person person);
       int deletePersonDao(int id);
       Person findPersonByIdDao(int id);
       int updateDao(Person person);
}

4.3ampper.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.wazh.dao.PersonDao">
    <select id="MakeLove"
        resultType="com.wazh.domain.Person">
        select * from ren
    </select>
    <delete id="deletePersonDao" parameterType="int">
      DELETE from ren where id=#{id}
    </delete>
    <!-- <update id="updatePersonDao">
    </update>
    <insert id="insertPersonDao" parameterType="com.wazh.domain.Person">
    </insert> -->
    <select id="findPersonByIdDao" parameterType="int" resultType="com.wazh.domain.Person">
    select * from ren where id=#{id}
    </select>
    <update id="updateDao" parameterType="com.wazh.domain.Person" >
    
    update ren set name=#{name},sex=#{sex} where id=#{id}
  </update>
  <insert id="insertPersonDao"  parameterType="com.wazh.domain.Person">
     insert into ren values(#{name},#{sex},#{id})
  </insert>
</mapper>

4.4service层

package com.wazh.service;

import java.util.List;

import com.wazh.domain.Person;

public interface PersonServicedao {
     public  List<Person> MakeLoveService();
     public int insertPersonService(Person person);
     public int deletePersonService(int id);
     public Person findPersonByIdService(int id);
     public int updateService(Person person);
     public int insertService(Person person);

}

4.5control层

package com.wazh.control;

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

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wazh.domain.Person;
import com.wazh.service.PersonServicedao;

@Controller
public class renController {
  @Resource
  private PersonServicedao personservice;
  @RequestMapping("toMain")
  public String toMain(Model model,HttpServletRequest req,HttpServletResponse res) {
      System.out.println("toMain方法实现");
      List<Person> list=personservice.MakeLoveService();
      req.setAttribute("list", list);
      return "index";
  }
  @RequestMapping("update")
  public String update(Model model,HttpServletRequest req,int id,HttpServletResponse res) throws IOException {
    req.setAttribute("xiaoren", personservice.findPersonByIdService(id));
     return "editor";
  }
  @RequestMapping("saveupdate")
      public String saveUpdate(Person person) throws IOException {
      int result=personservice.updateService(person);
         return "redirect:toMain.do";
      }
  @RequestMapping("insertready")
  public String insertready() throws IOException {
     return "add";
  }
  @RequestMapping("insert")
  public String insert(Person person) throws IOException {
     personservice.insertService(person);
     return "redirect:toMain.do";
  }
  @RequestMapping("delete")
  public String delete(int id) throws IOException {
     personservice.deletePersonService(id);
     return "redirect:toMain.do";
  }
}

5 全部jsp

5.1展示jsp_indexjsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<base href="<%=basePath%>">

<%
    
%>
<!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>首页</title>
</head>
<body>
    <span><%=basePath%></span>
    <br />
    <span><%=path%></span>
    <table border="2px" width="400px" height="50px">
        <th>id</th>
        <th>姓名</th>
        <th>性别</th>
        <c:forEach items="${list }" var="item">
            <tr>
                <td>${item.id }</td>
                <td>${item.name }</td>
                <td>${item.sex }</td>
                <td><a href="<%=basePath%>update.do?id=${item.id}">修改</a></td>
                <td><a href="<%=basePath%>delete.do?id=${item.id }">删除</a></td>
            </tr>
        </c:forEach>
        <tr>
        <td><a href="<%=basePath%>insertready.do">增加</a></td>
        </tr>
    </table>
</body>
</html>

5.2 add.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>
<form action="${pageContext.request.contextPath }\insert.do">
<table>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<tr>
<td><input type="text" id="id" name="id" >${id}</input></td>
<td><input type="text" id="name" name="name">${name}</input></td>
<td><input type="text" id="sex" name="sex">${sex}</input></td>
</tr>
</table>
<input type="submit" value="提交吧">
</form>
</body>

</html>

5.3editor.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>
<form action="${pageContext.request.contextPath }/saveupdate.do" method="post">
<input type="text"  name="id"   >${xiaoren.id }</input></br>
<input type="text"  name="name" >${xiaoren.name}</input></br>
<input type="text"  name="sex" > ${xiaoren.sex}</input></br>
<input type="submit" value="提交吧">
</form>
</body>
</html>

    

  

作为新入行得新手,水平很low,大家见谅哈,(除了会用框架,多问自己框架怎么实现得)有问题一起交流 ,我qq1228029181。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值