目录
一、ssm是什么?
1、基本概念
1.1、Spring
1.2、SpringMVC
1.3、MyBatis
二、步骤
1.创建maven项目并导入依赖
2.Mybatis层编写
一,配置
二,代码编写
3.spring层编写
4.SpringMVC层编写
至此,框架搭建基本结束! 后面就是一些简单的功能实现需要的代码编写。
1.编写controller层
列如实现查询功能:
2.前端构造
总结
SSM框架是Spring + Spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统。
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录
所需要的依赖:
1,junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring
junit
junit
4.13.2
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
4.0.1
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.3
provided
org.mybatis
mybatis
3.5.6
org.mybatis
mybatis-spring
2.0.6
com.mchange
c3p0
0.9.5.5
mysql
mysql-connector-java
8.0.25
org.springframework
spring-webmvc
5.3.9
org.springframework
spring-jdbc
5.3.9
2.Maven资源过滤设置(解决静态资源导出问题)
src/main/java
**/*.properties
**/*.xml
false
1.数据库配置文件database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
2.IDEA关连数据库导入对应的数据库
3.编写Mybatis核心配置文件
1.编写跟数据库对应字段的实体类;
2.在dao层编写对应操作的接口,以及~Mapper.xml配置文件去绑定每一个接口
insert into ssmbuild.books(bookName, detail, bookCounts)
values (#{bookName},#{detail},#{bookCounts});
delete from ssmbuild.books
where bookID=#{bookId}
update ssmbuild.books
set bookName=#{bookName},detail = #{detail},bookCounts = #{bookCounts}
where bookId = #{bookId}
3.编写业务层(调用dao层)以及实现类
package com.yy.service;
import com.yy.dao.BookMapper;
import com.yy.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService {
//service调用dao层:组合Dao
private BookMapper bookMapper;
//加一个set方法方便spring托管
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleteBookId(int id) {
return bookMapper.deleteBookId(id);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List queryAllBook() {
return bookMapper.queryAllBook();
}
}
1.spring整合dao层的主要配置文件编写
2.整合service层
1.添加web并进行配置web.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
2.mvc层整合配置文件
package com.yy.controller;
import com.yy.pojo.Books;
import com.yy.service.BookService;
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 {
//controller层调用service层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService ;
//查询全部的书籍,并且返回到一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List books = bookService.queryAllBook();
model.addAttribute("list",books);
return "allBook";
}
}
编写jsp文件。。。优化啥的不计~
实现的查询结果界面如图所示:
下面主要是说明一下在项目实现过程中出现的一些问题:
首先是数据库连接异常,或者数据库连接失败,大致如下:
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
。。。
。。。
。。。
。。。
代码太多不一一列举,主要是解决办法。首先我们要确认自己的mysql版本,如果是8.0以上的最好使用的数据库驱动的jar包与数据库版本相匹配,有时候很多情况都是因为版本不一致,导致读取底层数据库失败。列如小编的MySQL是8.0的,所以使用的驱动是8.0.25的,然后需要注意的是,如果使用的是Mysql8.0+,增加一个时区的配置:
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
例外,如多使用c3p0连接数据库连接不上时出现如下异常信息时:
一月 03, 2022 6:15:53 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
一月 03, 2022 6:15:54 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:18:33 -0800; debug? true; trace: 10]
一月 03, 2022 6:15:55 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hgek8xam8q10no6e8599|524d6d96, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hgek8xam8q10no6e8599|524d6d96, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
一月 03, 2022 6:16:25 下午 com.mchange.v2.resourcepool.BasicResourcePool
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@1e34cc74 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
java.sql.SQLException: Access denied for user 'young'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1176)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1163)
at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1908)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
一月 03, 2022 6:21:34 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
一月 03, 2022 6:21:35 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:18:33 -0800; debug? true; trace: 10]
一月 03, 2022 6:21:35 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hgek8xam8q8bf81o4hum0|524d6d96, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hgek8xam8q8bf81o4hum0|524d6d96, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai, maxAdministrativeTaskTime -> 0, maxConnectionAge -。。。
需要在我们的数据库配置文件中,在每个数据名前加入jdbc,即可避免:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
如果在项目使用tomcat运行过程中出现错误,我们可以通过多编写测试类来查找:
import com.yy.pojo.Books;
import com.yy.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookServiceImpl = (BookService) context.getBean("BookServiceImpl");
for (Books books : bookServiceImpl.queryAllBook()) {
System.out.println(books);
}
}
}
查看预期结果是否符合:
Books(bookID=1, bookName=Java, bookCounts=1, detail=从入门道放弃)
Books(bookID=2, bookName=MySQL, bookCounts=10, detail=从删库到跑路)
Books(bookID=3, bookName=Linux, bookCounts=3, detail=从进门到进牢)
额外补充一点,在maven工程中,idea一般不会自动把项目依赖包部署到web项目,需要我们手动导入。在WEB-INF目录中新建一个lib目录然后导入所有包,不然tomcat会启动报错,出现一个或多个筛选器失败。。!
若有不足之处,欢迎业界大佬点评留言~