ssm的初步整合

新建一个web项目名字为app 在lib下导入所需要的jar包

如


然后再src根目录下创建配置文件如图如图


bean.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:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
      <context:annotation-config></context:annotation-config>
      <context:component-scan base-package="lcw.*"></context:component-scan>
     <!-- datasource -->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- results in a setDriverClassName(String) call -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/app"/>
            <property name="username" value="root"/>
        <property name="password" value="root"/>
      </bean>
    <!-- sessionfactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:lcw/mapping/*.xml"></property>
        <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
    </bean>
    
    <!-- dao注入 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="lcw.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- transaction -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- aspect -->
    <aop:config>
        <aop:pointcut id="businessService"
               expression="execution(public * lcw.serviceimpl..*.*(..))"/>
        <aop:advisor pointcut-ref="businessService" advice-ref="tx-advice"/>
    </aop:config>

    <tx:advice id="tx-advice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
           </tx:attributes>
    </tx:advice>

    
</beans>


数据库要存在,不然会报错

mvc.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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="lcw.controller"></context:component-scan>
</beans>


mybatis.cfg.xml内容


<?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>
         <typeAliases>
            <package name="lcw.model"></package>
        </typeAliases>
    </configuration>


web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!-- spring -->
      <display-name>Struts Blank</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> -->
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <!-- spring mvc -->
     <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:mvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      
    <welcome-file-list>
        <welcome-file>JSP/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
实体类

package lcw.model;

public class fload {
    private Integer fId;

    private String fName;

    public Integer getfId() {
        return fId;
    }

    public void setfId(Integer fId) {
        this.fId = fId;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName == null ? null : fName.trim();
    }
}


dao如下

package lcw.dao;

import lcw.model.fload;

public interface floadMapper {
    int deleteByPrimaryKey(Integer fId);

    int insert(fload record);

    int insertSelective(fload record);

    fload selectByPrimaryKey(Integer fId);

    int updateByPrimaryKeySelective(fload record);

    int updateByPrimaryKey(fload record);
}


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="lcw.dao.floadMapper" >
  <resultMap id="BaseResultMap" type="lcw.model.fload" >
    <id column="f_id" property="fId" jdbcType="INTEGER" />
    <result column="f_name" property="fName" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    f_id, f_name
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from fload
    where f_id = #{fId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from fload
    where f_id = #{fId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="lcw.model.fload" >
    insert into fload (f_id, f_name)
    values (#{fId,jdbcType=INTEGER}, #{fName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="lcw.model.fload" >
    insert into fload
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="fId != null" >
        f_id,
      </if>
      <if test="fName != null" >
        f_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="fId != null" >
        #{fId,jdbcType=INTEGER},
      </if>
      <if test="fName != null" >
        #{fName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="lcw.model.fload" >
    update fload
    <set >
      <if test="fName != null" >
        f_name = #{fName,jdbcType=VARCHAR},
      </if>
    </set>
    where f_id = #{fId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="lcw.model.fload" >
    update fload
    set f_name = #{fName,jdbcType=VARCHAR}
    where f_id = #{fId,jdbcType=INTEGER}
  </update>
</mapper>


service如下

package lcw.service;

import lcw.model.fload;

public interface testService {
    public void addTest(fload fload);
}

serviceimpl如此

package lcw.serviceimpl;

import javax.annotation.Resource;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import lcw.dao.floadMapper;
import lcw.model.fload;
import lcw.service.testService;

@Service("testImpl")
public class testImpl implements testService {
    
    @Resource
    private floadMapper floadMapper;
    public void addTest(fload fload) {
        floadMapper.insert(fload);
    }
}


controller如下

package lcw.controller;

import javax.annotation.Resource;

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

import lcw.model.fload;
import lcw.service.testService;
import lcw.serviceimpl.testImpl;

@Controller
public class testController {
        @Resource
        private testService testImpl;
        @RequestMapping("/addFload")
        public void addFload(String fname){
            fload fload = new fload();
            fload.setfName(fname);
            testImpl.addTest(fload);
        }
}

jsp如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'add.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
      <form action="addFload.do" method="post">
        <input type="text" name="fname"><br><br>
        <input type="submit" value="添加">&nbsp;&nbsp;&nbsp;
    </form>
  </body>
</html>

然后部署运行即可

别忘记简历数据库



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值