新建数据库ssm

建立数据库表user

  1. CREATE TABLE `user` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT ,
  3.   `sex` varchar(255) NULL ,
  4.   `name` varchar(255) NULL ,
  5.   PRIMARY KEY (`id`)
  6. );

新建一个Java web工程 SSM,目录结构如下

下面挨个看下配置文件

conf.properties配置详情

  1. url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
  2. driver=com.mysql.jdbc.Driver
  3. username=root
  4. password=root

log4j.properties配置详情

  1. log4j.rootLogger=info, live, file
  2. log4j.logger.org.springframework=ERROR
  3. —log4j.logger.org.logicalcobwebs.proxool=ERROR
  4. log4j.appender.live=org.apache.log4j.ConsoleAppender
  5. log4j.appender.live.Target=System.out
  6. log4j.appender.live.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.live.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss}(%r)%n--> [%t] %l: %m %x %n
  8. log4j.appender.file.File=/log/liveApi.log
  9. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
  11. log4j.appender.file.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss}(%r)%n--> [%t] %l: %m %x %n
  12. log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
  13. log4j.logger.com.ssm.dao=DEBUG

web.xml配置详情

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <display-name>SSM</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. <welcome-file>index.htm</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. <welcome-file>default.html</welcome-file>
  12. <welcome-file>default.htm</welcome-file>
  13. <welcome-file>default.jsp</welcome-file>
  14. </welcome-file-list>
  15. <!-- 配置 Spring -->
  16. <context-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>/WEB-INF/applicationContext.xml</param-value>
  19. </context-param>
  20. <!-- 配置DispatchcerServlet -->
  21. <servlet>
  22. <servlet-name>springDispatcherServlet</servlet-name>
  23. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  24. <!-- 配置Spring mvc下的配置文件的位置和名称 -->
  25. <init-param>
  26. <param-name>contextConfigLocation</param-name>
  27. <param-value>/WEB-INF/springmvc.xml</param-value>
  28. </init-param>
  29.  
  30. <load-on-startup>1</load-on-startup>
  31. </servlet>
  32. <servlet-mapping>
  33. <servlet-name>springDispatcherServlet</servlet-name>
  34. <url-pattern>/</url-pattern>
  35. </servlet-mapping>
  36. <!-- spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题 -->
  37. <filter>
  38. <filter-name>encodingFilter</filter-name>
  39. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  40. <init-param>
  41. <param-name>encoding</param-name>
  42. <param-value>UTF-8</param-value>
  43. </init-param>
  44. <!-- force强制,促使 -->
  45. <init-param>
  46. <param-name>forceEncoding</param-name>
  47. <param-value>true</param-value>
  48. </init-param>
  49. </filter>
  50. <filter-mapping>
  51. <filter-name>encodingFilter</filter-name>
  52. <url-pattern>/</url-pattern>
  53. </filter-mapping>
  54. <!-- Spring监听器 -->
  55. <listener>
  56. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  57. </listener>
  58. <!-- 防止Spring内存溢出监听器 -->
  59. <listener>
  60. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  61. </listener>
  62. <!-- 异常页面捕获 -->
  63. <error-page>
  64. <exception-type>java.lang.Throwable</exception-type>
  65. <location>/500.html</location>
  66. </error-page>
  67. <error-page>
  68. <error-code>500</error-code>
  69. <location>/500.html</location>
  70. </error-page>
  71. <error-page>
  72. <error-code>404</error-code>
  73. <location>/404.html</location>
  74. </error-page>
  75. </web-app>

