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

前言:

想必大家对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配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14.  
  15. <context:property-placeholder location="classpath:remote/db.properties"
  16. ignore-unresolvable="true" />
  17. <!-- 配置数据源 使用的是Druid数据源 -->
  18. <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  19. init-method="init" destroy-method="close">
  20. <property name="url" value="${jdbc.url}" />
  21. <property name="username" value="${jdbc.username}" />
  22. <property name="password" value="${jdbc.password}" />
  23. <property name="driverClassName" value="${jdbc.driver}" />
  24.  
  25. <!-- 初始化连接大小 -->
  26. <property name="initialSize" value="0" />
  27. <!-- 连接池最大使用连接数量 -->
  28. <property name="maxActive" value="20" />
  29.  
  30. <!-- 连接池最小空闲 -->
  31. <property name="minIdle" value="0" />
  32. <!-- 获取连接最大等待时间 -->
  33. <property name="maxWait" value="60000" />
  34. <property name="poolPreparedStatements" value="true" />
  35. <property name="maxPoolPreparedStatementPerConnectionSize"
  36. value="33" />
  37. <!-- 用来检测有效sql -->
  38. <property name="validationQuery" value="${validationQuery}" />
  39. <property name="testOnBorrow" value="false" />
  40. <property name="testOnReturn" value="false" />
  41. <property name="testWhileIdle" value="true" />
  42. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  43. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  44. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  45. <property name="minEvictableIdleTimeMillis" value="25200000" />
  46. <!-- 打开removeAbandoned功能 -->
  47. <property name="removeAbandoned" value="true" />
  48. <!-- 1800秒,也就是30分钟 -->
  49. <property name="removeAbandonedTimeout" value="1800" />
  50. <!-- 关闭abanded连接时输出错误日志 -->
  51. <property name="logAbandoned" value="true" />
  52. <!-- 监控数据库 -->
  53. <property name="filters" value="mergeStat" />
  54. </bean>
  55.  
  56. <!-- myBatis文件内嵌 -->
  57. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  58. <property name="dataSource" ref="dataSource" />
  59. <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
  60. <property name="configLocation" value="classpath:spring/myBatis-config.xml" />
  61. <property name="mapperLocations" value="classpath:com/demo/mapper/*.xml" />
  62. <!-- 配置PageHelper拦截实现分页 -->
  63. <!-- <property name="plugins"> -->
  64. <!-- <array> -->
  65. <!-- <bean id="sqlInterceptor" class="com.shou6.filter.SqlInterceptor"> -->
  66. <!-- 拦截的sql语句 -->
  67. <!-- <property name="sql_Intercept" value="^\s*select[\s\S]*$" /> -->
  68. <!-- 不拦截的sql语句 ^\s*select\s+count\s*\\(\s*(?:\*|\w+)\s*\)\s+[\s\S]+$ -->
  69. <!-- <property name="sql_Not_Intercept" value="^\s*select\s+[\s\S]+\S+_enable=1\s+[\s\S]+$" /> -->
  70. <!-- </bean> -->
  71. <!-- <bean class="com.github.pagehelper.PageHelper"> -->
  72. <!-- <property name="properties"> -->
  73. <!-- <value> -->
  74. <!-- dialect=mysql -->
  75. <!-- 该参数默认为false,设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用,和startPage中的pageNum效果一样 -->
  76. <!-- offsetAsPageNum=true -->
  77. <!-- 该参数默认为false,设置为true时,使用RowBounds分页会进行count查询 -->
  78. <!-- rowBoundsWithCount=true -->
  79. <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page类型) -->
  80. <!-- <property name="pageSizeZero" value="true"/> -->
  81.  
  82. <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
  83. <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
  84. <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
  85. <!-- PageHelper.startPage(currentPage, pageSize, true)//第三个参数为true时,该设置的“pageNum>pages会查询最后一页”才生效 -->
  86. <!-- reasonable=false -->
  87. <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
  88. <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
  89. <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
  90. <!-- 不理解该含义的前提下,不要随便复制该配置 <property name="params" value="pageNum=start;pageSize=limit;"/> -->
  91. <!-- </value> -->
  92. <!-- </property> -->
  93. <!-- </bean> -->
  94. <!-- </array> -->
  95. <!-- </property> -->
  96. </bean>
  97. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  98. <property name="basePackage" value="com.demo.dao" />
  99. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  100. </bean>
  101.  
  102. </beans>

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5.  
  6. <!-- 通过别名简化对类的使用 -->
  7. <typeAliases>
  8. <!-- 通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean, 并且默认设置一个别名,默认的名字为非限定类名来作为它的别名。 -->
  9. <package name="com.demo.entity" />
  10. </typeAliases>
  11. </configuration>

