2020最新版SSM框架整合教程

网友投稿 678 2023-04-05

2020最新版SSM框架整合教程

2020最新版SSM框架整合教程

实验环境为:IDEA2020.1+mysql8.0.21+Tomcat9.0.36+Maven3.3.9

最终项目结构图:

一、搭建数据库环境

创建一个存放书籍数据的数据库表

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,'从进门到进牢');

生成表格:

二、基本环境搭建

1、创建maven项目,添加web支持

2、导入依赖

junit

junit

4.13

mysql

mysql-connector-java

8.0.21

com.mchange

c3p0

0.9.5.5

javax.servlet

servlet-api

2.5

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.5

org.mybatis

mybatis-spring

2.0.5

org.springframework

spring-webmvc

5.2.8.RELEASE

org.springframework

spring-jdb c

5.2.6.RELEASE

org.projectlombok

lombok

1.18.12

最后为了防止maven配置文件无法被导出或生效,加入以下代码

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

3、建立项目基本结构

在src/main/java目录下新建以下四个包,为后续实验准备

pojo:用来放实体类

dao:数据访问层,data access object

service:服务层,调用dao层

controller:控制层,调用service层

三、MyBatis层编写

1、编写数据库配置文件

在resource目录下新建database.properties

注意MySQL8.0以上要设置时区,最后加上serverTimezone=Asia/Shanghai

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai

jdbc.username=root

jdbc.password=200024

2、IDEA关联数据库

时区问题解决方案:https://jb51-/article/186512.htm

set global time_zone = '+8:00';

打开上述新建的数据表

3、编写MyBatis核心配置文件

在resource目录下新建mybatis-config.xml

数据源的配置,交给后续Spring去做

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4、编写pojo实体类

在pojo包下创建数据库表所对应的实体类Books,这里使用了lombok插件

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Books {

private int bookID;

private String bookName;

private int bookCounts;

private String detail;

}

5、编写dao层

1. 编写Mapper接口

在dao包下新建BookMapper接口,编写增删改查四种业务对应的方法

public interface BookMapper {

//增加一本书

int addBook(Books books);

//删除一本书

//@Param注解指定传入参数的名称

int deleteBookByID(@Param("bookID") int id);

//更新一本书

int updateBook(Books books);

//查询一本书

//@Param注解指定传入参数的名称

Books queryByID(@Param("bookID") int id);

//查询全部的书

List queryAllBooks();

}

2. 编写Mapper接口对应的Mapper.xml

一个Mapper.xml对应一个Mapper接口,要用namespace绑定上述接口

实现上述接口里的所有方法

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into ssmbuild.books(bookName,bookCount,detail)

values (#{bookName},#{bookCount},#{detail})

delete from ssmbuild.books where bookID=#{bookID}

update ssmbuild.books set

bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}

where bookID=#{bookID} ;

select * from ssmbuild.books where bookID=#{bookID}

select * from ssmbuild.books

然后到mybatis核心配置文件中注册上述mapper.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

6、编写service层

1. 编写service层的接口

在service包下新建BookService接口,同Mapper接口里的方法一致

package service;

import pojo.Books;

import java.util.List;

public interface BookService {

//增加一本书

int addBook(Books books);

//删除一本书

int deleteBookByID(int id);

//更新一本书

int updateBook(Books books);

//查询一本书

Books queryByID(int id);

//查询全部的书

List queryAllBooks();

}

2. 编写service层接口实现类

然后再service包下新建上述接口的实现类BookServiceImpl

service层用来调用dao层,所以内置私有属性为dao层的Mapper接口对象

package service;

import pojo.Books;

import java.util.List;

public interface BookService {

//增加一本书

int addBook(Books books);

//删除一本书

int deleteBookByID(int id);

//更新一本书

int updateBook(Books books);

//查询一本书

Books queryByID(int id);

//查询全部的书

List queryAllBooks();

}

四、Spring层编写

1、Spring整合dao层

在resource目录下新建spring-dao.xml

关联数据库配置文件database.properties,要引入context约束

配置MyBatis数据源,这里使用第三方的c3p0,还可以附加一些私有属性

创建sqlSessionFactory,在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建,要配置两个重要属性

configLocation绑定MyBatis核心配置文件

dataSource指定数据源(必要)

配置自动扫描包dao,动态实现了dao层接口可以注入到Spring容器中

(在原来我们是创建sqlSessionTemplate对象,然后再创建一个Mapper接口实现类,其中内置sqlSessionTemplate私有对象,通过该对象进行操作)

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

2、Spring整合service层

在resource目录下新建spring-service.xml

配置扫描service包,使该包下的注解生效

将所有业务类注入到Spring中

配置声明式事务,需要注入数据源

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

五、SpringMVC层编写

1、编写spring-mvc.xml

自动扫描包

过滤静态资源

支持mvc注解驱动

视图解析器

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

2、Spring配置文件整合

applicationContext.xml导入上述配置文件,作为整体的配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

3、配置web.xml

注册DispatcherServlet,需要绑定SpringMVC配置文件,这里一定要绑定整体的配置文件applicationContext.xml,并设置启动级别

增加乱码过滤

设置session过期时间

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

4、编写Controller

再controller包下新建BookController类

@Controller

@RequestMapping("/book")

public class BookController {

//controller层调用service层

@Autowired

@Qualifier("BookServiceImpl")

private BookService bookService;

//查询全部书籍,并且返回到一个页面进行显示

@RequestMapping("/allBooks")

public String list(Model model) {

List books = bookService.queryAllBooks();

model.addAttribute("list", books);

return "allBooks";

}

}

5、编写视图层

在web/WEB-INF/目录下新建jsp包,用来存放我们自定义视图页面

1. 编写index.jsp

链接跳转到我们自定的展示所有书籍页面allBooks.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

进入书籍页面

2. 编写展示所有书籍页面allBooks.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

${list}

6、运行测试

配置Tomcat启动测试,记得添加lib目录,否则Tomcat启动不来

启动Tomcat后,默认进入的index.jsp

然后我们点击超链接

成功显示了我们的所有书籍!

到此位置,SSM整合项目到此结束,后续大家可以自己实现相关业务!!!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:电脑如何打开小程序功能(电脑如何打开小程序功能键)
下一篇:Idea自动生成Entity实现过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~