applicationContext.xml配置详情

  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:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
  16.  
  17. <!-- 配置自动扫描的包 -->
  18. <context:component-scan base-package="com.ssm">
  19. <!-- 制定扫包规则 ,扫描除去使用@Controller注解的JAVA类 -->
  20. <context:exclude-filter type="annotation"
  21. expression="org.springframework.stereotype.Controller" />
  22. </context:component-scan>
  23.  
  24. <!-- ===========================数据源配置=============================== -->
  25. <!-- 引入外部数据源配置信息 -->
  26. <bean
  27. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  28. <property name="locations">
  29. <value>classpath:conf.properties</value>
  30. </property>
  31. </bean>
  32.  
  33. <!-- 配置数据源 -->
  34. <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  35. init-method="init" destroy-method="close">
  36. <property name="driverClassName" value="${driver}" />
  37. <property name="url" value="${url}" />
  38. <property name="username" value="${username}" />
  39. <property name="password" value="${password}" />
  40. <!-- 初始化连接大小 -->
  41. <property name="initialSize" value="0" />
  42. <!-- 连接池最大使用连接数量 -->
  43. <property name="maxActive" value="20" />
  44. <!-- 连接池最小空闲 -->
  45. <property name="minIdle" value="0" />
  46. <!-- 获取连接最大等待时间 -->
  47. <property name="maxWait" value="60000" />
  48.  
  49. <property name="validationQuery">
  50. <value>SELECT 1</value>
  51. </property>
  52. <property name="testOnBorrow" value="false" />
  53. <property name="testOnReturn" value="false" />
  54. <property name="testWhileIdle" value="true" />
  55.  
  56. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  57. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  58. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  59. <property name="minEvictableIdleTimeMillis" value="25200000" />
  60.  
  61. <!-- 打开removeAbandoned功能 -->
  62. <property name="removeAbandoned" value="true" />
  63. <!-- 1800秒,也就是30分钟 -->
  64. <property name="removeAbandonedTimeout" value="1800" />
  65. <!-- 关闭abanded连接时输出错误日志 -->
  66. <property name="logAbandoned" value="true" />
  67.  
  68. <!-- 监控数据库 -->
  69. <!-- <property name="filters" value="stat" /> -->
  70. <property name="filters" value="mergeStat" />
  71. </bean>
  72. <!-- 配置MyBatis session工厂 -->
  73. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  74. <property name="dataSource" ref="dataSource" />
  75. <!-- 自动扫描mapping.xml文件 -->
  76. <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
  77. </bean>
  78.  
  79. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  80. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  81. <property name="basePackage" value="com.ssm.dao" />
  82. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  83. </bean>
  84.  
  85. <!-- (事务管理)transaction manager -->
  86. <bean id="transactionManager"
  87. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  88. <property name="dataSource" ref="dataSource" />
  89. </bean>
  90.  
  91. <!-- 注解方式配置事物 -->
  92. <tx:annotation-driven transaction-manager="transactionManager" />
  93.  
  94. </beans>

springmvc.xml配置详情

  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:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:task="http://www.springframework.org/schema/task"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  11. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
  12. ">
  13. <!-- ====================================================== -->
  14. <!-- 配置@ResponseBody 保证返回值为UTF-8 -->
  15. <!-- 因为StringHttpMessageConverter默认是ISO8859-1 -->
  16. <!-- 用于使用@ResponseBody后返回中文避免乱码 -->
  17. <bean id="utf8Charset" class="java.nio.charset.Charset"
  18. factory-method="forName">
  19. <constructor-arg value="UTF-8" />
  20. </bean>
  21.  
  22. <!-- 配置自动扫描的包 -->
  23. <context:component-scan base-package="com.ssm">
  24. <!-- 制定扫包规则 ,只扫描使用@Controller注解的JAVA类 -->
  25. <context:include-filter type="annotation"
  26. expression="org.springframework.stereotype.Controller" />
  27. </context:component-scan>
  28.  
  29. <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
  30. <mvc:annotation-driven>
  31. <mvc:message-converters register-defaults="true">
  32. <!-- 配置Fastjson支持 -->
  33. <bean
  34. class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  35. <property name="charset" value="UTF-8" />
  36. <property name="supportedMediaTypes">
  37. <list>
  38. <value>text/html;charset=UTF-8</value>
  39. <value>application/json</value>
  40. <value>application/xml;charset=UTF-8</value>
  41. </list>
  42. </property>
  43. <property name="features">
  44. <list>
  45. <!-- 输出key时是否使用双引号 -->
  46. <value>QuoteFieldNames</value>
  47. <!-- 是否输出值为null的字段 -->
  48. <!-- <value>WriteMapNullValue</value> -->
  49. <!-- 数值字段如果为null,输出为0,而非null -->
  50. <value>WriteNullNumberAsZero</value>
  51. <!-- List字段如果为null,输出为[],而非null -->
  52. <value>WriteNullListAsEmpty</value>
  53. <!-- 字符类型字段如果为null,输出为"",而非null -->
  54. <value>WriteNullStringAsEmpty</value>
  55. <!-- Boolean字段如果为null,输出为false,而非null -->
  56. <value>WriteNullBooleanAsFalse</value>
  57. <!-- null String不输出 -->
  58. <value>WriteNullStringAsEmpty</value>
  59. <!-- null String也要输出 -->
  60. <!-- <value>WriteMapNullValue</value> -->
  61.  
  62. <!-- Date的日期转换器 -->
  63. <value>WriteDateUseDateFormat</value>
  64. </list>
  65. </property>
  66. </bean>
  67. </mvc:message-converters>
  68. </mvc:annotation-driven>
  69.  
  70. <!-- 文件上传配置 -->
  71. <bean id="multipartResolver"
  72. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  73. <property name="defaultEncoding">
  74. <value>UTF-8</value>
  75. </property>
  76. <property name="maxUploadSize">
  77. <!-- 100M 1024 * 1024 * 100 -->
  78. <value>104857600</value>
  79. </property>
  80. <property name="maxInMemorySize">
  81. <value>4096</value>
  82. </property>
  83. </bean>
  84. </beans>

==============顺便介绍一下数据库逆向生成工具(网上应该也有现成的)================================start

新建一个java 工程generatorSqlmapCustom,如下图所示

