ssm:SpringMVC+Spring+MyBatis

CRUD:Create(创建)
Retrieve(查询)
Update(更新)
Delete(删除)

功能点

•1、分页
•2、数据校验
•jquery前端校验+JSR303后端校验
•3、ajax
•4、Rest风格的URI;使用HTTP协议请求方式的动词,来表示对资源的操作(GET(查询),POST(新增),PUT(修改),DELETE(删除))

技术点

•基础框架-ssm(SpringMVC+Spring+MyBatis)
•数据库-MySQL
•前端框架-bootstrap快速搭建简洁美观的界面
•项目的依赖管理-Maven
•分页-pagehelper
•逆向工程-MyBatis Generator

基础环境搭建

• 1、创建一个maven工程
• 2、引入项目依赖的jar包
• spring
• springmvc
• mybatis
• 数据库连接池,驱动包
• 其他(jstl,servlet-api,junit)

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.11</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-webmvc</artifactId>
  11. <version>4.3.7.RELEASE</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-jdbc</artifactId>
  17. <version>4.3.7.RELEASE</version>
  18. </dependency>
  19. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-aspects</artifactId>
  23. <version>4.3.7.RELEASE</version>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  26. <dependency>
  27. <groupId>org.mybatis</groupId>
  28. <artifactId>mybatis</artifactId>
  29. <version>3.4.2</version>
  30. </dependency>
  31. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
  32. <dependency>
  33. <groupId>org.mybatis</groupId>
  34. <artifactId>mybatis-spring</artifactId>
  35. <version>2.0.6</version>
  36. </dependency>
  37. <!--数据库连接池-->
  38. <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
  39. <dependency>
  40. <groupId>c3p0</groupId>
  41. <artifactId>c3p0</artifactId>
  42. <version>0.9.1.2</version>
  43. </dependency>
  44. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  45. <dependency>
  46. <groupId>mysql</groupId>
  47. <artifactId>mysql-connector-java</artifactId>
  48. <version>5.1.41</version>
  49. </dependency>
  50. <!--jstl servlet junit-->
  51. <!-- https://mvnrepository.com/artifact/jstl/jstl -->
  52. <dependency>
  53. <groupId>jstl</groupId>
  54. <artifactId>jstl</artifactId>
  55. <version>1.2</version>
  56. </dependency>
  57. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  58. <dependency>
  59. <groupId>javax.servlet</groupId>
  60. <artifactId>javax.servlet-api</artifactId>
  61. <version>4.0.1</version>
  62. <scope>provided</scope>
  63. </dependency>

• 3、引入bootstrap前端框架

  1. <%--引入jq--%>
  2. <script type="text/javascript" src="static/js/jquery-1.12.4.min.js"></script>
  3. <%--引入样式--%>
  4. <link href="static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
  5. <script src="static/bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript"></script>