5.application-trans:事务管理

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/util
  14. http://www.springframework.org/schema/util/spring-util.xsd">
  15. <!-- 配置事务管理器 -->
  16. <bean id="transactionManager"
  17. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  18. <property name="dataSource" ref="dataSource" />
  19. </bean>
  20.  
  21. <!-- 拦截器方式配置事物 -->
  22. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  23. <tx:attributes>
  24. <!-- 传播行为 -->
  25. <tx:method name="get*" propagation="REQUIRED" read-only="true" />
  26. <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
  27. </tx:attributes>
  28. </tx:advice>
  29.  
  30. <!-- 方式1:注解方式配置事物管理 -->
  31. <!-- <tx:annotation-driven transaction-manager="txAdvice" /> -->
  32.  
  33. <!-- 方式2:Spring Aop配置事务管理 expose-proxy="true":解决非事务方法调用本类事务方法时事务不起作用 -->
  34. <aop:config expose-proxy="true">
  35. <aop:pointcut id="txPoint" expression="execution(* com.demo.service.impl.*.*(..))" />
  36. <aop:advisor pointcut-ref="txPoint" advice-ref="txAdvice" />
  37. </aop:config>
  38.  
  39. </beans>

5.application:核心配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd">
  12. <!-- xsi:schemaLocation尽量不配版本号,这样会默认从本地加载xsd文件,断网不会导致加载出错 -->
  13.  
  14. <!--引入配置属性文件,使用@Value获取值 -->
  15. <!-- <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> -->
  16. <!-- <property name="locations"> -->
  17. <!-- <list> -->
  18. <!-- <value>classpath:nofilter.properties</value> -->
  19. <!-- <value>classpath:config/param-injection.properties</value> -->
  20. <!-- </list> -->
  21. <!-- </property> -->
  22. <!-- <property name="fileEncoding" value="UTF-8"/> -->
  23. <!-- </bean> -->
  24.  
  25. <!-- <import resource="application-quartz.xml"/> -->
  26. <import resource="application-mybatis.xml"/>
  27. <!-- <import resource="application-redis.xml"/> -->
  28.  
  29. <!--自动扫描含有@Service将其注入为bean -->
  30. <context:component-scan base-package="com.demo.service" />
  31.  
  32. <import resource="application-trans.xml"/>
  33.  
  34. <!-- 用于持有ApplicationContext,可以使用BeanMgrUtil.getBean('xxxx')的静态方法得到spring bean对象 -->
  35. <!-- <bean class="com.shou6.utils.tools.BeanMgrUtil" lazy-init="false" /> -->
  36.  
  37. </beans>

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd">
  14.  
  15. <!-- spring全局异常捕获 -->
  16. <!-- <bean class="com.shou6.utils.exception.ExceptionHandler" /> -->
  17.  
  18. <!-- AOP拦截controller注意:需要把切面类和controller 放在同一个spring的xml配置文件中 -->
  19. <context:component-scan base-package="com.demo.controller"/>
  20. <!-- <context:component-scan base-package="com.shou6.filter"/> -->
  21.  
  22. <!-- 对@AspectJ切面的bean创建代理(不设置则@Aspect注解的切面将无效) -->
  23. <aop:aspectj-autoproxy proxy-target-class="true" />
  24.  
  25. <!-- Jackson转换器 -->
  26. <bean id="mappingJacksonHttpMessageConverter"
  27. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  28. <property name="supportedMediaTypes">
  29. <list>
  30. <value>application/json;charset=UTF-8</value>
  31. <value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
  32. </list>
  33. </property>
  34. </bean>
  35.  
  36. <!-- String转换器(为配合APP支付异步通知) -->
  37. <bean id="stringHttpMessageConverter"
  38. class="org.springframework.http.converter.StringHttpMessageConverter">
  39. <constructor-arg value="UTF-8" index="0"></constructor-arg><!--避免出现乱码 -->
  40. <property name="supportedMediaTypes">
  41. <list>
  42. <value>text/plain;charset=UTF-8</value>
  43. <value>text/xml;charset=UTF-8</value>
  44. </list>
  45. </property>
  46. </bean>
  47.  
  48. <!-- 启动Spring MVC的注解功能,完成请求和返回的POJO-json/xml自动转换 -->
  49. <bean
  50. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  51. <property name="messageConverters">
  52. <list>
  53. <!-- json转换器 -->
  54. <ref bean="mappingJacksonHttpMessageConverter" />
  55. <ref bean="stringHttpMessageConverter" />
  56. </list>
  57. </property>
  58. </bean>
  59.  
  60. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  61. <bean
  62. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  63. <property name="viewClass"
  64. value="org.springframework.web.servlet.view.JstlView" />
  65. <!-- 解决默认返回均是application/json格式问题 -->
  66. <property name="contentType" value="text/html;charset=UTF-8" />
  67. <property name="prefix" value="/" />
  68. <property name="suffix" value=".jsp" />
  69. </bean>
  70.  
  71. <!-- 配置多文件上传 -->
  72. <bean id="multipartResolver"
  73. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  74. <property name="defaultEncoding">
  75. <value>UTF-8</value>
  76. </property>
  77. <property name="maxUploadSize">
  78. <!-- 上传文件大小限制为5M,5120*1000b -->
  79. <value>5120000</value>
  80. </property>
  81. <property name="maxInMemorySize">
  82. <value>4096</value>
  83. </property>
  84. </bean>
  85.  
  86. </beans>