generatorConfig.xml配置详情

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5.  
  6. <generatorConfiguration>
  7. <context id="testTables" targetRuntime="MyBatis3">
  8. <commentGenerator>
  9. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  10. <property name="suppressAllComments" value="true" />
  11. </commentGenerator>
  12. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14. connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root"
  15. password="root">
  16. </jdbcConnection>
  17. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  18. NUMERIC 类型解析为java.math.BigDecimal -->
  19. <javaTypeResolver>
  20. <property name="forceBigDecimals" value="false" />
  21. </javaTypeResolver>
  22.  
  23. <!-- targetProject:生成PO类的位置 -->
  24. <javaModelGenerator targetPackage="com.ssm.pojo"
  25. targetProject=".\src">
  26. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  27. <property name="enableSubPackages" value="false" />
  28. <!-- 从数据库返回的值被清理前后的空格 -->
  29. <property name="trimStrings" value="true" />
  30. </javaModelGenerator>
  31. <!-- targetProject:mapper映射文件生成的位置 -->
  32. <sqlMapGenerator targetPackage="com.ssm.dao"
  33. targetProject=".\src">
  34. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  35. <property name="enableSubPackages" value="false" />
  36. </sqlMapGenerator>
  37. <!-- targetPackage:mapper接口生成的位置 -->
  38. <javaClientGenerator type="XMLMAPPER"
  39. targetPackage="com.ssm.dao"
  40. targetProject=".\src">
  41. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  42. <property name="enableSubPackages" value="false" />
  43. </javaClientGenerator>
  44. <!-- 指定数据库表 -->
  45. <table schema="" tableName="user"></table>
  46. </context>
  47. </generatorConfiguration>

