本笔记是在学习狂神说的SpringMVC时创建。
个人博客 : Dexter
SSM整合步骤
1.环境
IDEA
JDK 1.8
Tomcat 9
Maven3.6
Mysql 5.7
数据库环境
创建图书表
CREATE DATABASE `ssmbuild` ;
CREATE TABLE `books` (
`bookID` int(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` varchar(100) NOT NULL COMMENT '书名',
`bookCounts` int(11) NOT NULL COMMENT '数量',
`detail` varchar(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
导入数据略......
2.整合开始
1.打开IDEA创建普通Maven项目并添加wed支持
2.添加相关依赖
junit
junit
4.12
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
org.projectlombok
lombok
RELEASE
compile
注意:静态资源过滤问题
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
3.创建基本结构
mybatis-config.xml
applicationContext.xml
4. Mybatis层
(1)创建实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
public Books()
{
}
}
(2)创建BookMapper接口
package com.chao.mapper;
import com.chao.pojo.Books;
import java.util.List;
public interface BookMapper {
//增加一本书
int addBook(Books book);
//删除一本书
int deleteBook(int id);
//修改一本书
int updateBook(Books book);
//查询一本书
Books queryBookById(int id);
//查询所有的书
List queryAllBook();
}
(2)创建BookMapper.xml
insert into ssmbuild.books(bookName, bookCounts, detail)
values(#{bookName},#{bookName},#{detail});
delete from ssmbuild.books
where bookID=#{id};
update ssmbuild.books set bookName=#{bookName},bookCounts=#{bookCounts}
,detail=#{detail} where bookID=#{bookID} ;
(3)数据库外部文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
(4)Mybatis核心文件
5.编写业务层
(1)创建Service接口
package com.chao.Service;
import com.chao.pojo.Books;
import java.util.List;
public interface BookService {
//增加一本书
int addBook(Books book);
//删除一本书
int deleteBook(int id);
//修改一本书
int updateBook(Books book);
//查询一本书
Books queryBookById(int id);
//查询所有的书
List queryAllBook();
}
(2)编写实现类
package com.chao.Service;
import com.chao.mapper.BookMapper;
import com.chao.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public int addBook(Books book) {
return bookMapper.addBook(book);
}
@Override
public int deleteBook(int id) {
return bookMapper.deleteBook(id);
}
@Override
public int updateBook(Books book) {
return bookMapper.updateBook(book);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List queryAllBook() {
return bookMapper.queryAllBook();
}
}
6.Spring整合
(1)Spring整合Dao层,创建Spring-Dao.xml
(2)Spring整合Service层
(3)Spring整合MVC层
(4)整合controller层
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
5)Spring配置整合文件applicationContext.xml
3.整合结束
下面可以进行视图层的编写!!!