语法会随版本的变化而改变,但其核心流程几乎不变;

大部分的保存代码,少部分的解释,可以参照注解和注释。

SSM框架

Sql

  1. CREATE DATABASE `ssmbuild`;
  2. USE `ssmbuild`;
  3. DROP TABLE IF EXISTS `books`;
  4. CREATE TABLE `books` (
  5. `bookID` int(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
  6. `bookName` varchar(100) NOT NULL COMMENT '书名',
  7. `bookCounts` int(11) NOT NULL COMMENT '数量',
  8. `detail` varchar(200) NOT NULL COMMENT '描述',
  9. PRIMARY KEY (`bookID`)
  10. ) ENGINE = InnoDB CHARACTER SET = utf8;
  11. INSERT INTO `books` (`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
  12. (1,'Java',1,'Java'),
  13. (2,'MySQL',10,'MySQL'),
  14. (3,'Linux',5,'Linux');

导入依赖

  1. <!--依赖: junit, 数据库驱动,连接池,servlet, jsp, mybatis, mybattis-spring, spring, -->
  2. <dependencies>
  3. <!--Junit-->
  4. <dependency>
  5. <groupId>junit</groupId>
  6. <artifactId>junit</artifactId>
  7. <version>4.12</version>
  8. </dependency>
  9. <!--数据库驱动-->
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <version>5.1.47</version>
  14. </dependency>
  15. <!--数据库连接池: c3p0-->
  16. <dependency>
  17. <groupId>com.mchange</groupId>
  18. <artifactId>c3p0</artifactId>
  19. <version>0.9.5.2</version>
  20. </dependency>
  21. <!--Servlet - JSP -->
  22. <dependency>
  23. <groupId>javax.servlet</groupId>
  24. <artifactId>servlet-api</artifactId>
  25. <version>2.5</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>javax.servlet.jsp</groupId>
  29. <artifactId>jsp-api</artifactId>
  30. <version>2.2</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>javax.servlet</groupId>
  34. <artifactId>jstl</artifactId>
  35. <version>1.2</version>
  36. </dependency>
  37. <!--Mybatis-->
  38. <dependency>
  39. <groupId>org.mybatis</groupId>
  40. <artifactId>mybatis</artifactId>
  41. <version>3.5.2</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.mybatis</groupId>
  45. <artifactId>mybatis-spring</artifactId>
  46. <version>2.0.2</version>
  47. </dependency>
  48. <!--Spring-->
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-webmvc</artifactId>
  52. <version>5.1.9.RELEASE</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-jdbc</artifactId>
  57. <version>5.1.9.RELEASE</version>
  58. </dependency>
  59. </dependencies>
  60. <!--静态资源导出问题-->
  61. <build>
  62. <resources>
  63. <resource>
  64. <directory>src/main/java</directory>
  65. <includes>
  66. <include>**/properties</include>
  67. <include>**/*.xml</include>
  68. </includes>
  69. <filtering>false</filtering>
  70. </resource>
  71. <resource>
  72. <directory>src/mainresources</directory>
  73. <includes>
  74. <include>**/properties</include>
  75. <include>**/.xml</include>
  76. </includes>
  77. <filtering>false</filtering>
  78. </resource>
  79. </resources>
  80. </build>

建立基本结构和配置框架

SSM整合 - 图1

  • cn.dafran.pojo
  • cn.dafran.mapper
  • cn.dafran.service
  • cn.dafran.controller
  • mybatis-config.xml的头文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <!--mybatis-->
  6. <configuration>
  7. </configuration>
  • 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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--spring-->
  7. </beans>

持久层(Mybatis):Dao层(mapper) 层编写

  1. 数据库配置文件 database.properties
  1. jdbc.driver=com.mysql.jdbc.Driver
  2. #如果使用的是MySQL8.0+,增加一个时区的配置; &serverTimezone=Asia/Shanghai
  3. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
  4. jdbc.username=root
  5. jdbc.password=123456
  1. 配置mybatis-config.xmlg.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <!--mybatis-->
  6. <configuration>
  7. <!--配置数据源,交给Spring去做-->
  8. <typeAliases>
  9. <package name="cn.dafran.pojo"/>
  10. </typeAliases>
  11. <!--在mybatis配置文件中注册-->
  12. <mappers>
  13. <mapper class="cn.dafran.mapper.BookMapper"/>
  14. </mappers>
  15. </configuration>

Entity

  1. <!--Lombok-->
  2. <dependency>
  3. <groupId>org.projectlombok</groupId>
  4. <artifactId>lombok</artifactId>
  5. <version>1.16.10</version>
  6. </dependency>
  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Books {
  5. /**
  6. * 书id
  7. */
  8. private Integer bookID;
  9. /**
  10. * 书名
  11. */
  12. private String bookName;
  13. /**
  14. * 数量
  15. */
  16. private Integer bookCounts;
  17. /**
  18. * 描述
  19. */
  20. private String detail;
  21. }

Dao

  1. public interface BookMapper {
  2. //增加一本书
  3. int addBook(Books books);
  4. //删除一本书
  5. int deleteBookById(@Param("bookId") int id);
  6. //修改一本书
  7. int updateBook(Books books);
  8. //查找一本书
  9. Books queryBooksById(@Param("bookId") int id);
  10. //查询全部书
  11. List<Books> queryAllBooks();
  12. }
  1. <mapper namespace="cn.dafran.mapper.BookMapper">
  2. <insert id="addBook" parameterType="Books">
  3. insert into ssmbuild.books (bookName, bookCounts, detail)
  4. VALUES (#{bookName},#{bookCounts},#{detail});
  5. </insert>
  6. <delete id="deleteBookById" parameterType="int">
  7. delete from ssmbuild.books
  8. where bookID= #{bookId};
  9. </delete>
  10. <update id="updateBook" parameterType="Books">
  11. update ssmbuild.books
  12. set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
  13. where bookID=#{bookID};
  14. </update>
  15. <select id="queryBooksById" resultType="Books">
  16. select * from ssmbuild.books
  17. where bookID=#{bookId};
  18. </select>
  19. <select id="queryAllBooks" resultType="Books">
  20. select * from ssmbuild.books;
  21. </select>
  22. </mapper>

Service

  1. public interface BookService {
  2. //增加一本书
  3. int addBook(Books books);
  4. //删除一本书
  5. int deleteBookById(int id);
  6. //修改一本书
  7. int updateBook(Books books);
  8. //查找一本书
  9. Books queryBooksById(int id);
  10. //查询全部书
  11. List<Books> queryAllBooks();
  12. }
  1. public class BookServiceImpl implements BookService {
  2. //service调用dao层,组合Dao
  3. private BookMapper bookMapper;
  4. public void setBookMapper(BookMapper bookMapper) {
  5. this.bookMapper = bookMapper;
  6. }
  7. @Override
  8. public int addBook(Books books) {
  9. return bookMapper.addBook(books);
  10. }
  11. @Override
  12. public int deleteBookById(int id) {
  13. return bookMapper.deleteBookById(id);
  14. }
  15. @Override
  16. public int updateBook(Books books) {
  17. return bookMapper.updateBook(books);
  18. }
  19. @Override
  20. public Books queryBooksById(int id) {
  21. return bookMapper.queryBooksById(id);
  22. }
  23. @Override
  24. public List<Books> queryAllBooks() {
  25. return bookMapper.queryAllBooks();
  26. }
  27. }

Spring层

spring-dao.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. <!--spring整合dao层-->
  10. <!--1.关联数据库配置文件-->
  11. <context:property-placeholder location="classpath:database.properties"/>
  12. <!--2.连接池
  13. dbcp: 半自动化操作,不能 自动连接
  14. c3p0: 自动化操作(自动化的加载配置文件,并且可以自动设置到对象中! )
  15. druid:
  16. hikari:spring2.0 新增默认数据源
  17. -->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  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. <property name="dataSource" ref="dataSource"/>
  36. <!--绑定Mybatis的配置文件-->
  37. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  38. </bean>
  39. <!--配置dao接口扫描包,动态的实现Dao接口可以注入到Spring容器中!-->
  40. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  41. <!--注入sqlSessionFactory-->
  42. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  43. <!--要扫描的dao包-->
  44. <property name="basePackage" value="cn.dafran.mapper"/>
  45. </bean>
  46. </beans>

spring-service.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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  7. <!--1.扫描service的包-->
  8. <context:component-scan base-package="cn.dafran.service"/>
  9. <!--2.将所有的业务注入到spring中-->
  10. <bean id="BookServiceImpl" class="cn.dafran.service.impl.BookServiceImpl">
  11. <property name="bookMapper" ref="bookMapper"/>
  12. </bean>
  13. <!--3.声明是事务层-->
  14. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  15. <!--注入数据源-->
  16. <property name="dataSource" ref="dataSource"/>
  17. </bean>
  18. <!--4.AOP事务支持-->
  19. </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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--spring配置方法-->
  7. <!--也可以在service层 中加@Service注解 @Autowired-->
  8. <import resource="classpath:spring-dao.xml"/>
  9. <import resource="classpath:spring-service.xml"/>
  10. </beans>

SpringMVC 层(controller)

添加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. <!--DispatchServlet-->
  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. <param-value>classpath:applicationContext.xml</param-value>
  13. </init-param>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>springmvc</servlet-name>
  18. <url-pattern>/</url-pattern>
  19. </servlet-mapping>
  20. <!--乱码过滤-->
  21. <filter>
  22. <filter-name>encodingFilter</filter-name>
  23. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  24. <init-param>
  25. <param-name>encoding</param-name>
  26. <param-value>utf-8</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>encodingFilter</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33. <!--Session-->
  34. <session-config>
  35. <session-timeout>15</session-timeout>
  36. </session-config>
  37. </web-app>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  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/mvc
  9. http://www.springframework.org/schema/mvc/spring-cache.xsd
  10. http://www.springframework.org/schema/context
  11. https://www.springframework.org/schema/context/spring-context.xsd">
  12. <!--1.注解驱动-->
  13. <mvc:annotation-driven/>
  14. <!--2.静态资源注入-->
  15. <mvc:default-servlet-handler/>
  16. <!--3.扫描包:controller-->
  17. <context:component-scan base-package="cn.dafran.controller"/>
  18. <!--4.视图解析器-->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="/WEB-INF/jsp/"/>
  21. <property name="suffix" value=".jsp"/>
  22. </bean>
  23. </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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--spring配置方法-->
  7. <!--也可以在service层 中加@Service注解 @Autowired-->
  8. <import resource="classpath:spring-dao.xml"/>
  9. <import resource="classpath:spring-service.xml"/>
  10. <import resource="classpath:spring-mvc.xml"/>
  11. </beans>