GeneratorSqlmap.java代码

  1. public void generator() throws Exception{
  2.  
  3. List<String> warnings = new ArrayList<String>();
  4. boolean overwrite = true;
  5. //指定 逆向工程配置文件
  6. File configFile = new File("generatorConfig.xml");
  7. ConfigurationParser cp = new ConfigurationParser(warnings);
  8. Configuration config = cp.parseConfiguration(configFile);
  9. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  10. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
  11. callback, warnings);
  12. myBatisGenerator.generate(null);
  13.  
  14. }
  15. public static void main(String[] args) throws Exception {
  16. try {
  17. GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
  18. generatorSqlmap.generator();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22.  
  23. }

执行main函数 生成对应的映射文件,然后刷新项目

==============顺便介绍一下数据库逆向生成工具================================end

将逆向生成的文件拷贝到对应的文件夹

如果不想使用逆向工程,手动配置一下也可以

User.java

  1. package com.ssm.pojo;
  2.  
  3. public class User {
  4. private Integer id;
  5.  
  6. private String sex;
  7.  
  8. private String name;
  9.  
  10. public Integer getId() {
  11. return id;
  12. }
  13.  
  14. public void setId(Integer id) {
  15. this.id = id;
  16. }
  17.  
  18. public String getSex() {
  19. return sex;
  20. }
  21.  
  22. public void setSex(String sex) {
  23. this.sex = sex == null ? null : sex.trim();
  24. }
  25.  
  26. public String getName() {
  27. return name;
  28. }
  29.  
  30. public void setName(String name) {
  31. this.name = name == null ? null : name.trim();
  32. }
  33. }

UserExample.java其实这个类可以没有,这个类主要就是方便配置sql查询条件

  1. package com.ssm.pojo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class UserExample {
  7. protected String orderByClause;
  8.  
  9. protected boolean distinct;
  10.  
  11. protected List<Criteria> oredCriteria;
  12.  
  13. public UserExample() {
  14. oredCriteria = new ArrayList<Criteria>();
  15. }
  16.  
  17. public void setOrderByClause(String orderByClause) {
  18. this.orderByClause = orderByClause;
  19. }
  20.  
  21. public String getOrderByClause() {
  22. return orderByClause;
  23. }
  24.  
  25. public void setDistinct(boolean distinct) {
  26. this.distinct = distinct;
  27. }
  28.  
  29. public boolean isDistinct() {
  30. return distinct;
  31. }
  32.  
  33. public List<Criteria> getOredCriteria() {
  34. return oredCriteria;
  35. }
  36.  
  37. public void or(Criteria criteria) {
  38. oredCriteria.add(criteria);
  39. }
  40.  
  41. public Criteria or() {
  42. Criteria criteria = createCriteriaInternal();
  43. oredCriteria.add(criteria);
  44. return criteria;
  45. }
  46.  
  47. public Criteria createCriteria() {
  48. Criteria criteria = createCriteriaInternal();
  49. if (oredCriteria.size() == 0) {
  50. oredCriteria.add(criteria);
  51. }
  52. return criteria;
  53. }
  54.  
  55. protected Criteria createCriteriaInternal() {
  56. Criteria criteria = new Criteria();
  57. return criteria;
  58. }
  59.  
  60. public void clear() {
  61. oredCriteria.clear();
  62. orderByClause = null;
  63. distinct = false;
  64. }
  65.  
  66. protected abstract static class GeneratedCriteria {
  67. protected List<Criterion> criteria;
  68.  
  69. protected GeneratedCriteria() {
  70. super();
  71. criteria = new ArrayList<Criterion>();
  72. }
  73.  
  74. public boolean isValid() {
  75. return criteria.size() > 0;
  76. }
  77.  
  78. public List<Criterion> getAllCriteria() {
  79. return criteria;
  80. }
  81.  
  82. public List<Criterion> getCriteria() {
  83. return criteria;
  84. }
  85.  
  86. protected void addCriterion(String condition) {
  87. if (condition == null) {
  88. throw new RuntimeException("Value for condition cannot be null");
  89. }
  90. criteria.add(new Criterion(condition));
  91. }
  92.  
  93. protected void addCriterion(String condition, Object value, String property) {
  94. if (value == null) {
  95. throw new RuntimeException("Value for " + property + " cannot be null");
  96. }
  97. criteria.add(new Criterion(condition, value));
  98. }
  99.  
  100. protected void addCriterion(String condition, Object value1, Object value2, String property) {
  101. if (value1 == null || value2 == null) {
  102. throw new RuntimeException("Between values for " + property + " cannot be null");
  103. }
  104. criteria.add(new Criterion(condition, value1, value2));
  105. }
  106.  
  107. public Criteria andIdIsNull() {
  108. addCriterion("id is null");
  109. return (Criteria) this;
  110. }
  111.  
  112. public Criteria andIdIsNotNull() {
  113. addCriterion("id is not null");
  114. return (Criteria) this;
  115. }
  116.  
  117. public Criteria andIdEqualTo(Integer value) {
  118. addCriterion("id =", value, "id");
  119. return (Criteria) this;
  120. }
  121.  
  122. public Criteria andIdNotEqualTo(Integer value) {
  123. addCriterion("id <>", value, "id");
  124. return (Criteria) this;
  125. }
  126.  
  127. public Criteria andIdGreaterThan(Integer value) {
  128. addCriterion("id >", value, "id");
  129. return (Criteria) this;
  130. }
  131.  
  132. public Criteria andIdGreaterThanOrEqualTo(Integer value) {
  133. addCriterion("id >=", value, "id");
  134. return (Criteria) this;
  135. }
  136.  
  137. public Criteria andIdLessThan(Integer value) {
  138. addCriterion("id <", value, "id");
  139. return (Criteria) this;
  140. }
  141.  
  142. public Criteria andIdLessThanOrEqualTo(Integer value) {
  143. addCriterion("id <=", value, "id");
  144. return (Criteria) this;
  145. }
  146.  
  147. public Criteria andIdIn(List<Integer> values) {
  148. addCriterion("id in", values, "id");
  149. return (Criteria) this;
  150. }
  151.  
  152. public Criteria andIdNotIn(List<Integer> values) {
  153. addCriterion("id not in", values, "id");
  154. return (Criteria) this;
  155. }
  156.  
  157. public Criteria andIdBetween(Integer value1, Integer value2) {
  158. addCriterion("id between", value1, value2, "id");
  159. return (Criteria) this;
  160. }
  161.  
  162. public Criteria andIdNotBetween(Integer value1, Integer value2) {
  163. addCriterion("id not between", value1, value2, "id");
  164. return (Criteria) this;
  165. }
  166.  
  167. public Criteria andSexIsNull() {
  168. addCriterion("sex is null");
  169. return (Criteria) this;
  170. }
  171.  
  172. public Criteria andSexIsNotNull() {
  173. addCriterion("sex is not null");
  174. return (Criteria) this;
  175. }
  176.  
  177. public Criteria andSexEqualTo(String value) {
  178. addCriterion("sex =", value, "sex");
  179. return (Criteria) this;
  180. }
  181.  
  182. public Criteria andSexNotEqualTo(String value) {
  183. addCriterion("sex <>", value, "sex");
  184. return (Criteria) this;
  185. }
  186.  
  187. public Criteria andSexGreaterThan(String value) {
  188. addCriterion("sex >", value, "sex");
  189. return (Criteria) this;
  190. }
  191.  
  192. public Criteria andSexGreaterThanOrEqualTo(String value) {
  193. addCriterion("sex >=", value, "sex");
  194. return (Criteria) this;
  195. }
  196.  
  197. public Criteria andSexLessThan(String value) {
  198. addCriterion("sex <", value, "sex");
  199. return (Criteria) this;
  200. }
  201.  
  202. public Criteria andSexLessThanOrEqualTo(String value) {
  203. addCriterion("sex <=", value, "sex");
  204. return (Criteria) this;
  205. }
  206.  
  207. public Criteria andSexLike(String value) {
  208. addCriterion("sex like", value, "sex");
  209. return (Criteria) this;
  210. }
  211.  
  212. public Criteria andSexNotLike(String value) {
  213. addCriterion("sex not like", value, "sex");
  214. return (Criteria) this;
  215. }
  216.  
  217. public Criteria andSexIn(List<String> values) {
  218. addCriterion("sex in", values, "sex");
  219. return (Criteria) this;
  220. }
  221.  
  222. public Criteria andSexNotIn(List<String> values) {
  223. addCriterion("sex not in", values, "sex");
  224. return (Criteria) this;
  225. }
  226.  
  227. public Criteria andSexBetween(String value1, String value2) {
  228. addCriterion("sex between", value1, value2, "sex");
  229. return (Criteria) this;
  230. }
  231.  
  232. public Criteria andSexNotBetween(String value1, String value2) {
  233. addCriterion("sex not between", value1, value2, "sex");
  234. return (Criteria) this;
  235. }
  236.  
  237. public Criteria andNameIsNull() {
  238. addCriterion("name is null");
  239. return (Criteria) this;
  240. }
  241.  
  242. public Criteria andNameIsNotNull() {
  243. addCriterion("name is not null");
  244. return (Criteria) this;
  245. }
  246.  
  247. public Criteria andNameEqualTo(String value) {
  248. addCriterion("name =", value, "name");
  249. return (Criteria) this;
  250. }
  251.  
  252. public Criteria andNameNotEqualTo(String value) {
  253. addCriterion("name <>", value, "name");
  254. return (Criteria) this;
  255. }
  256.  
  257. public Criteria andNameGreaterThan(String value) {
  258. addCriterion("name >", value, "name");
  259. return (Criteria) this;
  260. }
  261.  
  262. public Criteria andNameGreaterThanOrEqualTo(String value) {
  263. addCriterion("name >=", value, "name");
  264. return (Criteria) this;
  265. }
  266.  
  267. public Criteria andNameLessThan(String value) {
  268. addCriterion("name <", value, "name");
  269. return (Criteria) this;
  270. }
  271.  
  272. public Criteria andNameLessThanOrEqualTo(String value) {
  273. addCriterion("name <=", value, "name");
  274. return (Criteria) this;
  275. }
  276.  
  277. public Criteria andNameLike(String value) {
  278. addCriterion("name like", value, "name");
  279. return (Criteria) this;
  280. }
  281.  
  282. public Criteria andNameNotLike(String value) {
  283. addCriterion("name not like", value, "name");
  284. return (Criteria) this;
  285. }
  286.  
  287. public Criteria andNameIn(List<String> values) {
  288. addCriterion("name in", values, "name");
  289. return (Criteria) this;
  290. }
  291.  
  292. public Criteria andNameNotIn(List<String> values) {
  293. addCriterion("name not in", values, "name");
  294. return (Criteria) this;
  295. }
  296.  
  297. public Criteria andNameBetween(String value1, String value2) {
  298. addCriterion("name between", value1, value2, "name");
  299. return (Criteria) this;
  300. }
  301.  
  302. public Criteria andNameNotBetween(String value1, String value2) {
  303. addCriterion("name not between", value1, value2, "name");
  304. return (Criteria) this;
  305. }
  306. }
  307.  
  308. public static class Criteria extends GeneratedCriteria {
  309.  
  310. protected Criteria() {
  311. super();
  312. }
  313. }
  314.  
  315. public static class Criterion {
  316. private String condition;
  317.  
  318. private Object value;
  319.  
  320. private Object secondValue;
  321.  
  322. private boolean noValue;
  323.  
  324. private boolean singleValue;
  325.  
  326. private boolean betweenValue;
  327.  
  328. private boolean listValue;
  329.  
  330. private String typeHandler;
  331.  
  332. public String getCondition() {
  333. return condition;
  334. }
  335.  
  336. public Object getValue() {
  337. return value;
  338. }
  339.  
  340. public Object getSecondValue() {
  341. return secondValue;
  342. }
  343.  
  344. public boolean isNoValue() {
  345. return noValue;
  346. }
  347.  
  348. public boolean isSingleValue() {
  349. return singleValue;
  350. }
  351.  
  352. public boolean isBetweenValue() {
  353. return betweenValue;
  354. }
  355.  
  356. public boolean isListValue() {
  357. return listValue;
  358. }
  359.  
  360. public String getTypeHandler() {
  361. return typeHandler;
  362. }
  363.  
  364. protected Criterion(String condition) {
  365. super();
  366. this.condition = condition;
  367. this.typeHandler = null;
  368. this.noValue = true;
  369. }
  370.  
  371. protected Criterion(String condition, Object value, String typeHandler) {
  372. super();
  373. this.condition = condition;
  374. this.value = value;
  375. this.typeHandler = typeHandler;
  376. if (value instanceof List<?>) {
  377. this.listValue = true;
  378. } else {
  379. this.singleValue = true;
  380. }
  381. }
  382.  
  383. protected Criterion(String condition, Object value) {
  384. this(condition, value, null);
  385. }
  386.  
  387. protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
  388. super();
  389. this.condition = condition;
  390. this.value = value;
  391. this.secondValue = secondValue;
  392. this.typeHandler = typeHandler;
  393. this.betweenValue = true;
  394. }
  395.  
  396. protected Criterion(String condition, Object value, Object secondValue) {
  397. this(condition, value, secondValue, null);
  398. }
  399. }
  400. }
  1. UserMapper.java
  1. package com.ssm.dao;
  2. import com.ssm.pojo.User;
  3. import com.ssm.pojo.UserExample;
  4. import java.util.List;
  5. import org.apache.ibatis.annotations.Param;
  6. public interface UserMapper {
  7. int countByExample(UserExample example);
  8.  
  9. int deleteByExample(UserExample example);
  10.  
  11. int deleteByPrimaryKey(Integer id);
  12.  
  13. int insert(User record);
  14.  
  15. int insertSelective(User record);
  16.  
  17. List<User> selectByExample(UserExample example);
  18.  
  19. User selectByPrimaryKey(Integer id);
  20.  
  21. int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
  22.  
  23. int updateByExample(@Param("record") User record, @Param("example") UserExample example);
  24.  
  25. int updateByPrimaryKeySelective(User record);
  26.  
  27. int updateByPrimaryKey(User record);
  28. }