• 4、编写ssm整合的关键配置文件
• web.xml,spring,springmvc,mybatis,使用mybatis的逆向工程生成对应的bean以及mapper
web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <!--1. 启动 spring 的容器-->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!--2.springMVC的前端控制器拦截所有请求-->
  15. <servlet>
  16. <servlet-name>dispatcherServlet</servlet-name>
  17. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  18. <load-on-startup>1</load-on-startup>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>dispatcherServlet</servlet-name>
  22. <url-pattern>/</url-pattern>
  23. </servlet-mapping>
  24. <!--3.字符编码过滤器 放在所有过滤器之前-->
  25. <filter>
  26. <filter-name>CharacterEncodingFilter</filter-name>
  27. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  28. <init-param>
  29. <param-name>encoding</param-name>
  30. <param-value>utf-8</param-value>
  31. </init-param>
  32. <init-param>
  33. <param-name>forceRequestEncoding</param-name>
  34. <param-value>true</param-value>
  35. </init-param>
  36. <init-param>
  37. <param-name>forceResponseEncoding</param-name>
  38. <param-value>utf-8</param-value>
  39. </init-param>
  40. </filter>
  41. <filter-mapping>
  42. <filter-name>CharacterEncodingFilter</filter-name>
  43. <url-pattern>/*</url-pattern>
  44. </filter-mapping>
  45. <!--4. rest 风格 uri 将普通请求转为put delete请求-->
  46. <filter>
  47. <filter-name>HiddenHttpMethodFilter</filter-name>
  48. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  49. </filter>
  50. <filter-mapping>
  51. <filter-name>HiddenHttpMethodFilter</filter-name>
  52. <url-pattern>/*</url-pattern>
  53. </filter-mapping>
  54. </web-app>

dispatcherServlet-servlet.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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
  6. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  11. <!--SpringMVC 跳转逻辑控制,配置-->
  12. <context:component-scan base-package="com.atguigu" use-default-filters="false">
  13. <!--只扫描控制器-->
  14. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  15. </context:component-scan>
  16. <!--配置视图解析器-->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/views/"></property>
  19. <property name="suffix" value=".jsp"></property>
  20. </bean>
  21. <!--标配-->
  22. <!--将mvc不能处理的请求交给tomcat-->
  23. <mvc:default-servlet-handler/>
  24. <!--mvc高级功能-->
  25. <mvc:annotation-driven/>
  26. </beans>

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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
  6. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  11. <context:component-scan base-package="com.atguigu">
  12. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  13. </context:component-scan>
  14. <!-- Spring 的配置文件 业务逻辑相关 -->
  15. <!-- 数据源 -->
  16. <!--properties文件引入-->
  17. <context:property-placeholder location="classpath:dbconfig.properties"/>
  18. <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  20. <property name="driverClass" value="${jdbc.driverClass}"></property>
  21. <property name="user" value="${jdbc.user}"></property>
  22. <property name="password" value="${jdbc.password}"></property>
  23. </bean>
  24. <!--配置mybatis整合-->
  25. <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="pooledDataSource"></property>
  27. <!-- 指定 mybatis 全局配置文件的位置-->
  28. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  29. <!--指定mapper文件的位置-->
  30. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  31. <!--别名处理器: -->
  32. <property name="typeAliasesPackage" value="com.atguigu.crud.bean"></property>
  33. </bean>
  34. <!--配置扫描器,将mybatis接口的实现加入到ioc容器中-->
  35. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  36. <!--扫描所有dao接口的实现加入到ioc容器-->
  37. <property name="basePackage" value="com.atguigu.crud.dao"></property>
  38. </bean>
  39. <!-- spring事务管理器 -->
  40. <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  41. <!--控制数据源-->
  42. <property name="dataSource" ref="pooledDataSource"></property>
  43. </bean>
  44. <!-- 基于注解的事务管理 -->
  45. <aop:config>
  46. <!--切入点表达式-->
  47. <aop:pointcut id="txPoint" expression="execution(* com.atguigu.crud.service..*(..))"/>
  48. <!--配置事务增强-->
  49. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
  50. </aop:config>
  51. <!--配置事务增强,如何切入-->
  52. <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
  53. <tx:attributes>
  54. <!--所有方法都是事务方法-->
  55. <tx:method name="*"/>
  56. <!--以get开头的所有方法-->
  57. <tx:method name="get*" read-only="true"/>
  58. </tx:attributes>
  59. </tx:advice>
  60. <!-- spring 配置文件的核心点:数据源 事务控制 mybatis-->
  61. </beans>

dbconfig.properties

  1. jdbc.jdbcUrl=jdbc:mysql:///3306/ssm_crud
  2. jdbc.driverClass=com.mysql.jdbc.Driver
  3. jdbc.user=root
  4. jdbc.password=000823

逆向工程

  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. <generatorConfiguration>
  6. <context id="DB2Tables" targetRuntime="MyBatis3">
  7. <commentGenerator>
  8. <property name="suppressAllComments" value="true"/>
  9. </commentGenerator>
  10. <!--配置数据库连接信息-->
  11. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  12. connectionURL="jdbc:mysql:///ssm_crud?useSSL=false"
  13. userId="root"
  14. password="000823">
  15. </jdbcConnection>
  16. <javaTypeResolver>
  17. <property name="forceBigDecimals" value="false"/>
  18. </javaTypeResolver>
  19. <!--指定javaBean位置-->
  20. <javaModelGenerator targetPackage="com.atguigu.crud.bean" targetProject=".\src\main\java">
  21. <property name="enableSubPackages" value="true"/>
  22. <property name="trimStrings" value="true"/>
  23. </javaModelGenerator>
  24. <!--指定映射文件-->
  25. <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
  26. <property name="enableSubPackages" value="true"/>
  27. </sqlMapGenerator>
  28. <!--指定dao接口位置-->
  29. <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.crud.dao" targetProject=".\src\main\java">
  30. <property name="enableSubPackages" value="true"/>
  31. </javaClientGenerator>
  32. <!--指定每个表的生成策略-->
  33. <table tableName="tbl_emp" domainObjectName="Employee">
  34. </table>
  35. <table tableName="tbl_dept" domainObjectName="Department">
  36. </table>
  37. </context>
  38. </generatorConfiguration>
  1. @Test
  2. public void t1() throws Exception{
  3. List<String> warnings = new ArrayList();
  4. boolean overwrite = true;
  5. File configFile = new File("mbg.xml");
  6. ConfigurationParser cp = new ConfigurationParser(warnings);
  7. Configuration config = cp.parseConfiguration(configFile);
  8. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  9. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  10. myBatisGenerator.generate((ProgressCallback)null);
  11. }

• 5、测试mapper

  1. <!-- 配置一个可以执行批量的sqlSession -->
  2. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  3. <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
  4. <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  5. </bean>
  1. @Autowired
  2. DepartmentMapper departmentMapper;
  3. @Autowired
  4. EmployeeMapper employeeMapper;
  5. @Autowired
  6. SqlSession sqlSession;
  7. /**
  8. * 测试departmentMapper
  9. */
  10. @Test
  11. public void t1(){
  12. ////1、创建springioc容器
  13. //ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
  14. ////2、获取mapper
  15. //DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);
  16. System.out.println(departmentMapper);
  17. // 1.插入部门
  18. //departmentMapper.insertSelective(new Department(null,"开发部"));
  19. //departmentMapper.insertSelective(new Department(null,"测试部"));
  20. // 2.插入员工
  21. // employeeMapper.insertSelective(new Employee(null,"小徐","1","3050106006@com",1));
  22. // 3.批量插入;使用可以执行批量操作的sqlSession
  23. // for (int i = 0; i < 1000; i++) {
  24. // employeeMapper.insertSelective(new Employee(null,"小徐","1","3050106006@com",1));
  25. // }
  26. EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
  27. for (int i = 0; i < 1000; i++) {
  28. String uid = UUID.randomUUID().toString().substring(0, 5) + ""+i;
  29. mapper.insertSelective(new Employee(null,uid,"M",uid+"@com",1));
  30. }
  31. }