当前位置:   article > 正文

SSM整合,非常详细的ssm整合 案例【源码】

SSM整合,非常详细的ssm整合 案例【源码】

 

目录

ssm的整合就是spring框架和mybatis框架的整合

一,创建一个mven web项目:

 二,引用相关的依赖

三,配置springMVC配置文件

四,配置spring文件

五,web.xml

六,使用easycode代码生成器

七,业务层

八,控制层

九,结果


ssm的整合就是spring框架和mybatis框架的整合

一,创建一个mven web项目

 

将web.xml文件中的内容用下面的代码替换掉 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. </web-app>

 二,引用相关的依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-tx</artifactId>
  5. <version>5.2.9.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-jdbc</artifactId>
  10. <version>5.2.9.RELEASE</version>
  11. </dependency>
  12. <!--spring和mybatis整合的依赖-->
  13. <dependency>
  14. <groupId>org.mybatis</groupId>
  15. <artifactId>mybatis-spring</artifactId>
  16. <version>2.0.6</version>
  17. </dependency>
  18. <!--druid连接池-->
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>druid</artifactId>
  22. <version>1.2.4</version>
  23. </dependency>
  24. <!--springmvc-->
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. <version>5.2.9.RELEASE</version>
  29. </dependency>
  30. <!--mybatis的依赖-->
  31. <dependency>
  32. <groupId>org.mybatis</groupId>
  33. <artifactId>mybatis</artifactId>
  34. <version>3.5.6</version>
  35. </dependency>
  36. <!--mysql的依赖-->
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <version>8.0.26</version>
  41. </dependency>
  42. <!--lombok-->
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <version>1.18.22</version>
  47. </dependency>
  48. <!--jackson-->
  49. <dependency>
  50. <groupId>com.fasterxml.jackson.core</groupId>
  51. <artifactId>jackson-databind</artifactId>
  52. <version>2.12.4</version>
  53. </dependency>
  54. <!--servlet和jsp-->
  55. <dependency>
  56. <groupId>javax.servlet</groupId>
  57. <artifactId>servlet-api</artifactId>
  58. <version>2.5</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>javax.servlet.jsp</groupId>
  62. <artifactId>jsp-api</artifactId>
  63. <version>2.2</version>
  64. </dependency>
  65. <!--log4j-->
  66. <dependency>
  67. <groupId>log4j</groupId>
  68. <artifactId>log4j</artifactId>
  69. <version>1.2.17</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.slf4j</groupId>
  73. <artifactId>slf4j-log4j12</artifactId>
  74. <version>1.7.30</version>
  75. </dependency>
  76. </dependencies>

三,配置springMVC配置文件

  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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <!--包扫描-->
  8. <context:component-scan base-package="com.rqk.controller"/>
  9. <!--静态资源放行-->
  10. <mvc:default-servlet-handler/>
  11. <!--特殊注解开启-->
  12. <mvc:annotation-driven/>
  13. <!--视图解析器-->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/WEB-INF/views/"/>
  16. <property name="suffix" value=".jsp"/>
  17. </bean>
  18. </beans>

四,配置spring文件

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--包扫描-->
  7. <context:component-scan base-package="com.rqk.service"/>
  8. <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
  9. <!--定义数据源 连接数据库-->
  10. <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql://localhost:3306/mydatis?serverTimezone=Asia/Shanghai"/>
  12. <property name="name" value="root"/>
  13. <property name="password" value=""/>
  14. <!--初始连接数-->
  15. <property name="initialSize" value="5"/>
  16. <!--最大连接数量-->
  17. <property name="maxActive" value="10"/>
  18. <!--连接超时等待时间 单位:毫秒-->
  19. <property name="maxWait" value="3000"/>
  20. </bean>
  21. <!--spring把mybatis配置文件的内容封装到SqlSessionFactoryBean类中-->
  22. <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  23. <!-- mybatis配置文件-->
  24. <property name="dataSource" value="ds"/>
  25. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
  26. </bean>
  27. <!--为指定到接口生成代理实现类。交于spring容器管理dao接口的代理实现类-->
  28. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  29. <property name="basePackage" value="com.rqk.dao"/>
  30. </bean>
  31. </beans>

五,web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <servlet>
  7. <servlet-name>springmvc</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:springmvc.xml</param-value>
  12. </init-param>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>springmvc</servlet-name>
  16. <url-pattern>/</url-pattern>
  17. </servlet-mapping>
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <context-param>
  22. <param-name>contextConfigLocation</param-name>
  23. <param-value>classpath:spring.xml</param-value>
  24. </context-param>
  25. </web-app>

六,使用easycode代码生成器

七,业务层

       1. 业务层接口

  1. public interface Bookservice {
  2. public List<Book> findall();
  3. }

         2.接口实现类

  1. @Service(value = "bookserviec")
  2. public class Bookserviecimpl implements Bookservice {
  3. @Autowired
  4. private BookDao bookDao;
  5. public List<Book> findall() {
  6. return bookDao.findall();
  7. }
  8. }

八,控制层

  1. @Controller
  2. @RequestMapping("/book")
  3. public class Bookcontroller {
  4. @Autowired
  5. private Bookservice bookserviec;
  6. @RequestMapping("/list")
  7. @ResponseBody
  8. public List<Book> list(){
  9. return bookserviec.findall();
  10. }
  11. }

九,结果

 数据库信息

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/200042
推荐阅读
相关标签