7.log4j:日志配置文件

  1. # Rules reminder:
  2. # DEBUG &lt; INFO &lt; WARN &lt; ERROR &lt; FATAL
  3.  
  4. # Global logging configuration
  5. log4j.rootLogger=debug,stdout
  6.  
  7. # My logging configuration...
  8. log4j.logger.cn.jbit.mybatisdemo=DEBUG
  9.  
  10. ## Console output...
  11. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  12. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  13. log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
  14.  
  15. log4j.logger.org.apache.ibatis=DEBUG
  16. ## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
  17. log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
  18. ## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
  19. log4j.logger.java.sql.Connection=DEBUG
  20. log4j.logger.java.sql.Statement=DEBUG
  21. log4j.logger.java.sql.PreparedStatement=DEBUG
  22.  
  23. #base log config
  24. log4j.rootLogger=INFO,CONSOLE,INFO_FILE,ERROR_FILE
  25. log4j.addivity.org.apache=true
  26.  
  27. #console config
  28. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
  29. log4j.appender.CONSOLE.Threshold=INFO
  30. log4j.appender.CONSOLE.Target=System.out
  31. log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
  32. log4j.appender.CONSOLE.layout.ConversionPattern=[%d{HH\:mm\:ss ms}]-%5p (%F\:%L)\:%m%n
  33.  
  34. # information logs config
  35. log4j.appender.INFO_FILE=org.apache.log4j.DailyRollingFileAppender
  36. log4j.appender.INFO_FILE.File=..\\logs\\OSS-INFO.log
  37. log4j.appender.INFO_FILE.Threshold=INFO
  38. log4j.appender.INFO_FILE.Append=true
  39. log4j.appender.INFO_FILE.layout=org.apache.log4j.PatternLayout
  40. log4j.appender.INFO_FILE.layout.ConversionPattern=[%d{HH\:mm\:ss ms}]-[%p]\:%m -&gt;%l %n
  41.  
  42. # error logs config
  43. log4j.appender.ERROR_FILE=org.apache.log4j.DailyRollingFileAppender
  44. log4j.appender.ERROR_FILE.File=..\\logs\\OSS-ERROR.log
  45. log4j.appender.ERROR_FILE.Threshold=ERROR
  46. log4j.appender.ERROR_FILE.Append=true
  47. log4j.appender.ERROR_FILE.layout=org.apache.log4j.PatternLayout
  48. log4j.appender.ERROR_FILE.layout.ConversionPattern=[%d{HH\:mm\:ss ms}]-[%p]\:%m -&gt;%l %n
  49.  
  50. # fail bill logs config
  51. log4j.logger.FAIL_BILL=INFO, FAIL_BILL
  52. log4j.appender.FAIL_BILL=org.apache.log4j.DailyRollingFileAppender
  53. log4j.appender.FAIL_BILL.File=..\\logs\\FAIL_BILL.log
  54. log4j.appender.FAIL_BILL.Threshold=FAIL_BILL
  55. log4j.appender.FAIL_BILL.Append=true
  56. log4j.appender.FAIL_BILL.layout=org.apache.log4j.PatternLayout
  57. log4j.appender.FAIL_BILL.layout.ConversionPattern=[%d{HH\:mm\:ss ms}]\:%m %n

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>MyDemo</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring/application.xml</param-value>
  10. </context-param>
  11.  
  12. <!-- 监听spring -->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16.  
  17. <!-- 启动系统服务 -->
  18. <!-- <listener> -->
  19. <!-- <listener-class>com.shou6.init.ServerInitHandler</listener-class> -->
  20. <!-- </listener> -->
  21.  
  22. <!-- 防止内存泄漏 -->
  23. <listener>
  24. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  25. </listener>
  26.  
  27. <!-- 编码过滤 -->
  28. <filter>
  29. <filter-name>encodingFilter</filter-name>
  30. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  31. <init-param>
  32. <param-name>encoding</param-name>
  33. <param-value>utf-8</param-value>
  34. </init-param>
  35. <init-param>
  36. <param-name>forceEncoding</param-name>
  37. <param-value>true</param-value>
  38. </init-param>
  39. </filter>
  40. <filter-mapping>
  41. <filter-name>encodingFilter</filter-name>
  42. <url-pattern>/*</url-pattern>
  43. </filter-mapping>
  44.  
  45. <!-- spring mvc -->
  46. <servlet>
  47. <description>spring mvc servlet</description>
  48. <servlet-name>mvcServlet</servlet-name>
  49. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  50. <init-param>
  51. <param-name>contextConfigLocation</param-name>
  52. <param-value>
  53. classpath:spring/spring-mvc.xml
  54. </param-value>
  55. </init-param>
  56. <load-on-startup>1</load-on-startup>
  57. </servlet>
  58. <servlet-mapping>
  59. <servlet-name>mvcServlet</servlet-name>
  60. <url-pattern>*.do</url-pattern>
  61. <url-pattern>*.mdo</url-pattern>
  62. </servlet-mapping>
  63.  
  64. </web-app>

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

下面我就来测试一下

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

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

  1. /**
  2. * @author dsn
  3. *
  4. * @version 创建时间:2018年7月5日 上午11:30:00
  5. */
  6. package com.demo.entity;
  7.  
  8. /**
  9. * @author dsn
  10. * @version 创建时间:2018年7月5日 上午11:30:00
  11. */
  12. public class User {
  13.  
  14. /**
  15. * @return the id
  16. */
  17. public int getId() {
  18. return id;
  19. }
  20.  
  21. /**
  22. * @param id
  23. * the id to set
  24. */
  25. public void setId(int id) {
  26. this.id = id;
  27. }
  28.  
  29. /**
  30. * @return the username
  31. */
  32. public String getUsername() {
  33. return username;
  34. }
  35.  
  36. /**
  37. * @param username
  38. * the username to set
  39. */
  40. public void setUsername(String username) {
  41. this.username = username;
  42. }
  43.  
  44. /**
  45. * @return the userpass
  46. */
  47. public String getUserpass() {
  48. return userpass;
  49. }
  50.  
  51. /**
  52. * @param userpass
  53. * the userpass to set
  54. */
  55. public void setUserpass(String userpass) {
  56. this.userpass = userpass;
  57. }
  58.  
  59. /**
  60. * @return the name
  61. */
  62. public String getName() {
  63. return name;
  64. }
  65.  
  66. /**
  67. * @param name
  68. * the name to set
  69. */
  70. public void setName(String name) {
  71. this.name = name;
  72. }
  73.  
  74. private int id;
  75. private String username;
  76. private String userpass;
  77. private String name;
  78.  
  79. }

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

  1. /**
  2. * @author dsn
  3. *
  4. * @version 创建时间:2018年7月5日 上午11:35:10
  5. */
  6. package com.demo.dao;
  7.  
  8. import com.demo.entity.User;
  9.  
  10. /**
  11. * @author dsn
  12. * @version 创建时间:2018年7月5日 上午11:35:10
  13. */
  14. public interface UserMapper extends BaseMapper<User> {
  15.  
  16. }

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

  1. /**
  2. * @author dsn
  3. *
  4. * @version 创建时间:2018年7月5日 上午11:35:36
  5. */
  6. package com.demo.service;
  7.  
  8. import com.demo.entity.User;
  9.  
  10. /**
  11. * @author dsn
  12. * @version 创建时间:2018年7月5日 上午11:35:36
  13. */
  14. public interface UserService extends BaseService<User> {
  15.  
  16. }

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.demo.dao.UserMapper" >
  4. <resultMap id="userResultMap" type="com.demo.entity.User" >
  5. <id column="id" property="id" jdbcType="INTEGER" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="username" property="username" jdbcType="VARCHAR" />
  8. <result column="userpass" property="userpass" jdbcType="VARCHAR" />
  9. </resultMap>
  10. <select id="selectList" parameterType="User" resultMap="userResultMap">
  11. select
  12. id,username,userpass,name
  13. from user
  14. <where>
  15. disable='1'
  16. <if test="key!=null">
  17. and (
  18. username = #{key,jdbcType=VARCHAR}
  19. or
  20. name = #{key,jdbcType=VARCHAR}
  21. )
  22. </if>
  23. </where>
  24. </select>
  25. <insert id="insert" parameterType="User"
  26. useGeneratedKeys="true" keyProperty="id">
  27. insert into user(name,username,userpass)
  28. values(#{name},#{username},#{userpass})
  29. </insert>
  30. </mapper>

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

  1. /**
  2. * @author dsn
  3. *
  4. * @version 创建时间:2018年7月5日 上午11:47:26
  5. */
  6. package com.demo.controller;
  7.  
  8. import javax.annotation.Resource;
  9.  
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13.  
  14. import com.demo.entity.User;
  15. import com.demo.service.UserService;
  16.  
  17. /**
  18. * @author dsn
  19. * @version 创建时间:2018年7月5日 上午11:47:26
  20. */
  21. @Controller
  22. @RequestMapping("user")
  23. public class UserAction {
  24.  
  25. @Resource
  26. private UserService userService;
  27.  
  28. @ResponseBody
  29. @RequestMapping(value = "/adduser")
  30. public String addactivity(User user, String msg) throws Exception {
  31. int insert = userService.insert(user);
  32. if (insert == 1) {
  33. msg = "插入成功";
  34. } else {
  35. msg = "插入失败";
  36. }
  37. return msg;
  38. }
  39.  
  40. }

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

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

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

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

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

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

maven搭建ssm初级框架的更多相关文章

  1. Maven项目搭建(二):Maven搭建SSM框架

    上一章给大家讲解了如何使用Maven搭建web项目. 这次给大家介绍一下怎么使用Maven搭建SSM框架项目. 首先我们来看一下pom.xml的属性介绍: project: pom的xml根元素. p ...

  2. Eclipse中使用Maven搭建SSM框架

    Eclipse中不使用Maven搭建SSM框架:https://www.cnblogs.com/xuyiqing/p/9569459.html IDEA中使用Maven搭建SSM框架:https:// ...

  3. 使用maven搭建ssm框架的javaweb项目

    目前主流的javaweb项目,常会用到ssm(Spring+Spring MVC+Mybatis)框架来搭建项目的主体框架,本篇介绍搭建SSM框架的maven项目的实施流程.记之共享! 一.SSM框架 ...

  4. 使用maven搭建SSM框架

    使用maven搭建SSM框架,首先得准备好maven环境. 搭建maven环境 第一步:下载maven http://maven.apache.org/download.cgi 下载后解压就可以了. ...

  5. 使用Maven搭建SSM框架(Eclipse)

    今天学习一下使用Maven搭建SSM框架,以前都是用别人配置好的框架写代码,今天试试自己配置一下SSM框架. 这里我的参数是Windows7 64位,tomcat9,eclipse-jee-neon- ...

  6. 使用maven搭建ssm框架环境

    1.前言 因为经常换环境,在搭ssm框架的时候老是出错,所以记录一下最近搭建的环境,以供参考. 本文讲解如何使用maven搭建ssm框架,并能用于简单的登录注册. IDE:IDEA,JDK版本:1.8 ...

  7. Maven 搭建 SSM 项目 (oracle)

    简单谈一下maven搭建 ssm 项目 (使用数据库oracle,比 mysql 难,所以这里谈一下) 在创建maven 的web项目时,常常会缺了main/java , main/test 两个文件 ...

  8. maven搭建ssm

    前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实战>.其实园内这方面文章已有不少,那么为什么我还要重复造轮子呢?我只是想记录自己的实践 ...

  9. 【原】无脑操作:eclipse + maven搭建SSM框架

    网上看到一些Spring + Spring MVC + MyBatis框架的搭建教程,不是很详细或是时间久远了,自己动手整一个简单无脑的! 0.系统环境 1)Windows 10 企业版 2)JDK ...

