配置之前现需要引入依赖的jar包:

*Spring

*SpringMvc

*Mybatis

*数据库连接池,驱动包

*其他(jstl,Servlet ,Junit..)

1.poxm.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.atguigu</groupId>
  4. <artifactId>ssm_crud</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <packaging>war</packaging>
  7. <dependencies>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>3.8.1</version>
  12. <scope>test</scope>
  13. </dependency>
  14. <!-- PageHelper -->
  15. <dependency>
  16. <groupId>com.github.pagehelper</groupId>
  17. <artifactId>pagehelper</artifactId>
  18. <version>5.0.0</version>
  19. </dependency>
  20. <dependency>
  21. <!-- MBG -->
  22. <groupId>org.mybatis.generator</groupId>
  23. <artifactId>mybatis-generator-core</artifactId>
  24. <version>1.3.5</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>log4j</groupId>
  28. <artifactId>log4j</artifactId>
  29. <version>1.2.16</version>
  30. <scope>compile</scope>
  31. </dependency>
  32. <!-- Spring jdbc -->
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-jdbc</artifactId>
  36. <version>4.3.7.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-webmvc</artifactId>
  41. <version>4.3.7.RELEASE</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-aspects</artifactId>
  46. <version>4.3.7.RELEASE</version>
  47. </dependency>
  48. <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-test</artifactId>
  52. <version>4.3.7.RELEASE</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.mybatis</groupId>
  56. <artifactId>mybatis</artifactId>
  57. <version>3.4.2</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.mybatis</groupId>
  61. <artifactId>mybatis-spring</artifactId>
  62. <version>1.3.1</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>c3p0</groupId>
  66. <artifactId>c3p0</artifactId>
  67. <version>0.9.1</version>
  68. </dependency>
  69. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  70. <dependency>
  71. <groupId>mysql</groupId>
  72. <artifactId>mysql-connector-java</artifactId>
  73. <version>5.1.6</version>
  74. </dependency>
  75.  
  76. <dependency>
  77. <groupId>jstl</groupId>
  78. <artifactId>jstl</artifactId>
  79. <version>1.2</version>
  80. </dependency>
  81. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  82. <dependency>
  83. <groupId>javax.servlet</groupId>
  84. <artifactId>javax.servlet-api</artifactId>
  85. <version>3.0.1</version>
  86. <scope>provided</scope>
  87. </dependency>
  88. </dependencies>
  89. </project>

2.本项目还使用了BootStrap前端框架,故到官网下载Bootstrap。在webapp目录下创建static文件保存我们的静态文件。同时将我们下载的Bootstrap解压到该目录下。

