Spring SpringMVC,Spring整合MyBatis 事务配置的详细流程
整合思路
(1)SSM是什么?
Spring,SpringMVC,Mybastis
(2)思路
搭建整合的环境,初始化环境
搭建Spring环境,配置完成并测试(service层)
再使用Spring整合MyBatis框架,并测试(Dao层)
最后使用Spring整合SpringMVC框架,并测试(web层)
SSM搭建环境
(1)数据库创建ssm
(2)创建maven工程
(3)git(创建.gitignore来过滤不用提交的文件)
(4)依赖框架
(5)log4j.properties
数据库准备
createdatabasessm; usessm; createtableperson( idintprimarykeyauto_increment, `name`varchar(20), passwordvarchar(20), moneydouble );
pom.xml
UTF-8 1.8 1.8 5.2.9.RELEASE 1.6.6 1.2.12 5.1.6 3.4.5 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 test mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 c3p0 c3p0 0.9.1.2 jar compile
log4j.properties
#SetrootcategoryprioritytoINFOanditsonlyappendertoCONSOLE.
#log4j.rootCategory=INFO,CONSOLEdebuginfowarnerrorfatal
log4j.rootCategory=debug,CONSOLE,LOGFILE
#SettheenterpriseloggercategorytoFATALanditsonlyappendertoCONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL,CONSOLE
#CONSOLEissettobeaConsoleAppenderusingaPatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601}%-6r[%15.15t]%-5p%30.30c%x-%m\n
#LOGFILEissettobeaFileappenderusingaPatternLayout.
#输出到控制台
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
#输出到日志文件
log4j.appender.LOGFILE.File=E:\logFile\SSM\ssm.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601}%-6r[%15.15t]%-5p%30.30c%x-%m\n
搭建Spring环境,配置完成并测试(service层)
思路
(1)编写业务类调用测试逻辑
》TestPersonService
》IPersonServicePersonServiceImpl
》Person
(2)applicationContext.xml
(3)配置组件扫描–验证IOC
(4)配置哪些不扫描
(5)验证DI
》IPersonDaoPersonDaoImpl
TestPersonService
packagecom.zx.service;
importcom.zx.domain.Person;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
importjava.util.ArrayList;
importjava.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
publicclassTestPersonService{
@Autowired
IPersonServicepersonService;
@Test
publicvoidtest(){
Personperson=newPerson(1,"rose","123456",1000.00);
Listlist=personService.findAll();
System.out.println(list);
personService.save(person);
}
}
IPersonService
packagecom.zx.service;
importcom.zx.domain.Person;
importjava.util.List;
publicinterfaceIPersonService{
ListfindAll();
voidsave(Personperson);
voidsaves(ListpersonList);
}
PersonServiceImpl
packagecom.zx.service;
importcom.zx.dao.IPersonDao;
importcom.zx.dao.PersonDaoImpl;
importcom.zx.domain.Person;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
importjava.util.List;
@Service
publicclassPersonServiceImplimplementsIPersonService{
@Qualifier("IPersonDao")
@Autowired
privateIPersonDaopersonDao;
@Override
publicListfindAll(){
Listlist=personDao.findAll();
System.out.println(list);
returnlist;
}
@Override
publicvoidsave(Personperson){
personDao.save(person);
}
@Override
publicvoidsaves(ListpersonList){
for(inti=0;i
Person
packagecom.zx.domain;
publicclassPerson{
privateintid;
privateStringname;
privateStringpassword;
privatedoublemoney;
省略....
applicationContext.xml
Spring整合Mybatis
配置Mybatis(原来没用spring的)
)
(1)SqlMapConfig.xml
》》指定四大信息:账号密码ip端口
》》指定domain别名
》》指定映射文件
(2)编写测试
》》保存
》》查询
SqlMapConfig.xml
TestMyBatis
publicclassTestMyBatis{
privateSqlSessionsession;
@Before
publicvoidinit(){
//加载配置文件
InputStreamin=TestMyBatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory对象
SqlSessionFactoryfactory=newSqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
session=factory.openSession();
}
@After
publicvoiddestory(){
session.commit();
session.close();
}
@Test
publicvoidtest01(){
//最核心对象是session
//System.out.println(session);
//Mybastis的特点是sql与代码是分开的,需要映射文件
IPersonDaodao=session.getMapper(IPersonDao.class);
Listlist=dao.findAll();
System.out.println(list);
}
@Test
publicvoidtest02(){
//最核心对象是session
IPersonDaodao=session.getMapper(IPersonDao.class);
dao.save(newPerson("tony",200.00));
}
}
IPersonDao.xml
select*fromperson;
insertintoperson(name,money)values(#{name},#{money})
Spring整合Mybatis
(1)Spring整合MyBatis需要添加整合包
(2)什么是mybatis-spring
MyBatis-Spring会帮助你将MyBatis代码无缝地整合到Spring中。它将允许MyBatis参与到Spring的事务管理之中,创建映射器mapper和SqlSession并注入到bean中
不需要调用session.getMapper(IpersonDao.class)
session.commit()
session.close()
(在测试Spring整合Mybatis时先不给DepartmentImpl加注解)
pom.xml
org.mybatis
mybatis-spring
1.3.0
applicationContext.xml
将SqlMapConfig.xml的数据配置到spring中
TestPersonDao
packagecom.zx.dao;
importcom.zx.domain.Person;
importorg.apache.ibatis.session.SqlSession;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.jdbc.Sql;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
publicclassTestPersonDao{
privateSqlSessionsqlSession;
@Qualifier("IPersonDao")
@Autowired
IPersonDaopersonDao;
@Test
publicvoidtest01(){
System.out.println(personDao);
/*Listlist=personDao.findAll();
System.out.println(list);*/
personDao.save(newPerson(3,"jackma","123456",12222222.00));
}
}
Spring管理事务
(1)表达式设置有哪些serivce方法需要事务管理(2)通知设置增删改业务方法都要事务管理具体对应的事务
applicationContext.xml
TestPersonService
@Test
publicvoidtest02(){
ListpersonList=newArrayList<>();
personList.add(newPerson("jack",100.00));
personList.add(newPerson("rose",200.00));
personList.add(newPerson("tony",300.00));
personService.saves(personList);
}
使用Spring整合SpringMVC框架,并测试(web层)
(1)web.xml中配置前端控制器DispatcherServlet
SpringMVC的核心就是DispatcherServlet,DispatcherServlet实质也是一个HttpServlet。DispatcherSevlet负责将请求分发,所有的请求都有经过它来统一分发。
(2)web.xml中配置编码过滤器CharacterEncodingFilter
在SpringMVC中,框架直接给我们提供了一个用来解决请求和响应的乱码问题的过滤器CharacterEncodingFilter
(3)web.xml中配置编码监听器ContextLoaderListener
web.xml中的配置文件中ContextLoaderListener作为监听器,会监听web容器相关事件,在web容器启动或者关闭时触发执行响应程序
web.xml配置DispatcherServlet,CharacterEncodingFilter,ContextLoaderListener
ArchetypeCreatedWebApplication
contextConfigLocation
classpath:applicationContext.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
springmvc.xml
(1)springmvc中配置视图解析器,组件扫描,注解驱动
(2)配置springmvc对资源文件的放行
(3)编写一个PersonController测试
(4)编写一个list.jsp页面进行展示数据
PersonController
@Controller
@RequestMapping("/person")
publicclassPersonController{
@Autowired
privateIPersonServicepersonService;
@RequestMapping(path="/list",method=RequestMethod.GET)
publicStringlist(Modelmodel){
//显示所有的person数据
Listlist=personService.findAll();
System.out.println("list()list="+list);
//数据放在Model对象,由Model传给页面
model.addAttribute("list",list);//参1key参2value
return"list";
}
}
list.jsp
<%@pagecontentType="text/html;charset=UTF-8"language="java"isELIgnored="false"%>
<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
Title
${person.id}
${person.name}
${person.money}
到此这篇关于SpringSpringMVC,Spring整合MyBatis事务配置的详细流程的文章就介绍到这了,更多相关SSM整合内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!