SSM整合
一、MyBatis层
1、首先创建一个数据库
2、搭建环境
创建一个maven项目
添加依赖
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.49</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.0.9</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> servlet-api</ artifactId>
< version> 2.5</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jsp-api</ artifactId>
< version> 2.0</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 2.0.2</ version>
</ dependency>
< 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>
</ dependencies>
静态资源
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
</ build>
idea连接数据库
构造项目结构目录
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
< configuration>
</ configuration>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " com.itlf.pojo" />
</ typeAliases>
< mappers>
< mapper class = " com.itlf.mapper.BookMapper" />
</ mappers>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: aop= " http://www.springframework.org/schema/aop"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd" >
</ beans>
< import resource = " spring-mapper.xml" />
< import resource = " spring-service.xml" />
< import resource = " spring-mvc.xml" />
mybatis连接数据库:
数据库配置文件 database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=123456
pojo
Books
package com. itlf. pojo ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
mapper
BookMapper
package com. itlf. mapper ;
import com. itlf. pojo. Books ;
import org. apache. ibatis. annotations. Param ;
import java. util. List ;
public interface BookMapper {
int addBook ( Books book) ;
int deleteBookById ( @Param ( "bookId" ) int id) ;
int updateBook ( Books book) ;
Books queryBookById ( int id) ;
List < Books > queryAllBooks ( ) ;
}
Books queryBookByName ( @Param ( "bookName" ) String name) ;
BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.itlf.mapper.BookMapper" >
< insert id = " addBook" parameterType = " Books" >
insert into ssmbuild.books (bookName, bookCounts, detail)
values (#{bookName}, #{bookCounts}, #{detail})
</ insert>
< delete id = " deleteBookById" parameterType = " Books" >
delete from ssmbuild.books where bookID=#{bookId}
</ delete>
< update id = " updateBook" parameterType = " Books" >
update books
set bookName = #{bookName},bookCounts = #{bookCounts},detail=#{detail}
where bookID=#{bookID}
</ update>
< select id = " queryBookById" resultType = " Books" >
select bookID,bookName,bookCounts,detail from books
< where>
< if test = " bookID!=null" >
and bookID=#{bookID}
</ if>
< if test = " bookName!=null" >
and bookName=#{bookName}
</ if>
< if test = " bookCounts!=null" >
and bookCounts=#{bookCounts}
</ if>
</ where>
</ select>
< select id = " queryAllBooks" resultType = " Books" >
select bookID,bookName,bookCounts,detail from books
</ select>
</ mapper>
< select id = " queryBookByName" resultType = " Books" >
select bookID,bookName,bookCounts,detail from books
< where>
< if test = " bookName!=null" >
and bookName=#{bookName}
</ if>
</ where>
</ select>
二、Spring层
spring-mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " classpath:database.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " mapperLocations" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.itlf.mapper" />
</ bean>
</ beans>
spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd" >
< context: component-scan base-package = " com.itlf.service" />
< bean id = " BookServiceImpl" class = " com.itlf.service.BookServiceImpl" >
< property name = " mapper" ref = " bookMapper" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
</ beans>
三、SpringMVC层
增加web支持
web.xml
<?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" >
< 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-config>
< session-timeout> 15</ session-timeout>
</ session-config>
</ web-app>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< mvc: annotation-driven/>
< mvc: default-servlet-handler/>
< context: component-scan base-package = " com.itlf.controller" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/jsp/" />
< property name = " suffix" value = " .jsp" />
</ bean>
</ beans>
四、写实际的业务
BookController
package com. itlf. controller ;
import com. itlf. pojo. Books ;
import com. itlf. 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 list ( Model model) {
List < Books > booksList = bookService. queryAllBooks ( ) ;
model. addAttribute ( "list" , booksList) ;
return "allBook" ;
}
@RequestMapping ( "/toAddPaper" )
public String toAddPaper ( ) {
return "addBook" ;
}
@RequestMapping ( "/addBook" )
public String addBook ( Books book) {
System . out. println ( "addBook" + book) ;
bookService. addBook ( book) ;
return "redirect:/book/allBook" ;
}
@RequestMapping ( "/toUpdatePaper" )
public String toUpdatePaper ( int id, Model model) {
Books book = bookService. queryBookById ( id) ;
model. addAttribute ( "book" , book) ;
return "updateBook" ;
}
@RequestMapping ( "/updateBook" )
public String updateBook ( Books book) {
bookService. updateBook ( book) ;
return "redirect:/book/allBook" ;
}
@RequestMapping ( "/deleteBook" )
public String deleteBook ( int id) {
bookService. deleteBookById ( id) ;
return "redirect:/book/allBook" ;
}
@RequestMapping ( "/queryBookName" )
public String queryBookName ( String bookName, Model model) {
Books book = bookService. queryBookByName ( bookName) ;
model. addAttribute ( "book" , book) ;
return "queryBook" ;
}
}
allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: LiuF
Date: 2021/6/9
Time: 20:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 学生展示页面</ title>
< link href = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel = " stylesheet" >
</ head>
< body>
< div class = " container" >
< div class = " row clearfix" >
< div class = " col-md-12 column" >
< div class = " page-header" >
< h1>
< small> 学生列表---显示所有学生</ small>
< small> </ small>
</ h1>
</ div>
</ div>
< div class = " row" >
< div class = " col-md-4 column" >
< a class = " btn btn-primary" href = " ${pageContext.request.contextPath}/book/toAddPaper" > 新增学生</ a>
</ div>
< div class = " col-md-4 column" > </ div>
< form action = " ${pageContext.request.contextPath}/book/queryBookName" method = " post" class = " form-inline" >
< div class = " col-md-4 column" >
< input type = " text" class = " form-control" name = " bookName" placeholder = " 输入查询书籍的名字" >
< input type = " submit" value = " 查询" class = " btn btn-primary" >
</ div>
</ form>
</ 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>
< th> 操作</ th>
</ tr>
</ thead>
< tbody>
< c: forEach var = " book" items = " ${list}" >
< tr>
< td> ${book.bookID}</ td>
< td> ${book.bookName}</ td>
< td> ${book.bookCounts}</ td>
< td> ${book.detail}</ td>
< td>
< a href = " ${pageContext.request.contextPath}/book/toUpdatePaper?id=${book.bookID}" > 修改</ a>
|
< a href = " ${pageContext.request.contextPath}/book/deleteBook?id=${book.bookID}"
onclick = " deleteClick()" > 删除</ a>
</ td>
</ tr>
</ c: forEach>
</ tbody>
</ table>
</ div>
</ div>
</ div>
</ body>
</ html>
addBook.jsp
<%--
Created by IntelliJ IDEA.
User: LiuF
Date: 2021/6/9
Time: 23:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 添加书籍</ title>
< link href = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel = " stylesheet" >
</ head>
< body>
< div class = " container" >
< div class = " row clearfix" >
< div class = " col-md-12 column" >
< div class = " page-header" >
< h1>
< small> 新增书籍</ small>
</ h1>
</ div>
</ div>
</ div>
< form action = " ${pageContext.request.contextPath}/book/addBook" method = " post" >
< div class = " form-group" >
< label for = " bookName" > 书籍名称</ label>
< input type = " text" class = " form-control" id = " bookName" name = " bookName" required >
</ div>
< div class = " form-group" >
< label for = " bookCounts" > 书籍数量</ label>
< input type = " text" class = " form-control" id = " bookCounts" name = " bookCounts" required >
</ div>
< div class = " form-group" >
< label for = " detail" > 书籍详情</ label>
< input type = " text" class = " form-control" id = " detail" name = " detail" required >
</ div>
< button type = " submit" class = " btn btn-default" > 添加</ button>
</ form>
</ div>
</ body>
</ html>
updateBook.jsp
<%--
Created by IntelliJ IDEA.
User: LiuF
Date: 2021/6/10
Time: 8:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 修改书籍</ title>
< link href = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel = " stylesheet" >
</ head>
< body>
< div class = " container" >
< div class = " row clearfix" >
< div class = " col-md-12 column" >
< div class = " page-header" >
< h1>
< small> 修改书籍</ small>
</ h1>
</ div>
</ div>
</ div>
< form action = " ${pageContext.request.contextPath}/book/updateBook" method = " post" >
< input type = " hidden" name = " bookID" value = " ${book.bookID}" >
< div class = " form-group" >
< label for = " bookName" > 书籍名称</ label>
< input type = " text" class = " form-control" id = " bookName" name = " bookName" required value = " ${book.bookName}" >
</ div>
< div class = " form-group" >
< label for = " bookCounts" > 书籍数量</ label>
< input type = " text" class = " form-control" id = " bookCounts" name = " bookCounts" required value = " ${book.bookCounts}" >
</ div>
< div class = " form-group" >
< label for = " detail" > 书籍详情</ label>
< input type = " text" class = " form-control" id = " detail" name = " detail" required value = " ${book.detail}" >
</ div>
< button type = " submit" class = " btn btn-default" > 修改</ button>
</ form>
</ div>
</ body>
</ html>
queryBook.jsp
<%--
Created by IntelliJ IDEA.
User: LiuF
Date: 2021/6/11
Time: 9:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 查询书籍</ title>
< link href = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel = " stylesheet" >
</ head>
< body>
< div class = " container" >
< div class = " row clearfix" >
< div class = " col-md-12 column" >
< div class = " page-header" >
< h1>
< small> 查询书籍</ small>
</ h1>
</ div>
</ div>
</ div>
< form action = " ${pageContext.request.contextPath}/book/allBook" >
< div class = " form-group" >
< label> 书籍名称</ label>
< input type = " text" class = " form-control" value = " ${book.bookName}" readonly >
</ div>
< div class = " form-group" >
< label> 书籍数量</ label>
< input type = " text" class = " form-control" value = " ${book.bookCounts}" readonly >
</ div>
< div class = " form-group" >
< label> 书籍详情</ label>
< input type = " text" class = " form-control" value = " ${book.detail}" readonly >
</ div>
< button type = " submit" class = " btn btn-default" > 返回</ button>
</ form>
</ div>
</ body>
</ html>