WHCSRL 技术网

ssm框架整合

目录

1.导入依赖

2.创建实体类

3.写dao层接口

4.写mybatis核心配置文件和接口配置文件

5.用spring整合Mybatis层也就是Dao层

6.spring整合Service层

7.添加web支持

8.完整的目录结构


1.导入依赖

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.12</version>
  5. </dependency>
  6. <!--数据库驱动-->
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <version>5.1.47</version>
  11. </dependency>
  12. <!-- 数据库连接池 -->
  13. <dependency>
  14. <groupId>com.mchange</groupId>
  15. <artifactId>c3p0</artifactId>
  16. <version>0.9.5.2</version>
  17. </dependency>
  18. <!--Servlet - JSP -->
  19. <dependency>
  20. <groupId>javax.servlet</groupId>
  21. <artifactId>servlet-api</artifactId>
  22. <version>2.5</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>javax.servlet.jsp</groupId>
  26. <artifactId>jsp-api</artifactId>
  27. <version>2.2</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>javax.servlet</groupId>
  31. <artifactId>jstl</artifactId>
  32. <version>1.2</version>
  33. </dependency>
  34. <!--Mybatis-->
  35. <dependency>
  36. <groupId>org.mybatis</groupId>
  37. <artifactId>mybatis</artifactId>
  38. <version>3.5.2</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.mybatis</groupId>
  42. <artifactId>mybatis-spring</artifactId>
  43. <version>2.0.2</version>
  44. </dependency>
  45. <!--Spring-->
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-webmvc</artifactId>
  49. <version>5.1.9.RELEASE</version>
  50. </dependency>
  51. <!--spring jdbc-->
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-jdbc</artifactId>
  55. <version>5.1.9.RELEASE</version>
  56. </dependency>
  57. <!-- spring的核心依赖 -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-context</artifactId>
  61. <version>5.1.9.RELEASE</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>com.fasterxml.jackson.core</groupId>
  65. <artifactId>jackson-databind</artifactId>
  66. <version>2.12.1</version>
  67. </dependency>
  68. <!--json的依赖-->
  69. <dependency>
  70. <groupId>com.alibaba</groupId>
  71. <artifactId>druid-spring-boot-starter</artifactId>
  72. <version>1.1.10</version>
  73. </dependency>
  74. </dependencies>
  75. <!-- <dependency>
  76. <groupId>com.fasterxml.jackson.core</groupId>
  77. <artifactId>jackson-databind</artifactId>
  78. <version>2.10.0</version>
  79. </dependency>-->
  80. <!--静态资源问题-->
  81. <build>
  82. <resources>
  83. <resource>
  84. <directory>src/main/java</directory>
  85. <includes>
  86. <include>**/*.properties</include>
  87. <include>**/*.xml</include>
  88. </includes>
  89. <filtering>false</filtering>
  90. </resource>
  91. <resource>
  92. <directory>src/main/resources</directory>
  93. <includes>
  94. <include>**/*.properties</include>
  95. <include>**/*.xml</include>
  96. </includes>
  97. <filtering>false</filtering>
  98. </resource>
  99. </resources>
  100. </build>

2.创建实体类

  1. package pojo;
  2. public class books {
  3. private int bookId;
  4. private String bookName;
  5. private int bookCounts;
  6. private String detail;
  7. public books(int bookId, String bookName, int bookCounts, String detail) {
  8. this.bookId = bookId;
  9. this.bookName = bookName;
  10. this.bookCounts = bookCounts;
  11. this.detail = detail;
  12. }
  13. public books() {
  14. }
  15. public int getBookId() {
  16. return bookId;
  17. }
  18. public void setBookId(int bookId) {
  19. this.bookId = bookId;
  20. }
  21. public String getBookName() {
  22. return bookName;
  23. }
  24. public void setBookName(String bookName) {
  25. this.bookName = bookName;
  26. }
  27. public int getBookCounts() {
  28. return bookCounts;
  29. }
  30. public void setBookCounts(int bookCounts) {
  31. this.bookCounts = bookCounts;
  32. }
  33. public String getDetail() {
  34. return detail;
  35. }
  36. public void setDetail(String detail) {
  37. this.detail = detail;
  38. }
  39. }

3.写dao层接口

这里暂时只有一个方法

  1. package Dao;
  2. import pojo.books;
  3. import java.util.List;
  4. public interface BooksMapper {
  5. List<books> selectbooks();
  6. }

4.写mybatis核心配置文件和接口配置文件

这个是接口配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--把刚刚我们写的接口配置进去-->
  6. <mapper namespace="Dao.BooksMapper">
  7. <select id="BooksMapper" resultType="pojo.books">
  8. select * from books
  9. </select>
  10. </mapper>

mybatis核心配置文件

  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. <!--把那个接口导进来-->
  7. <mappers>
  8. <package name="Dao"/>
  9. </mappers>
  10. </configuration>

5.用spring整合Mybatis层也就是Dao层