UserMapper.xml (注意标红的地方,主键已经在数据库中设置为自动递增,所以插入sql得手动配一下)

  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.ssm.dao.UserMapper" >
  4. <resultMap id="BaseResultMap" type="com.ssm.pojo.User" >
  5. <id column="id" property="id" jdbcType="INTEGER" />
  6. <result column="sex" property="sex" jdbcType="VARCHAR" />
  7. <result column="name" property="name" jdbcType="VARCHAR" />
  8. </resultMap>
  9. <sql id="Example_Where_Clause" >
  10. <where >
  11. <foreach collection="oredCriteria" item="criteria" separator="or" >
  12. <if test="criteria.valid" >
  13. <trim prefix="(" suffix=")" prefixOverrides="and" >
  14. <foreach collection="criteria.criteria" item="criterion" >
  15. <choose >
  16. <when test="criterion.noValue" >
  17. and ${criterion.condition}
  18. </when>
  19. <when test="criterion.singleValue" >
  20. and ${criterion.condition} #{criterion.value}
  21. </when>
  22. <when test="criterion.betweenValue" >
  23. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  24. </when>
  25. <when test="criterion.listValue" >
  26. and ${criterion.condition}
  27. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  28. #{listItem}
  29. </foreach>
  30. </when>
  31. </choose>
  32. </foreach>
  33. </trim>
  34. </if>
  35. </foreach>
  36. </where>
  37. </sql>
  38. <sql id="Update_By_Example_Where_Clause" >
  39. <where >
  40. <foreach collection="example.oredCriteria" item="criteria" separator="or" >
  41. <if test="criteria.valid" >
  42. <trim prefix="(" suffix=")" prefixOverrides="and" >
  43. <foreach collection="criteria.criteria" item="criterion" >
  44. <choose >
  45. <when test="criterion.noValue" >
  46. and ${criterion.condition}
  47. </when>
  48. <when test="criterion.singleValue" >
  49. and ${criterion.condition} #{criterion.value}
  50. </when>
  51. <when test="criterion.betweenValue" >
  52. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  53. </when>
  54. <when test="criterion.listValue" >
  55. and ${criterion.condition}
  56. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  57. #{listItem}
  58. </foreach>
  59. </when>
  60. </choose>
  61. </foreach>
  62. </trim>
  63. </if>
  64. </foreach>
  65. </where>
  66. </sql>
  67. <sql id="Base_Column_List" >
  68. id, sex, name
  69. </sql>
  70. <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.ssm.pojo.UserExample" >
  71. select
  72. <if test="distinct" >
  73. distinct
  74. </if>
  75. <include refid="Base_Column_List" />
  76. from user
  77. <if test="_parameter != null" >
  78. <include refid="Example_Where_Clause" />
  79. </if>
  80. <if test="orderByClause != null" >
  81. order by ${orderByClause}
  82. </if>
  83. </select>
  84. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  85. select
  86. <include refid="Base_Column_List" />
  87. from user
  88. where id = #{id,jdbcType=INTEGER}
  89. </select>
  90. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  91. delete from user
  92. where id = #{id,jdbcType=INTEGER}
  93. </delete>
  94. <delete id="deleteByExample" parameterType="com.ssm.pojo.UserExample" >
  95. delete from user
  96. <if test="_parameter != null" >
  97. <include refid="Example_Where_Clause" />
  98. </if>
  99. </delete>
  100. <insert id="insert" parameterType="com.ssm.pojo.User" useGeneratedKeys="true" keyProperty="id">
  101. insert into user (sex, name
  102. )
  103. values (#{sex,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}
  104. )
  105. </insert>
  106. <insert id="insertSelective" parameterType="com.ssm.pojo.User" >
  107. insert into user
  108. <trim prefix="(" suffix=")" suffixOverrides="," >
  109. <if test="id != null" >
  110. id,
  111. </if>
  112. <if test="sex != null" >
  113. sex,
  114. </if>
  115. <if test="name != null" >
  116. name,
  117. </if>
  118. </trim>
  119. <trim prefix="values (" suffix=")" suffixOverrides="," >
  120. <if test="id != null" >
  121. #{id,jdbcType=INTEGER},
  122. </if>
  123. <if test="sex != null" >
  124. #{sex,jdbcType=VARCHAR},
  125. </if>
  126. <if test="name != null" >
  127. #{name,jdbcType=VARCHAR},
  128. </if>
  129. </trim>
  130. </insert>
  131. <select id="countByExample" parameterType="com.ssm.pojo.UserExample" resultType="java.lang.Integer" >
  132. select count(*) from user
  133. <if test="_parameter != null" >
  134. <include refid="Example_Where_Clause" />
  135. </if>
  136. </select>
  137. <update id="updateByExampleSelective" parameterType="map" >
  138. update user
  139. <set >
  140. <if test="record.id != null" >
  141. id = #{record.id,jdbcType=INTEGER},
  142. </if>
  143. <if test="record.sex != null" >
  144. sex = #{record.sex,jdbcType=VARCHAR},
  145. </if>
  146. <if test="record.name != null" >
  147. name = #{record.name,jdbcType=VARCHAR},
  148. </if>
  149. </set>
  150. <if test="_parameter != null" >
  151. <include refid="Update_By_Example_Where_Clause" />
  152. </if>
  153. </update>
  154. <update id="updateByExample" parameterType="map" >
  155. update user
  156. set id = #{record.id,jdbcType=INTEGER},
  157. sex = #{record.sex,jdbcType=VARCHAR},
  158. name = #{record.name,jdbcType=VARCHAR}
  159. <if test="_parameter != null" >
  160. <include refid="Update_By_Example_Where_Clause" />
  161. </if>
  162. </update>
  163. <update id="updateByPrimaryKeySelective" parameterType="com.ssm.pojo.User" >
  164. update user
  165. <set >
  166. <if test="sex != null" >
  167. sex = #{sex,jdbcType=VARCHAR},
  168. </if>
  169. <if test="name != null" >
  170. name = #{name,jdbcType=VARCHAR},
  171. </if>
  172. </set>
  173. where id = #{id,jdbcType=INTEGER}
  174. </update>
  175. <update id="updateByPrimaryKey" parameterType="com.ssm.pojo.User" >
  176. update user
  177. set sex = #{sex,jdbcType=VARCHAR},
  178. name = #{name,jdbcType=VARCHAR}
  179. where id = #{id,jdbcType=INTEGER}
  180. </update>
  181. </mapper>

  