3.编写SSM整合的关键配置文件

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>Archetype Created Web Application</display-name>
  4. <!-- 启动Spring的容器 -->
  5. <!-- needed for ContextLoaderListener -->
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>
  10.  
  11. <!-- Bootstraps the root web application context before servlet initialization -->
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!-- SpringMVC的前端控制器 -->
  16. <!-- The front controller of this Spring Web application, responsible for
  17. handling all application requests -->
  18. <servlet>
  19. <servlet-name>dispatcherServlet</servlet-name>
  20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21. <load-on-startup>1</load-on-startup>
  22. </servlet>
  23.  
  24. <!-- Map all requests to the DispatcherServlet for handling -->
  25. <servlet-mapping>
  26. <servlet-name>dispatcherServlet</servlet-name>
  27. <url-pattern>/</url-pattern>
  28. </servlet-mapping>
  29. <!-- 字符编码过滤器 -->
  30. <filter>
  31. <filter-name>CharacterEncodingFilte</filter-name>
  32. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  33. <init-param>
  34. <param-name>encoding</param-name>
  35. <param-value>utf-8</param-value>
  36. </init-param>
  37. <init-param>
  38. <param-name>forceRequestEncoding</param-name>
  39. <param-value>true</param-value>
  40. </init-param>
  41. <init-param>
  42. <param-name>forceResponseEncoding</param-name>
  43. <param-value>true</param-value>
  44. </init-param>
  45.  
  46. </filter>
  47. <filter-mapping>
  48. <filter-name>CharacterEncodingFilte</filter-name>
  49. <url-pattern>/*</url-pattern>
  50. </filter-mapping>
  51. <!-- 使用Rest风格的URI 将页面post请求转化为指定的delete或者put -->
  52. <filter>
  53. <filter-name>HiddenHttpMethodFilter</filter-name>
  54. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  55. </filter>
  56. <filter-mapping>
  57. <filter-name>HiddenHttpMethodFilter</filter-name>
  58. <url-pattern>/*</url-pattern>
  59. </filter-mapping>
  60. <!-- 加载log4j配置文件 -->
  61. <context-param>
  62. <param-name>log4jConfigLocation</param-name>
  63. <param-value>classpath:log4j.properties</param-value>
  64. <!-- <param-value>/WEB-INF/log4j.xml</param-value> -->
  65. </context-param>
  66. <listener>
  67. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  68. </listener>
  69. </web-app>

4.在WEB-INF目录下创建dispatcherServlet-servlet.xml(spring mvc的配置文件)文件,,同时在java目录下创建我们需要的包,配置如下:

  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:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  7. http://www.springframework.org/schema/beans 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.  
  13. <!-- Spring mvc 的配置文件,包含网站跳转逻辑的控制、配置 -->
  14. <context:component-scan base-package="com.atguigu" use-default-filters="false">
  15. <!-- 只扫瞄控制器 -->
  16. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  17. </context:component-scan>
  18. <!-- 视图解析器,方便页面返回 -->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="/WEB-INF/views/"></property>
  21. <property name="suffix" value=".jsp"></property>
  22. </bean>
  23. <!-- 两个标准配置 -->
  24. <!-- 将Spring mvc不能处理的请求将给Tomcat -->
  25. <mvc:default-servlet-handler/>
  26. <!-- 能支持Spring MVC更高级的内容 如JSR303校验,快捷的ajax 。。。映射动态请求 -->
  27. <mvc:annotation-driven/>
  28. </beans>

5.创建applicationContext.xml(spring的配置文件),同时创建数据库的配置文件dbconfig.properties。

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!-- sping 的配置文件,这里主要配置和业务逻辑相关的 -->
  16. <!-- ====================================================数据源,事物控制,xxx -->
  17.  
  18. <context:component-scan base-package="com.atguigu">
  19. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  20. </context:component-scan>
  21. <context:property-placeholder location="classpath:dbconfig.properties"/>
  22. <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  23. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  24. <property name="driverClass" value="${jdbc.driverClass}"></property>
  25. <property name="user" value="${jdbc.user}"></property>
  26. <property name="password" value="${jdbc.password}"></property>
  27. </bean>
  28. <!--=============================================================== 配置和mybatis的整合 -->
  29. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <!-- 制定Mybatis全局配置文件的位置 -->
  31. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  32. <property name="dataSource" ref="pooledDataSource"></property>
  33. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  34. </bean>
  35. <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
  36. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  37. <!-- 扫瞄所有dao接口的实现,加入到ioc容器中 -->
  38. <property name="basePackage" value="com.atguigu.crud.dao"></property>
  39. </bean>
  40. <!-- 配置一个可以批量操作的sqlSession -->
  41. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  42. <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
  43. <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  44. </bean>
  45. <!-- ===================================================================== -->
  46. <!-- =======================================================================事物控制的配置 -->
  47. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  48. <!-- 控制住数据源 -->
  49. <property name="dataSource" ref="pooledDataSource"></property>
  50. </bean>
  51. <!-- 开启注解的事物,使用xml配置形式的事物(必要主要的使用配置式) -->
  52. <aop:config>
  53. <!-- 切入点表达式 -->
  54. <aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
  55. <!-- 配置事物增强 -->
  56. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
  57. </aop:config>
  58. <!-- 配置事物增强,事物如何切入 -->
  59.  
  60. <tx:advice id="txAdvice">
  61. <tx:attributes>
  62. <!-- 所有的方法都是事物方法 -->
  63. <tx:method name="*"/>
  64. <!-- 以get开始的所有方法都是查询 -->
  65. <tx:method name="get*" read-only="true"/>
  66. </tx:attributes>
  67. </tx:advice>
  68. <!-- Spring配置文件的核心点(数据源、与mybatis的整合、事物控制) -->
  69.  
  70. </beans>

dbconfig.properties

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

6.mybatis-config.xml文件,同时在resources目录下创建mapper文件夹。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
  8.  
  9. <setting name="logImpl" value="LOG4J"/>
  10. </settings>
  11.  
  12. <typeAliases>
  13. <package name="com.atguigu.crud.bean"/>
  14. </typeAliases>
  15. <plugins>
  16. <plugin interceptor="com.github.pagehelper.PageInterceptor">
  17. <property name="reasonable" value="true"/>
  18. </plugin>
  19. </plugins>
  20. </configuration>

至此基本的配置已经完成。

SSM_CRUD新手练习(2)配置文件的更多相关文章

  1. SSM_CRUD新手练习(3)创建数据库

    在上一节我们已经完成了基本的SSM配置,现在需要创建我们数据库. 我们需要两张表分别为tbl_emp(员工表)和tbl_dedpt(部门表).同时d_id是部门表对应dept_id的外键. 需要注意的 ...

  2. SSM_CRUD新手练习(9)显示分页数据

    我们已经做好了用来显示数据的分页模板,现在只需要将我们从后台取出的数据填充好,显示出来. 我们使用<c:forEach>标签循环取出数据,所以需要先导入JSTL标签库 <%@ tag ...

  3. SSM_CRUD新手练习(8)搭建BootStrap分页页面

      经过Spring单元测试模拟请求,我们能够成功的取出数据,接下来,我们就开始写用来显示查询数据的分页页面吧. 我们使用Bootstrap来帮助我们快速开发漂亮的页面,具体怎么用可以查看Bootst ...

  4. SSM_CRUD新手练习(7)Spring单元测试分页请求

    好久没写这个系列博客了是因为本人去公司实习去了,公司用的是Spring+SpingMvc+Hibernate现在有时间了不管怎么样继续把这个项目写完. 因为机器的原因,我的环境变成了IDEA+orac ...

  5. SSM_CRUD新手练习(6)分页后台控制器编写

    经过测试基础环境已经搭建好了,现在我们开始编写CRUD. 我们来看一下查询的逻辑该怎么写: 1.访问index.jsp页面 2.index.jsp页面发送查询员工的请求 3.EmployeeContr ...

  6. SSM_CRUD新手练习(5)测试mapper

    上一篇我们使用逆向工程生成了所需要的bean.dao和对应的mapper.xml文件,并且修改好了我们需要的数据库查询方法. 现在我们来测试一下DAO层,在test包下新建一个MapperTest.j ...

  7. SSM_CRUD新手练习(10)返回分页的JSON数据

    我们完成了员工的分页查询,但是现在这种做法只能适应浏览器和服务器的交互模式,但在移动互联网时代,客户端不仅仅只有浏览器,还有安卓和IOS客户端.我们的解决方式是AJAX+JSON方式来实现平台无关性. ...

  8. SSM_CRUD新手练习(4)修改生成的mapper.xml映射文件

    我们为什么要修改呢,这是因为我们查询的时候,我们有时候需要连表查询,例如我们需要查询出员工表的信息(emp_id,emp_name...)与此同时,我们还想查询出该员工所在的部门(dept_name) ...

  9. SSM_CRUD新手练习(1)创建项目

    最近看了SSM框架,网上找了个入门视频的比较简单的小项目熟悉一下框架.现在把整个过程记录下来. 1.创建Maven工程,注意我们选择的是simple project就够了. 这样我们的Maven项目就 ...

随机推荐

  1. fedora 安装gdal

    hese steps worked for me on a Fedora system: 1.) download the 3 files related to oracle instant clie ...

  2. 可以搜索局域网内的所有IP地址的软件

    几乎都用现有的工具,直接扫描,这里我已python为例,搜索一下局域网内所有活动IP,基本原理就是ping,对返回的结果进行分析,从而判断对应ip是否活动,代码很简单,实验环境win10+python ...

  3. linux通过python设置系统默认编码

    import sys sys.reload() sys.getdefaultencoding() # 查看设置前系统默认编码 sys.setdefaultencoding('utf-8') sys.g ...

  4. 11. pt-heartbeat

    pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop ------------------------------------- ...

  5. PHP Cron Expression Parser ( LARAVEL )

       The PHP cron expression parser can parse a CRON expression, determine if it is due to run, calcul ...

  6. Spring 注解(一)Spring 注解编程模型

    Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列 ...

  7. c++智能指针(1)

    根据muduo开源库作者陈硕的一些文章.对于多线程下C++编程提出了一些观点.主要是多线程下对象的销毁比较困难,但是由于多线程下,mutext是无法保护析构的.而后提出了智能指针的方案并对使用该指针会 ...

  8. Oracal 学习之用户角色创建分配表空间 给角色分配权限

    //创建角色inspur 密码为inspur,默认的表空间为USERS create user inspur identified by inspur default tablespace USERS ...

  9. .net调用word转换pdf出现80080005错误的解决办法

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005. 1:在服务器上安装offi ...

  10. 【Web】Sublime Text 3 连接sftp/ftp(远程服务器)

    在 Win 下常用 Xftp 软件来和远程服务传递文件,但是要是在项目开发的时候频繁的将远程文件拖到本地编辑然后再传回远程服务器,那真是麻烦无比,但是Sublime中SFTP插件,它让这世界美好了许多 ...