1. 数据库准备
CREATE DATABASE ssmbuild;
USE ssmbuild;
CREATE TABLE ` books` (
` bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id' ,
` bookName` VARCHAR ( 100 ) NOT NULL COMMENT '书名' ,
` bookCounts` INT NOT NULL COMMENT '数量' ,
` detail` VARCHAR ( 200 ) NOT NULL COMMENT '描述' ,
KEY ` bookID` ( ` bookID` )
) ENGINE = INNODB DEFAULT CHARSET = utf8
INSERT INTO ` books` ( ` bookID` , ` bookName` , ` bookCounts` , ` detail` ) VALUES
( 1 , 'Java' , 1 , '从入门到放弃' ) ,
( 2 , 'MySQL' , 10 , '从删库到跑路' ) ,
( 3 , 'Linux' , 5 , '从进门到进牢' )
2.新建maven项目
3.导入依赖
<?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.hzu</ groupId>
< artifactId> ssmbuild</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.13</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> com.mchange</ groupId>
< artifactId> c3p0</ artifactId>
< version> 0.9.5.2</ 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> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.5</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 2.0.5</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.7.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.2.7.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.6</ version>
</ dependency>
< dependency>
< groupId> jstl</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
</ dependencies>
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
</ resources>
</ build>
</ project>
4.mybatis整合
4.1 mybatis-config.xml(mybatis的核心配置文件)
<?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>
< typeAliases>
< package name = " com.hzu.pojo" />
</ typeAliases>
</ configuration>
4.2 pojo
package com. hzu. 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;
}
4.3 mapper
4.3.1 bookMapper.java
package com. hzu. mapper;
import com. hzu. pojo. Books;
import org. apache. ibatis. annotations. Param;
import java. util. List;
public interface BooksMapper {
public int addBook ( Books book) ;
public int deleteBookById ( @Param ( "bookId" ) int id) ;
public int updateBook ( Books book) ;
public Books queryBookById ( @Param ( "bookId" ) int id) ;
public List< Books> queryAllBooks ( ) ;
}
4.3.2 bookMapper.xml
<?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.hzu.mapper.BooksMapper" >
< insert id = " addBook" parameterType = " Books" >
insert into ssmbuild.books (bookName, bookCounts, detail)
values (#{bookName},#{bookCounts},#{detail})
</ insert>
< delete id = " deleteBookById" parameterType = " _int" >
delete from ssmbuild.books where bookID=#{bookId}
</ delete>
< update id = " updateBook" parameterType = " Books" >
update ssmbuild.books
< set>
< if test = " bookName!=null" >
bookName=#{bookName},
</ if>
< if test = " bookCounts!=null" >
bookCounts=#{bookCounts},
</ if>
< if test = " detail!=null" >
detail=#{detail}
</ if>
</ set>
</ update>
< select id = " queryBookById" parameterType = " _int" resultType = " Books" >
select * from ssmbuild.books where bookID=#{bookId}
</ select>
< select id = " queryAllBooks" resultType = " Books" >
select * from ssmbuild.books
</ select>
</ mapper>
5.service层构建
5.1 BookService
package com. hzu. service;
import com. hzu. pojo. Books;
import org. apache. ibatis. annotations. Param;
import java. util. List;
public interface BooksService {
public int addBook ( Books book) ;
public int deleteBookById ( @Param ( "bookId" ) int id) ;
public int updateBook ( Books book) ;
public Books queryBookById ( @Param ( "bookId" ) int id) ;
public List< Books> queryAllBooks ( ) ;
}
5.2 BookServiceImpl
package com. hzu. service;
import com. hzu. mapper. BooksMapper;
import com. hzu. pojo. Books;
import java. util. List;
public class BooksServiceImpl implements BooksService {
private BooksMapper booksMapper;
public void setBooksMapper ( BooksMapper booksMapper) {
this . booksMapper = booksMapper;
}
@Override
public int addBook ( Books book) {
return booksMapper. addBook ( book) ;
}
@Override
public int deleteBookById ( int id) {
return booksMapper. deleteBookById ( id) ;
}
@Override
public int updateBook ( Books book) {
return booksMapper. updateBook ( book) ;
}
@Override
public Books queryBookById ( int id) {
return booksMapper. queryBookById ( id) ;
}
@Override
public List< Books> queryAllBooks ( ) {
return booksMapper. queryAllBooks ( ) ;
}
}
6. spring整合mybatis
6.1 spring-dao.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: 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/context https://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " classpath:db.properties" />
< 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 = " maxPoolSize" value = " 30" />
< property name = " minPoolSize" value = " 10" />
< property name = " autoCommitOnClose" value = " false" />
< property name = " checkoutTimeout" value = " 10000" />
< property name = " acquireRetryAttempts" value = " 2" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.hzu.mapper" />
</ bean>
</ beans>
6.2 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456
6.3 spring-service
<?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: 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/context https://www.springframework.org/schema/context/spring-context.xsd" >
< context: component-scan base-package = " com.hzu.service" />
< bean id = " BooksServiceImpl" class = " com.hzu.service.BooksServiceImpl" >
< property name = " booksMapper" ref = " booksMapper" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
</ beans>
7.springmvc整合
7.1 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> encoding</ 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> encoding</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
</ web-app>
7.2 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: 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/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" >
< mvc: annotation-driven/>
< mvc: default-servlet-handler/>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/jsp/" />
< property name = " suffix" value = " .jsp" />
</ bean>
< context: component-scan base-package = " com.hzu.controller" />
</ beans>
7.3 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"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd" >
< import resource = " classpath:spring-dao.xml" />
< import resource = " classpath:spring-service.xml" />
< import resource = " classpath:spring-mvc.xml" />
</ beans>
7.4 BookController
package com. hzu. controller;
import com. hzu. pojo. Books;
import com. hzu. service. BooksService;
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 ( "BooksServiceImpl" )
private BooksService booksService;
@RequestMapping ( "/allBook" )
public String queryAllBooks ( Model model) {
List< Books> books = booksService. queryAllBooks ( ) ;
model. addAttribute ( "books" , books) ;
return "allBook" ;
}
}
8 测试
8.1 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 首页</ title>
< style>
a {
text-decoration : none;
color : black;
font-size : 20px;
}
h3 {
width : 180px;
height : 50px;
margin : 100px auto;
text-align : center;
line-height : 50px;
background-color : deepskyblue;
border-radius : 5px;
}
</ style>
</ head>
< body>
< h3>
< a href = " ${pageContext.request.contextPath}/book/allBook" > 进入书籍页面</ a>
</ h3>
</ body>
</ html>
8.2 addBook.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>
<%--bootStrap美化页面--%>
< link rel = " stylesheet" href = " //netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" > </ head>
< body>
< div class = " bs-example" data-example-id = " panel-without-body-with-table" >
< div class = " panel panel-default" >
< div class = " panel-heading" > 书籍展示</ div>
< table class = " table" >
< 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>
</ body>
</ html>
8. 3 结果展示
8.4 目录结构