新建一个vo类,UserVo.java

  1. package com.ssm.vo;
  2.  
  3. public class UserVo {
  4. private String id;
  5.  
  6.   private String sex;
  7.  
  8.   private String name;
  9.  
  10. public String getId() {
  11. return id;
  12. }
  13.  
  14. public void setId(String id) {
  15. this.id = id;
  16. }
  17.  
  18. public String getSex() {
  19. return sex;
  20. }
  21.  
  22. public void setSex(String sex) {
  23. this.sex = sex;
  24. }
  25.  
  26. public String getName() {
  27. return name;
  28. }
  29.  
  30. public void setName(String name) {
  31. this.name = name;
  32. }
  33.  
  34. }

  

新建一个service接口,IUserService.java

  1. package com.ssm.service;
    import java.util.Map;
    import com.ssm.vo.UserVo;
  2. public interface IUserService {
  3.  
  4. public Map<String,String> onSave(UserVo userVo);
  5. }

实现IUserService接口,UserServiceImpl.java

  1. package com.ssm.service.impl;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import com.ssm.dao.UserMapper;
  10. import com.ssm.pojo.User;
  11. import com.ssm.service.IUserService;
  12. import com.ssm.vo.UserVo;
  13.  
  14. @Service
  15. public class UserServiceImpl implements IUserService{
  16.  
  17. @Autowired
  18. private UserMapper userMapper;
  19.  
  20. @Override
  21. public Map<String, String> onSave(UserVo userVo) {
  22. Map<String, String> map = new HashMap<String,String>();
  23. try {
  24. User user = new User();
  25. user.setName(userVo.getName());
  26. user.setSex(userVo.getSex());
  27. int i = userMapper.insert(user);
  28. if(i > 0){
  29. map.put("status", "Y");
  30. map.put("message", "新增成功");
  31. }
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. return map;
  36. }
  37.  
  38. }

 

新建一个control类,UserController.java

  1. package com.ssm.controller;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10.  
  11. import com.ssm.service.IUserService;
  12. import com.ssm.vo.UserVo;
  13.  
  14. @RequestMapping("/user")
  15. @Controller
  16. public class UserController {
  17. @Autowired
  18. private IUserService userService;
  19.  
  20. @RequestMapping("/onSave")
  21. @ResponseBody
  22. public Map<String,String> onSave(UserVo userVo){
  23. Map<String,String> map = new HashMap<String,String>();
  24. try {
  25. map = userService.onSave(userVo);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. return map;
  30. }
  31. }

在WebContent目录下,新增static目录,如下图所示:

新增index.jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <base href="${pageContext.request.contextPath}/static/"></base>
  7. <meta charset="utf-8" />
  8. <script src="js/jquery-1.12.4.min.js"></script>
  9.  
  10. <script type="text/javascript">
  11. $(document).ready(function() {
  12. onSave();
  13. });
  14.  
  15. function onSave() {
  16. $.ajax({
  17. type : "post",
  18. url : "${pageContext.request.contextPath}/user/onSave",
  19. async : false,
  20. timeout : 30000,
  21. data : {
  22. "sex":"男",
  23. "name":"张三"
  24. },
  25. dataType : 'json',
  26. beforeSend : function(XMLHttpRequest) {
  27.  
  28. },
  29. success : function(result) {
  30. alert(result.message)
  31. },
  32. error : function() {
  33. }
  34. });
  35. }
  36.  
  37. </script>
  38. </head>
  39.  
  40. <body>
  41. 欢迎您!!!!!!!!!!!!!!!!
  42. </body>
  43.  
  44. </html>

在浏览器地址栏,输入http://127.0.0.1:8080/SSM/index.jsp,回车,结果如下图所示

看下数据库表

eclipse搭建ssm框架的更多相关文章

  1. [框架]eclipse搭建ssm框架 一 标签: eclipsetomcat框架 2017-03-25 21:28 1085人阅读 评论(

    虽然现在也做过一些项目,但是自己从头搭起来的框架几乎没有,所以这两天自己搭了一下ssm的框架,下面写一下框架的搭建过程.并且给出增删改查四条线来方便大家熟悉代码. 环境准备 maven3.2.3 ec ...

  2. 使用Eclipse搭建SSM框架(Spring + Spring MVC + Mybatis)

    1.创建项目 1)打开Eclipse,点击File --> New --> Other 2)输入maven,找到Maven Project 3)然后一直按Next,直到出现一下界面: 4) ...

  3. eclipse搭建ssm框架的maven的工程

    版本:eclipse:Indigo Service Release 2.  jdk :jdk1.7.0_03. maven:apache-maven-3.3.3 . 上面的3个东西 先下载下来.然后运 ...

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

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

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

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

  6. maven/eclipse搭建ssm(spring+spring mvc+mybatis)

    maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实 ...

  7. 快速搭建ssm框架

    快速搭建SSM框架 因为最近有很多朋友问我自己的项目搭建的不够完善,并且经常出现一些小问题,那么今天我又整理了一下文档教大家如何快速搭建SSM框架我是用 eclipse搭建的,如果想用idear的话我 ...

  8. 利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)

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

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

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

