手把手教你整合SSM框架
发布日期:2021-06-29 15:52:03 浏览次数:3 分类:技术文章

本文共 7681 字,大约阅读时间需要 25 分钟。

文章目录

SSM框架整合

SSM框架(Spring+Spring MVC + MyBatis)框架是当前后端开发较为流行的框架集合,在整合时应该按照Spring整合其他框架的原则进行,在整合测试时,先测试每个框架单独是否配置成功,再测试整合是否成功。

整合示例

示例使用c3p0作为数据源

日志使用log4j

pom依赖

javax.servlet
javax.servlet-api
3.1.0
org.mybatis
mybatis
3.5.5
org.mybatis
mybatis-spring
2.0.5
log4j
log4j
1.2.17
mysql
mysql-connector-java
8.0.15
jstl
jstl
1.2
javax.servlet.jsp
jsp-api
2.0
javax.annotation
javax.annotation-api
1.3.1
org.springframework
spring-context
${spring-version}
org.springframework
spring-core
${spring-version}
org.springframework
spring-beans
${spring-version}
org.springframework
spring-aop
${spring-version}
org.springframework
spring-expression
${spring-version}
org.springframework
spring-jdbc
${spring-version}
org.springframework
spring-tx
${spring-version}
org.springframework
spring-test
${spring-version}
org.aspectj
aspectjweaver
1.8.7
org.springframework
spring-webmvc
${spring-version}
org.springframework
spring-web
${spring-version}
com.mchange
c3p0
0.9.5.2
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.5
junit
junit
4.12
test

配置文件

log4j

log4j.properties

### #配置根Logger ###log4j.rootLogger=debug,stdout### 输出到控制台 ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[%d{yyy-MM-dd HH\:mm\:ss}] [%5p] %c{1}\:%L - %m%n

mybatis

MyBatis的很多配置都在Spring配置文件中进行,只剩下不常用的配置在单独配置文件中进行

mybatis-config.xml

spring

spring配置文件中配置spring和mybatis相关配置

spring.xml

其中,和数据库有关的配置单独抽取出来

dbconfig.properties

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8jdbc.username=rootjdbc.password=root

Spring MVC

spring-servlet.xml

web.xml

为了让web服务启时Spring容器可以跟随启动,需要在web.xml中配置

Archetype Created Web Application
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
1
spring
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
/index.jsp

事务控制

在Spring配置文件中已经开启了Spring的事务控制器和基于注解的事务管理

在Service类中进行事务控制即可

AccountService.java

package com.console.service;import com.console.mybatis.bean.Account;import com.console.mybatis.mapper.AccountMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.management.RuntimeErrorException;@Service@Transactional(rollbackFor = Exception.class)public class AccountService {
@Autowired private AccountMapper accountMapper; /** * 转账 * @param from * @param to * @param money */ public void transfer(Account from,Account to,Double money){
if (from.getMoney() < money){
throw new RuntimeException("余额不足,无法转账"); } from.setMoney(from.getMoney() - money); to.setMoney(to.getMoney() + money); accountMapper.update(from); //假定这里发生异常,测试事务是否成功 int i = 1 / 0; accountMapper.update(to); } public Account getById(Integer id){
Account account = accountMapper.queryById(id); return account; }}

AccountServiceTest.java

package com.console.service;import com.console.mybatis.bean.Account;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:spring.xml")public class AccountServiceTest {
@Autowired private AccountService accountService; @Before public void setUp() throws Exception {
} @Test public void transfer() {
Account account1 = accountService.getById(2); Account account2 = accountService.getById(3); try {
double money = 500; System.out.println(account1); System.out.println(account2); accountService.transfer(account1, account2, money); System.out.println("------------"); System.out.println(account1); System.out.println(account2); }catch (Exception e){
account1=accountService.getById(2); account2=accountService.getById(3); System.out.println("发生错误,重新获取账户信息"); System.out.println(account1); System.out.println(account2); } }}

事务控制时,在业务层获取到的异常事务控制之后,调用业务方法的方法内同样可以获取到

示例项目

项目仓库地址 :https://gitee.com/ZhijianCao_admin/ssm.git

转载地址:https://console.blog.csdn.net/article/details/108749360 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:自己造个简单数据校验的注解@Value和@Mail
下一篇:一个案例教你理解Spring面向切面编程(Spring Aop)

发表评论

最新留言

不错!
[***.144.177.141]2024年04月26日 15时17分38秒