-
环境
IDEA
Mysql 5.7.19
tomcat 8
maven 3.8.4
-
数据库设计
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
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 DEFAULT CHARSET = utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`) VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
-
新建一个maven项目
-
pom.xml导入依赖
-
Junit测试
-
mybatis依赖,mybatis连接spring依赖
-
spring依赖
-
servlet,jsp
-
数据库连接池,JDBC
别忘了设置静态资源过滤
<?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>org.example</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<!--1. 添加依赖-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--版本要和mysql的版本匹配起来,mysql是5开头的话这个也要以5开头,如果是8开头。。。-->
<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.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</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.13</version>
</dependency>
<!-- 版本和mybatis,spring的版本有关 -->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--2、静态资源过滤-->
<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>
</project>
-
项目结构
-
整合mybatis层
6.1 配置文件
(1)mybatis核心配置文件
mybatis核心配置文件只干两件事:
-
起别名
-
注册mapper.xml文件
db.properties的引入交给spring去干
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--1、别名-->
<typeAliases>
<package name=""/>
</typeAliases>
<!--2、注册mapper.xml -->
<mappers>
</mappers>
</configuration>
(2)spring核心配置文件
(3)db.properties
如果用的mysql是8版本及以上,url参数里还要加上上海的时区
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_mybatis?useSSL=false
username=root
password=123456
6.2 pojo
注意,参数名最好和数据库字段名保持一致
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private String bookCounts;
private String detail;
}
6.3 dao
BooksMapper.java
public interface BooksMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookID") int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(@Param("bookID") int id);
//查询全部的书
List<Books> queryAllBook();
}
BooksMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper>
<insert id="addBook" parameterType="pojo.Books">
insert into books(bookName,bookCounts,detail)
values (#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBookById" parameterType="int">
delete from 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="pojo.Books">
select * from books
where bookID=#{bookID}
</select>
<select id="queryAllBook" resultType="pojo.Books">
select * from books
</select>
</mapper>
然后立马把这个mapper.xml文件注册到mybatis-config.xml里面去。
6.4 service
BookService.java
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部的书
List<Books> queryAllBook();
}
BookServiceImpl.java
public class BookServiceImpl implements BookService{
//service层组合dao层
private BooksMapper booksMapper;
public void setBooksMapper(BooksMapper booksMapper){
this.booksMapper = booksMapper;
}
//-----------------------
public int addBook(Books books) {
return booksMapper.addBook(books);
}
public int deleteBookById(int id) {
return booksMapper.deleteBookById(id);
}
public int updateBook(Books books) {
return booksMapper.updateBook(books);
}
public Books queryBookById(int id) {
return booksMapper.queryBookById(id);
}
public List<Books> queryAllBook() {
return booksMapper.queryAllBook();
}
}
-
整合spring层
-
整合springMVC
新建spring-dao.xml
- 关联数据库配置文件db.properties
- 配置第三方连接池(可以用spring自带的,但是这里我们用第三方的C3P0)
- sqlSession
- 【可选】配置dao接口扫描包,动态实现dao接口注入spring容器中
新建spring-service.xml