目录
SSM框架整合
搭建数据库(MySQL)
基本项目的搭建
Mybatis层
Spring层
SpringMVC层
小结(MySQL数据库主键无法连续自增问题)
SSM框架整合
环境
- IDEA
- MySQL 5.5
- Tomcat 8.5.34
- Maven 3.6.0
搭建数据库(MySQL)
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项目,添加web支持
-
导入相关的maven依赖
<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> <!-- 数据库连接池 --> <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> </dependencies>
3.Maven资源过滤问题
<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>
-
建立基本结构和配置框架
-
com.inspur.pojo (持久层)
-
com.inspur.dao (Dao层)
-
com.inspur.service (业务处理层)
-
com.inspur.controller (业务控制层)
-
mybatis-config.xml (可在IDEA中设置xml模版) --> File | Settings | 编辑器 | 文件和代码模板
<?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> <!-- 别名设置 pojo:实体类上可加注解 @Alias("books")--> <!--可在Mapper.xml文件中使用时无需类的全限定名,默认为类名 --> <typeAliases> <package name=""/> </typeAliases> <!-- mapper文件路径 --> <mappers> <mapper resource=""></mapper> </mappers> </configuration>
-
applicationContext.xml (IDEA含有spring的初始化的配置文件)
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
-
-
Mybatis层
-
数据库配置文件db.properties (设为模版,同上), --> 在Spring配置文件中读取
jdbc.driver=com.mysql.jdbc.Driver #如果使用MySQL8.0+,需要增加一个时区的配置; &serverTimezone=Asia/Shanghai jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456
-
IDEA关联数据库(idea自带的数据库链接)
-
编写(完善配置信息 注意标签的顺序)MyBatis的核心配置文件(mybatis-config.xml)
<!-- 标准的 日志工厂实现 --> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 是否开启自动驼峰命名规则 (camel case)映射 --> <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>--> <!-- 开启全局缓存 --> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <package name="com.inspur.pojo"/> </typeAliases> <mappers> <mapper resource="com.inspur.dao.BookNapper.xml"/> </mappers>
-
编写数据库对应的实体类com.inspur.pojo.Books
使用Lombok插件
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.ibatis.type.Alias; import java.io.Serializable; /** * @author 12093 * @Created by 12093 */ @Data @AllArgsConstructor @NoArgsConstructor @Alias("books") //可在Mapper.xml文件中使用,无需类的全限定名 public class Books implements Serializable { //我们需要将实体类序列化(implements Serializable)!否则就会报错! private int bookID; private String bookName; private int bookCounts; private String detail; }
-
编写Dao层的Mapper接口
import com.inspur.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @author 12093 */ public interface BookMapper { //实现 增/删/改/查 // 增加一本书 int addBook(Books books); // 删除一本书 int deleteBookById(@Param("bookID") int id); // 更新一本书 int updateBookById(Books books); // 查询一本书 Books queryBookById(@Param("bookID") int id); // 查询全部的书 List<Books> queryAllBook(); //根据ID查询一本书 List queryBookByName(@Param("bookName") String bookName); }
-
编写接口对应的Mapper.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"> <!-- MyBatis映射文件 --> <!--namespace=绑定一个对应的Dao/Mapper接口--> <mapper namespace="com.inspur.dao.BookMapper"> <cache/> <!-- 设置此xml文件,使用二级缓存 --> <!--增加一个Book--> <insert id="addBook" parameterType="Books"> insert into books (bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <!--根据id删除一个Book--> <delete id="deleteBookById" parameterType="int"> delete from books where bookID = #{bookID} </delete> <!--更新Book--> <update id="updateBookById" parameterType="Books"> update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID}; </update> <!--根据id查询,返回一个Book--> <select id="queryBookById" resultType="Books"> select * from books where bookID = #{bookID} </select> <!--查询全部Book--> <select id="queryAllBook" resultType="Books"> select * from books; </select> <!--根据bookName查询,返回一个Book--> <select id="queryBookByName" resultType="Books"> select * from books where bookName like "%"#{bookName}"%" </select> </mapper>
-
编写Service层的接口和实现类
接口:
package com.inspur.service; import com.inspur.pojo.Books; import java.util.List; /** * @author 12093 * @Classname BookService * @Date 2020/11/8 22:58 * BookService:底下需要去实现,调用dao层 */ public interface BookService { // 增加一本Book int addBook(Books books); //根据id删除一本书Book int deleteBookById(int id); // 更新Book int updateBookById(Books books); //根据ID查询,返回一个Book Books queryBookById(int id); //查询全部Book,返回list集合 List<Books> queryAllBook(); //根据书名模糊查询一本书 List<Books> queryBookByName(@Param("bookName") String bookName); }
实现类:
```java
mport com.inspur.dao.BookMapper;
import com.inspur.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
//service调dao层: 组合Dao 可添加业务
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}
@Override
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
@Override
public int updateBookById(Books books) {
return bookMapper.updateBookById(books);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
@Override
public List<Books> queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
}
底层需求操作完毕