1、SSM框架整合
1.1、工具版本
-
tomcat:9.0.50
-
mysql:8.0.14
-
jdk:1.8
-
java:8
-
maven:3.6
1.2、MySql数据库
CREATE DATABASE ssm; use ssm; SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; CREATE TABLE `ssm`.`books` ( `bookID` int(16) NOT NULL COMMENT '书id', `bookName` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '书名', `bookCounts` int(11) DEFAULT NULL COMMENT '数量', `detail` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '描述', PRIMARY KEY (`bookID`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; INSERT INTO `books` VALUES (1, 'Java', 1, '从入门到放弃'); INSERT INTO `books` VALUES (2, 'Mysql', 10, '从删库到跑路'); INSERT INTO `books` VALUES (3, 'Linux', 5, '从入门到入牢'); SET FOREIGN_KEY_CHECKS = 1;
1.3、pom文件
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <groupId>com.liuxf</groupId> <artifactId>ssmbuild</artifactId> <version>1.0-SNAPSHOT</version> <!--依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring--> <dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 数据库连接池 c3p0 dbcp也可以 这个可以随意 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <!--在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
1.4、web项目
maven创建新项目,并将项目转化为web的项目。
具体步骤:idea->右键点击项目->ADD Frameworks Support->选择web->应用确认
1.5、工程结构
1.6、mybatis配置
1.6.1、database.properties
src/main/resources
jdbc.driver=com.mysql.jdbc.Driver # 如果使用的是mysql8.0以上还需要增加一个时区的配置; jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=123456
1.6.2、mybatis-config.xml
src/main/resources
数据库的配置脚本spring来实现,实现自动注入功能,脚本IOC容器进行控制
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--用来配置数据源,交给spring去做--> <typeAliases> <package name="com.Liuxf.pojo"/> </typeAliases> <mappers> <mapper resource="com/Liuxf/dao/BookMapper.xml"/> </mappers> </configuration>
1.6.3、spring-dao.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!--2.连接池 dbcp:半自动化,不能自动连接 c3p0:自动化连接(自动化的加载配置文件,并且可以自动的配置到对象中) druid: hikari:springboot2.0默认继承 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="10" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="100" /> <property name="checkoutTimeout" value="10000"/> <property name="autoCommitOnClose" value="false"/> <property name="acquireRetryAttempts" value="2" /> </bean> <!--3.sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定Mybatis的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--配置dao接口扫描包,动态的实现了dao接口可以注入到Spring容器中!--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入 sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要扫描的dao包--> <property name="basePackage" value="com.Liuxf.dao"/> </bean> </beans>
1.6.4、Books.java
src/main/java/com/Liuxf/pojo
package com.Liuxf.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Books { private Long bookID; private String bookName; private Long bookCounts; private String detail; }
1.6.5、BookMapper.java
src/main/java/com/Liuxf/dao
package com.Liuxf.dao; import com.Liuxf.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookMapper { //查询一本书 Books queryBookById(@Param("bookID")Long id); //查询多本书 List<Books> queryBook(@Param("books")Books books); //增加一本书 int addBook(@Param("books")Books books); //删除一本书 int deleteBookById(@Param("bookID") Long id); //修改一本书 int updateBook(@Param("books") Books books); }
1.6.6、BookMapper.xml
src/main/java/com/Liuxf/dao
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.Liuxf.dao.BookMapper"> <select id="queryBookById" resultType="com.Liuxf.pojo.Books" parameterType="int"> select * from ssm.books where bookID = #{bookID}; </select> <select id="queryBook" resultType="com.Liuxf.pojo.Books" parameterType="com.Liuxf.pojo.Books"> select * from ssm.books where 1=1 <if test="books.bookID != null and books.bookID != '' "> and bookID = #{bookID} </if>; <if test="books.bookName != null and books.bookName != '' "> and bookName = #{bookName} </if>; <if test="books.bookCounts != null and books.bookCounts != '' "> and bookCounts = #{bookCounts} </if>; <if test="books.detail != null and books.detail != '' "> and detail = #{detail} </if>; </select> <insert id="addBook" parameterType="com.Liuxf.pojo.Books"> insert into ssm.books (bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <delete id="deleteBook" parameterType="int"> delete from ssm.books where bookID = #{bookID}; </delete> <update id="updateBook" parameterType="com.Liuxf.pojo.Books"> update ssm.books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID}; </update> </mapper>
1.6.7、BookService.java
src/main/java/com/Liuxf/service
package com.Liuxf.service; import com.Liuxf.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookService { //查询一本书 Books queryBookById(@Param("bookID")Long id); //查询多本书 List<Books> queryBook(@Param("books") Books books); //增加一本书 int addBook(@Param("books")Books books); //删除一本书 int deleteBookById(@Param("bookID") Long id); //修改一本书 int updateBook(@Param("books") Books books); }
1.6.8、BookServiceImpl.java
src/main/java/com/Liuxf/service
package com.Liuxf.service; import com.Liuxf.dao.BookMapper; import com.Liuxf.pojo.Books; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookServiceImpl implements BookService { private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public Books queryBookById(Long id) { return bookMapper.queryBookById(id); } @Override public List<Books> queryBook(Books books) { return bookMapper.queryBook(books); } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int deleteBookById(Long id) { return bookMapper.deleteBookById(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } }
这样mybatis就已经搭建完毕,可以进行测试了。
16.9、MyTest.java
src/test/java
import com.Liuxf.pojo.Books; import com.Liuxf.service.BookService; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public static void main(String[] args) { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); BookService bookServiceImpl = (BookService)classPathXmlApplicationContext.getBean("BookServiceImpl"); Books books = new Books(); for (Books book:bookServiceImpl.queryBook(books) ) { System.out.println(book); } } }
1.7、springMVC搭建
1.7.1、spring-mvc.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.注解驱动--> <mvc:annotation-driven/> <!--2.静态资源过滤--> <mvc:default-servlet-handler/> <!--3.扫描包--> <context:component-scan base-package="com.Liuxf.controller"/> <!--4.视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
1.7.2、web.xml
web/WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--DispotchServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--乱码过滤--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--session--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
这样springMVC就搭建好了
1.8、spring搭建
1.8.1、applicationContext.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-mvc.xml"/> <import resource="classpath:spring-service.xml"/> </beans>
1.8.2、spring-service.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.扫描service下的包--> <context:component-scan base-package="com.Liuxf.service"/> <!--2.将我们的所有业务类注入到spring,可以通过配置或者注解实现--> <bean id="BookServiceImpl" class="com.Liuxf.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--3.声明式事务--> <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!--4.aop事务支持!--> </beans>
1.8.3、spring-dao.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!--2.连接池 dbcp:半自动化,不能自动连接 c3p0:自动化连接(自动化的加载配置文件,并且可以自动的配置到对象中) druid: hikari:springboot2.0默认继承 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="10" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="100" /> <property name="checkoutTimeout" value="10000"/> <property name="autoCommitOnClose" value="false"/> <property name="acquireRetryAttempts" value="2" /> </bean> <!--3.sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定Mybatis的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--配置dao接口扫描包,动态的实现了dao接口可以注入到Spring容器中!--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入 sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要扫描的dao包--> <property name="basePackage" value="com.Liuxf.dao"/> </bean> </beans>
1.8.4、spring-mvc.xml
src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.注解驱动--> <mvc:annotation-driven/> <!--2.静态资源过滤--> <mvc:default-servlet-handler/> <!--3.扫描包--> <context:component-scan base-package="com.Liuxf.controller"/> <!--4.视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
1.9、完成项目
1.9.1、BookController
src/main/java/com/Liuxf/controller
package com.Liuxf.controller; import com.Liuxf.pojo.Books; import com.Liuxf.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/book") public class BookController { @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查询全部的书籍 @RequestMapping("/allBook") public String queryBook(Model model, Books books){ List<Books> books1 = bookService.queryBook(books); model.addAttribute("books",books1); return "allBook"; } }
1.9.2、index.jsp
web/WEB-INF/jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">进入书籍界面</a> </h3> </body> </html>
1.9.3、addBook.jsp
web/WEB-INF/jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>数据展示界面</title> <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1>书籍展示</h1> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>书籍列表 -------- 显示所有书籍</small> </h1> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>书籍编号</th> <th>书籍名称</th> <th>书籍数量</th> <th>书籍详情</th> </tr> </thead> <tbody> <c:forEach var="book" items="${books}"> <tr> <td>${book.bookID}</td> <td>${book.bookName}</td> <td>${book.bookCounts}</td> <td>${book.detail}</td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </body> </html>