随机推荐

  1. docker 加速器配置目录

    centos 7 : /lib/systemd/system/docker.service

  2. 01.Java 开发简单的计算器

    难度为一般,适合具有 Java 基础和 Swing 组件编程知识的用户学习一. 实验介绍1.1 实验内容本次实验利用Java开发一个可以进行简单的四则运算的图形化计算器,会使用到 Java Swing ...

  3. 2018.11.01 NOIP训练 图论(线段树+倍增+dfs序)

    传送门 一道挺妙的题. 对于询问点(u,v),如右图所示,我们可以发现存在一个点m在u->v的路径中,m子树的点到u是最近的,m子树外到v是最近的.其中dis(u,m)=(dis(u,v)-1) ...

  4. s5-2 Cpu调度算法

    调度程序采用什么算法选择一个进程(作业)? 如何评价调度算法的性能? 调度准则 CPU利用率 – 使CPU尽可能的忙碌 吞吐量 – 单位时间内运行完的进程数 周转时间 – 进程从提交到运行结束的全部时 ...

  5. mysql学习之路_基础知识

                    Mysql php阶段将数据库分为三个阶 基础阶段: mysql数据库的基本操作(增删改查),以及一些高级操作(视图,触发器,函数,存储过程等),PHP操作没有sql数 ...

  6. XML xmlns

    xmlns xml namespaces 参考 http://www.w3school.com.cn/tags/tag_prop_xmlns.asp http://www.w3school.com.c ...

  7. C#重点内容之:事件(Event)

    一.事件的五个组成部分 事件的五个部分: 事件拥有者(对象) 事件成员(Event ,成员) 事件的响应者(对象) 事件处理器(成员,本质是一个回调方法) 事件订阅——把事件处理器与事件关联在一起 二 ...

  8. 2.2.11同步synchronized方法无限等待与解决

    同步方法容易造成死循环. package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { s ...

  9. 1.7.8使用return 停止线程

    package com.cky.thread; /** * Created by edison on 2017/12/3. */ public class MyThread12 extends Thr ...

  10. 知识点:定义input type=file 样式的方法(转)

    ——“当我们想要用css美化表单的时候,有两个控件就会和前段人员作对,一个是是大名鼎鼎的select,另一个就是我现在要说说的 input type=file” 为什么要美化file控件?试想一下,别 ...