zoukankan      html  css  js  c++  java
  • maven搭建ssm初级框架

    喜欢的朋友可以关注下,粉丝也缺。

    前言:

    想必大家对smm框架已经熟悉的不能再熟悉了,它是由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。我们在一般的项目都能用到它,自己搭建一个smm也挺方便的。下面我就给大家介绍一下如何搭建一个初级的ssm框架。

    下面吧demo的下载贡献给大家,需要的可以去下载

    https://download.csdn.net/download/dsn727455218/10524640

    正文:

    1.创建一个maven项目,这个这里我就不做介绍了,很简单的,拿eclipse举例,下载maven插件就行,已经maven本地仓库。

    2.准备需要得jar包,maven仓库有可以直接引用的,没有的可以动态的下载。

    3.mybatis配置文件

    <?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:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" 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/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
     
        <context:property-placeholder location="classpath:remote/db.properties"
            ignore-unresolvable="true" />
        <!-- 配置数据源 使用的是Druid数据源 -->
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="driverClassName" value="${jdbc.driver}" />
     
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="0" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="20" />
     
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="0" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="60000" />
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize"
                value="33" />
            <!-- 用来检测有效sql -->
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000" />
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true" />
            <!-- 1800秒,也就是30分钟 -->
            <property name="removeAbandonedTimeout" value="1800" />
            <!-- 关闭abanded连接时输出错误日志 -->
            <property name="logAbandoned" value="true" />
            <!-- 监控数据库 -->
            <property name="filters" value="mergeStat" />
        </bean>
     
        <!-- myBatis文件内嵌 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="configLocation" value="classpath:spring/myBatis-config.xml" />
            <property name="mapperLocations" value="classpath:com/demo/mapper/*.xml" />
            <!-- 配置PageHelper拦截实现分页 -->
    <!--         <property name="plugins"> -->
    <!--             <array> -->
    <!--                 <bean id="sqlInterceptor" class="com.shou6.filter.SqlInterceptor"> -->
    <!--                     拦截的sql语句 -->
    <!--                     <property name="sql_Intercept" value="^s*select[sS]*$" /> -->
    <!--                     不拦截的sql语句 ^s*selects+counts*\(s*(?:*|w+)s*)s+[sS]+$ -->
    <!--                     <property name="sql_Not_Intercept" value="^s*selects+[sS]+S+_enable=1s+[sS]+$" /> -->
    <!--                 </bean> -->
    <!--                 <bean class="com.github.pagehelper.PageHelper"> -->
    <!--                     <property name="properties"> -->
    <!--                         <value> -->
    <!--                             dialect=mysql -->
    <!--                             该参数默认为false,设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用,和startPage中的pageNum效果一样 -->
    <!--                             offsetAsPageNum=true -->
    <!--                             该参数默认为false,设置为true时,使用RowBounds分页会进行count查询 -->
    <!--                             rowBoundsWithCount=true -->
    <!--                             设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page类型) -->
    <!--                             <property name="pageSizeZero" value="true"/> -->
     
    <!--                             3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
    <!--                             启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
    <!--                             禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
    <!--                             PageHelper.startPage(currentPage, pageSize, true)//第三个参数为true时,该设置的“pageNum>pages会查询最后一页”才生效 -->
    <!--                             reasonable=false -->
    <!--                             3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
    <!--                             增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
    <!--                             可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
    <!--                             不理解该含义的前提下,不要随便复制该配置 <property name="params" value="pageNum=start;pageSize=limit;"/> -->
    <!--                         </value> -->
    <!--                     </property> -->
    <!--                 </bean> -->
    <!--             </array> -->
    <!--         </property> -->
        </bean>
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.demo.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
     
    </beans>

    4.myBatis-config:自动扫描实体类

    <?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, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean, 并且默认设置一个别名,默认的名字为非限定类名来作为它的别名。 -->
            <package name="com.demo.entity" />
        </typeAliases>
    </configuration>

    5.application-trans:事务管理

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">
        <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
     
        <!-- 拦截器方式配置事物 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为 -->
                <tx:method name="get*" propagation="REQUIRED" read-only="true" />
                <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
            </tx:attributes>
        </tx:advice>
        
        <!-- 方式1:注解方式配置事物管理 -->
        <!-- <tx:annotation-driven transaction-manager="txAdvice" /> -->
        
        <!-- 方式2:Spring Aop配置事务管理  expose-proxy="true":解决非事务方法调用本类事务方法时事务不起作用 -->
        <aop:config  expose-proxy="true">
            <aop:pointcut id="txPoint" expression="execution(* com.demo.service.impl.*.*(..))" />  
            <aop:advisor pointcut-ref="txPoint" advice-ref="txAdvice" /> 
        </aop:config>
        
    </beans>

    5.application:核心配置文件

    <?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: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/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- xsi:schemaLocation尽量不配版本号,这样会默认从本地加载xsd文件,断网不会导致加载出错 -->
        
        <!--引入配置属性文件,使用@Value获取值 -->
    <!--     <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> -->
    <!--         <property name="locations"> -->
    <!--             <list> -->
    <!--                 <value>classpath:nofilter.properties</value> -->
    <!--                 <value>classpath:config/param-injection.properties</value> -->
    <!--             </list> -->
    <!--         </property> -->
    <!--         <property name="fileEncoding" value="UTF-8"/> -->
    <!--     </bean> -->
        
    <!--     <import resource="application-quartz.xml"/> -->
        <import resource="application-mybatis.xml"/>
    <!--     <import resource="application-redis.xml"/> -->
        
        <!--自动扫描含有@Service将其注入为bean -->
        <context:component-scan base-package="com.demo.service" />
        
        <import resource="application-trans.xml"/>
        
        <!-- 用于持有ApplicationContext,可以使用BeanMgrUtil.getBean('xxxx')的静态方法得到spring bean对象 -->  
    <!--     <bean class="com.shou6.utils.tools.BeanMgrUtil" lazy-init="false" />   -->
        
    </beans>

    6.spring-mvc:核心配置文件

    <?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        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/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
     
        <!-- spring全局异常捕获 -->
    <!--     <bean class="com.shou6.utils.exception.ExceptionHandler" /> -->
     
        <!-- AOP拦截controller注意:需要把切面类和controller 放在同一个spring的xml配置文件中 -->
        <context:component-scan base-package="com.demo.controller"/>
    <!--     <context:component-scan base-package="com.shou6.filter"/> -->
     
        <!-- 对@AspectJ切面的bean创建代理(不设置则@Aspect注解的切面将无效) -->
        <aop:aspectj-autoproxy proxy-target-class="true" />
     
        <!-- Jackson转换器 -->
        <bean id="mappingJacksonHttpMessageConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
                </list>
            </property>
        </bean>
     
        <!-- String转换器(为配合APP支付异步通知) -->
        <bean id="stringHttpMessageConverter"
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8" index="0"></constructor-arg><!--避免出现乱码 -->
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/xml;charset=UTF-8</value>
                </list>
            </property>
        </bean>
     
        <!-- 启动Spring MVC的注解功能,完成请求和返回的POJO-json/xml自动转换 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <!-- json转换器 -->
                    <ref bean="mappingJacksonHttpMessageConverter" />
                    <ref bean="stringHttpMessageConverter" />
                </list>
            </property>
        </bean>
     
        <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <!-- 解决默认返回均是application/json格式问题 -->
            <property name="contentType" value="text/html;charset=UTF-8" />
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
     
        <!-- 配置多文件上传 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
            <property name="maxUploadSize">
                <!-- 上传文件大小限制为5M,5120*1000b -->
                <value>5120000</value>
            </property>
            <property name="maxInMemorySize">
                <value>4096</value>
            </property>
        </bean>
     
    </beans>

    7.log4j:日志配置文件

    # Rules reminder:
    # DEBUG &lt; INFO &lt; WARN &lt; ERROR &lt; FATAL
     
    # Global logging configuration
    log4j.rootLogger=debug,stdout
     
    # My logging configuration...
    log4j.logger.cn.jbit.mybatisdemo=DEBUG
     
     
    ## Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
     
    log4j.logger.org.apache.ibatis=DEBUG
    ## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
    log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
    ## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG
     
    #base log config
    log4j.rootLogger=INFO,CONSOLE,INFO_FILE,ERROR_FILE
    log4j.addivity.org.apache=true
     
    #console config
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.Threshold=INFO
    log4j.appender.CONSOLE.Target=System.out
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=[%d{HH:mm:ss ms}]-%5p (%F:%L):%m%n
     
    # information logs config
    log4j.appender.INFO_FILE=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.INFO_FILE.File=..\logs\OSS-INFO.log
    log4j.appender.INFO_FILE.Threshold=INFO
    log4j.appender.INFO_FILE.Append=true
    log4j.appender.INFO_FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.INFO_FILE.layout.ConversionPattern=[%d{HH:mm:ss ms}]-[%p]:%m -&gt;%l %n
     
    # error logs config
    log4j.appender.ERROR_FILE=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.ERROR_FILE.File=..\logs\OSS-ERROR.log
    log4j.appender.ERROR_FILE.Threshold=ERROR
    log4j.appender.ERROR_FILE.Append=true
    log4j.appender.ERROR_FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.ERROR_FILE.layout.ConversionPattern=[%d{HH:mm:ss ms}]-[%p]:%m -&gt;%l %n
     
    # fail bill logs config
    log4j.logger.FAIL_BILL=INFO, FAIL_BILL 
    log4j.appender.FAIL_BILL=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.FAIL_BILL.File=..\logs\FAIL_BILL.log
    log4j.appender.FAIL_BILL.Threshold=FAIL_BILL
    log4j.appender.FAIL_BILL.Append=true
    log4j.appender.FAIL_BILL.layout=org.apache.log4j.PatternLayout
    log4j.appender.FAIL_BILL.layout.ConversionPattern=[%d{HH:mm:ss ms}]:%m %n

    8.web:项目启动加载spring配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>MyDemo</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application.xml</param-value>
      </context-param>
      
      <!-- 监听spring -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <!-- 启动系统服务 -->
    <!--   <listener> -->
    <!--     <listener-class>com.shou6.init.ServerInitHandler</listener-class> -->
    <!--   </listener> -->
      
       <!-- 防止内存泄漏 -->
      <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
      </listener>
      
      <!-- 编码过滤 -->
      <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>
     
      <!-- spring mvc -->
      <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>mvcServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
                    classpath:spring/spring-mvc.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>mvcServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
        <url-pattern>*.mdo</url-pattern>
      </servlet-mapping>
      
    </web-app>

    所需要的配置文件就这么多,当然你需要redis,quartz也可以加入配置。

    下面我就来测试一下

    在项目中我创建BaseMapper作为一个父类接口,只需要继承就可以调用其方法,是为了方便重复创建方法;BaseService也是同样的道理。

    首先我们创建一个实体类User:

    /**
     * @author dsn
     *
     * @version 创建时间:2018年7月5日 上午11:30:00 
     */
    package com.demo.entity;
     
    /**
     * @author dsn
     * @version 创建时间:2018年7月5日 上午11:30:00
     */
    public class User {
     
        /**
         * @return the id
         */
        public int getId() {
            return id;
        }
     
        /**
         * @param id
         *            the id to set
         */
        public void setId(int id) {
            this.id = id;
        }
     
        /**
         * @return the username
         */
        public String getUsername() {
            return username;
        }
     
        /**
         * @param username
         *            the username to set
         */
        public void setUsername(String username) {
            this.username = username;
        }
     
        /**
         * @return the userpass
         */
        public String getUserpass() {
            return userpass;
        }
     
        /**
         * @param userpass
         *            the userpass to set
         */
        public void setUserpass(String userpass) {
            this.userpass = userpass;
        }
     
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
     
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
     
        private int id;
        private String username;
        private String userpass;
        private String name;
     
    }

    创建一个dao层接口:继承BaseMapper引入实体类User

    /**
     * @author dsn
     *
     * @version 创建时间:2018年7月5日 上午11:35:10 
     */
    package com.demo.dao;
     
    import com.demo.entity.User;
     
    /**
     * @author dsn
     * @version 创建时间:2018年7月5日 上午11:35:10
     */
    public interface UserMapper extends BaseMapper<User> {
     
    }

    创建一个service接口:继承BaseService引入实体类User

    /**
     * @author dsn
     *
     * @version 创建时间:2018年7月5日 上午11:35:36 
     */
    package com.demo.service;
     
    import com.demo.entity.User;
     
    /**
     * @author dsn
     * @version 创建时间:2018年7月5日 上午11:35:36
     */
    public interface UserService extends BaseService<User> {
     
    }

    创建一个Mapper文件:UserMapper.xml,这里我写一个insert的语句以及一个select语句,需要注意的是column需要与实体类User字段对应以及对应的type

    <?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.demo.dao.UserMapper" >
      <resultMap id="userResultMap" type="com.demo.entity.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="userpass" property="userpass" jdbcType="VARCHAR" />
      </resultMap>
      <select id="selectList" parameterType="User" resultMap="userResultMap">
          select
        id,username,userpass,name
        from user 
        <where>
            disable='1' 
            <if test="key!=null">
                and (
                username = #{key,jdbcType=VARCHAR}
                    or
                name = #{key,jdbcType=VARCHAR}
                )
            </if>
        </where>
      </select>
      <insert id="insert" parameterType="User" 
            useGeneratedKeys="true" keyProperty="id"> 
            insert into user(name,username,userpass)  
                 values(#{name},#{username},#{userpass})  
      </insert>
    </mapper>

    接下来我们创建一个UserAction:这是作为controller控制器,处理所有的访问请求以及逻辑

    /**
     * @author dsn
     *
     * @version 创建时间:2018年7月5日 上午11:47:26 
     */
    package com.demo.controller;
     
    import javax.annotation.Resource;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
     
    import com.demo.entity.User;
    import com.demo.service.UserService;
     
    /**
     * @author dsn
     * @version 创建时间:2018年7月5日 上午11:47:26
     */
    @Controller
    @RequestMapping("user")
    public class UserAction {
     
        @Resource
        private UserService userService;
     
        @ResponseBody
        @RequestMapping(value = "/adduser")
        public String addactivity(User user, String msg) throws Exception {
            int insert = userService.insert(user);
            if (insert == 1) {
                msg = "插入成功";
            } else {
                msg = "插入失败";
            }
            return msg;
        }
     
    }

    启动项目,用postman测试一下,完美收官。

    下面吧demo的下载贡献给大家,需要的可以去下载

    https://download.csdn.net/download/dsn727455218/10524640

    如有需要可以加我Q群【308742428】大家一起讨论技术。

    后面会不定时为大家更新文章,敬请期待。

    喜欢的朋友可以关注下,粉丝也缺。

  • 相关阅读:
    04 UUID
    MD5加密算法(信息摘要算法)、Base64算法
    03 MD5加密、Base64处理
    MVC分层思想、SSM编程架构
    1网络编程基本概念
    Tomcat闪退的解决办法
    win10下的jdk1.8安装
    枚举练习
    1000元买物品分配
    win10解决vc++6.0不兼容问题方法
  • 原文地址:https://www.cnblogs.com/dsn727455218/p/9273404.html
Copyright © 2011-2022 走看看