这个是spring整合Mybatis的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. 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. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 配置整合mybatis -->
  10. <!-- 1.关联数据库文件 -->
  11. <context:property-placeholder location="classpath:database.properties"/>
  12. <!-- 2.数据库连接池 -->
  13. <!--数据库连接池
  14. dbcp 半自动化操作 不能自动连接
  15. c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
  16. -->
  17. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <!-- 配置连接池属性 -->
  19. <property name="driverClass" value="${jdbc.driver}"/>
  20. <property name="jdbcUrl" value="${jdbc.url}"/>
  21. <property name="user" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23. <!-- c3p0连接池的私有属性 -->
  24. <property name="maxPoolSize" value="30"/>
  25. <property name="minPoolSize" value="10"/>
  26. <!-- 关闭连接后不自动commit -->
  27. <property name="autoCommitOnClose" value="false"/>
  28. <!-- 获取连接超时时间 -->
  29. <property name="checkoutTimeout" value="10000"/>
  30. <!-- 当获取连接失败重试次数 -->
  31. <property name="acquireRetryAttempts" value="2"/>
  32. </bean>
  33. <!-- 3.配置SqlSessionFactory对象 -->
  34. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  35. <!-- 注入数据库连接池 -->
  36. <property name="dataSource" ref="dataSource"/>
  37. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  38. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  39. </bean>
  40. <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
  41. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <!-- 注入sqlSessionFactory -->
  43. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  44. <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
  45. <property name="basePackage" value="Dao"/>
  46. </bean>
  47. </beans>

数据库配置文件,这里你们改一下数据库就OK

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild
  3. jdbc.username=root
  4. jdbc.password=root

6.spring整合Service层

写接口和实现类

  1. package Service;
  2. import org.springframework.stereotype.Service;
  3. import pojo.books;
  4. import java.util.List;
  5. public interface BooksService {
  6. List<books> selectbooks();
  7. }

实现类

  1. @Service
  2. public class BooksServicelmpl implements BooksService{
  3. /*这里是把Dao的接口引进来了因为Service层调用Dao层*/
  4. @Autowired
  5. private BooksMapper booksMapper;
  6. public void setBooksMapper(BooksMapper booksMapper) {
  7. this.booksMapper = booksMapper;
  8. }
  9. @Override
  10. public List<books> selectbooks() {
  11. return booksMapper.selectbooks();
  12. }
  13. }

写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
  6. http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  7. <context:component-scan base-package="Service"/>
  8. <!-- <bean id="bookServicelmpl" class="service.bookServicelmpl">
  9. <property name="bookMapper" ref="bookMapper"/>
  10. </bean>-->
  11. <!--因为serv层要调Dao层我们把数据源拿过来,如果这里报红就是因为没有引入Dao的那个数据源-->
  12. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  13. <property name="dataSource" ref="dataSource"/>
  14. </bean>
  15. </beans>

关联spring配置文件,我们写个总的配置文件到进去就ok

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <import resource="classpath:spring-Service.xml"/>
  7. <import resource="classpath:spring-Dao.xml"/>
  8. </beans>

7.spring整合Conteoller层

  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
  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. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- 扫描web相关的bean -->
  13. <context:component-scan base-package="Controller"/>
  14. <!-- 1.开启SpringMVC注解驱动 -->
  15. <mvc:annotation-driven/>
  16. <!-- 2.静态资源默认servlet配置-->
  17. <mvc:default-servlet-handler/>
  18. <!-- 配置视图解析器 -->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <!-- 前缀 -->
  21. <property name="prefix" value="/WEB-INF/jsp/"/>
  22. <!-- 后缀 -->
  23. <property name="suffix" value=".jsp"/>
  24. </bean>
  25. <mvc:annotation-driven>
  26. <mvc:message-converters register-defaults="true">
  27. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  28. <constructor-arg value="UTF-8"/>
  29. </bean>
  30. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  31. <property name="objectMapper">
  32. <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
  33. <property name="failOnEmptyBeans" value="false"/>
  34. </bean>
  35. </property>
  36. </bean>
  37. </mvc:message-converters>
  38. </mvc:annotation-driven>
  39. </beans>

写Controller的类

  1. @RestController
  2. public class bookController {
  3. @Autowired
  4. private BooksService booksService;
  5. public void setBooksService(BooksService booksService) {
  6. this.booksService = booksService;
  7. }
  8. @RequestMapping("/books")
  9. public List<books> selectbooks()
  10. {
  11. List<books> list = booksService.selectbooks();
  12. return list;
  13. }
  14. }

把三个配置文件关联

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <import resource="classpath:spring-Service.xml"/>
  7. <import resource="classpath:spring-Dao.xml"/>
  8. <import resource="spring-mvc.xml"/>
  9. </beans>

7.添加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. <!--DispatcherServlet-->
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>springmvc</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21. <!--乱码解决-->
  22. <filter>
  23. <filter-name>encodingFilter</filter-name>
  24. <filter-class>
  25. org.springframework.web.filter.CharacterEncodingFilter
  26. </filter-class>
  27. <init-param>
  28. <param-name>encoding</param-name>
  29. <param-value>utf-8</param-value>
  30. </init-param>
  31. </filter>
  32. <filter-mapping>
  33. <filter-name>encodingFilter</filter-name>
  34. <url-pattern>/*</url-pattern>
  35. </filter-mapping>
  36. <!--Session过期时间-->
  37. <session-config>
  38. <session-timeout>15</session-timeout>
  39. </session-config>
  40. </web-app>

创建一个lib包把依赖导进去

添加工件测试

8.完整的目录结构

 

推荐阅读