随机推荐

  1. linux下逻辑卷管理 调整分区大小

    [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50 ...

  2. appium自动化测试 环境搭建

    最 近接手的项目是移动端自动化测试 ,需要用的appium ,头一回使用, 项目特点:1)数据有时效性,需要在短时间内验证大量数据, 2) 人工去一个一个核对发现不了太多BUG. 环境搭建:参考虫师的 ...

  3. 基于 Spring Cloud 完整的微服务架构实战

    本项目是一个基于 Spring Boot.Spring Cloud.Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目. @作者:Sheldon地址:ht ...

  4. WPF程序开发方法小总结

    1.先做静态界面(静态界面带有 示例 数据---> 展示布局效果) 2.然后在VM写完之后,再对静态界面绑定数据源属性

  5. 20180831xlVBA_WorkbooksCosolidate

    Sub WorkbooksConsolidate() Rem 设置求和区域为 sheet名称/单元格区域;sheet名称/单元格区域 Const Setting As String = "S ...

  6. 圆的变化(自定义动画,及自定义UI)

    之前在面试的时候被问到过一个问题,如何实现一个圆沿着一条线由大到小 当时回答的含糊不清,现在已经明白怎么去实现 关键点:Paint,path,canvas 一种方法 在activity中去控制圆的x, ...

  7. 58Ajax

    Ajax 1 .客户端浏览器通过执行一段JS代码向服务器发送请求,服务器路由对应的视图函数返回一个json字符串作为响应,    浏览器接受响应后会触发该ajax请求的回调函数success,参数为响 ...

  8. Django中模型层中ORM的多表操作

    ORM的多表创建(一对一.一对多,多对多): 1模型创建 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等 ...

  9. 6月4 Smarty练习增删改

    练习Smarty的增删改所需要用到的数据库名称:timu,xuanxiang,kemu,nandu,leixing,然后使用smarty模板将前端后后台分割开来: 主页后台页面:zhupm.php & ...

  10. sqlserver创建计算列 转

    转 http://www.cnblogs.com/lgx5/p/6017874.html 表中其它列的计算值 创建的sql create table table1 ( number decimal(1 ...