Spring SpringMVC,Spring整合MyBatis 事务配置的详细流程

网友投稿 231 2022-11-16


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

数据库准备

create database ssm;

use ssm;

create table person(

id int primary key auto_increment,

`name` varchar(20),

password varchar(20),

money double

);

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

# Set root category priority to INFO and its only appender to CONSOLE.

#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal

log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.

log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.

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

# LOGFILE is set to be a File appender using a PatternLayout.

# 输出到控制台

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

》IPersonService PersonServiceImpl

》Person

(2)applicationContext.xml

(3)配置组件扫描 – 验证IOC

(4)配置哪些不扫描

(5)验证DI

》IPersonDao PersonDaoImpl

TestPersonService

package com.zx.service;

import com.zx.domain.Person;

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 java.util.ArrayList;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class TestPersonService {

@Autowired

IPersonService personService;

@Test

public void test(){

Person person=new Person(1,"rose","123456",1000.00);

List list= personService.findAll();

System.out.println(list);

personService.save(person);

}

}

IPersonService

package com.zx.service;

import com.zx.domain.Person;

import java.util.List;

public interface IPersonService {

List findAll();

void save(Person person);

void saves(List personList);

}

PersonServiceImpl

package com.zx.service;

import com.zx.dao.IPersonDao;

import com.zx.dao.PersonDaoImpl;

import com.zx.domain.Person;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class PersonServiceImpl implements IPersonService {

@Qualifier("IPersonDao")

@Autowired

private IPersonDao personDao;

@Override

public List findAll() {

List list=personDao.findAll();

System.out.println(list);

return list;

}

@Override

public void save(Person person) {

personDao.save(person);

}

@Override

public void saves(List personList) {

for(int i=0;i

personDao.save(personList.get(i));

}

}

}

Person

package com.zx.domain;

public class Person {

private int id;

private String name;

private String password;

private double money;

省略....

applicationContext.xml

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

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/aop

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

http://springframework.org/schema/tx

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

Spring整合Mybatis

配置Mybatis(原来没用spring的)

)

(1)SqlMapConfig.xml

》》指定四大信息:账号密码ip端口

》》指定domain别名

》》指定映射文件

(2)编写测试

》》保存

》》查询

SqlMapConfig.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

TestMyBatis

public class TestMyBatis {

private SqlSession session;

@Before

public void init(){

// 加载配置文件

InputStream in = TestMyBatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");

// 创建SqlSessionFactory对象

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

// 创建SqlSession对象

session = factory.openSession();

}

@After

public void destory(){

session.commit();

session.close();

}

@Test

public void test01(){

//最核心对象是session

// System.out.println(session);

//Mybastis的特点是sql与代码是分开的,需要映射文件

IPersonDao dao = session.getMapper(IPersonDao.class);

List list = dao.findAll();

System.out.println(list);

}

@Test

public void test02(){

//最核心对象是session

IPersonDao dao = session.getMapper(IPersonDao.class);

dao.save(new Person("tony",200.00));

}

}

IPersonDao.xml

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

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

select * from person;

insert into person (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

package com.zx.dao;

import com.zx.domain.Person;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.jdbc.Sql;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class TestPersonDao {

private SqlSession sqlSession;

@Qualifier("IPersonDao")

@Autowired

IPersonDao personDao;

@Test

public void test01(){

System.out.println(personDao);

/* List list=personDao.findAll();

System.out.println(list);*/

personDao.save(new Person(3,"jackma","123456",12222222.00));

}

}

Spring管理事务

(1)表达式设置有哪些serivce方法需要事务管理(2)通知设置 增删改业务方法 都要事务管理 具体对应的事务

applicationContext.xml

TestPersonService

@Test

public void test02(){

List personList = new ArrayList<>();

personList.add(new Person("jack",100.00));

personList.add(new Person("rose",200.00));

personList.add(new Person("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

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

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页面进行展示数据

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

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

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

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

PersonController

@Controller

@RequestMapping("/person")

public class PersonController {

@Autowired

private IPersonService personService;

@RequestMapping(path="/list",method = RequestMethod.GET)

public String list(Model model){

//显示所有的person数据

List list = personService.findAll();

System.out.println("list() list= "+list);

//数据放在Model对象,由Model传给页面

model.addAttribute("list",list);//参1 key 参2 value

return "list";

}

}

list.jsp

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

personDao.save(personList.get(i));

}

}

}

Person

package com.zx.domain;

public class Person {

private int id;

private String name;

private String password;

private double money;

省略....

applicationContext.xml

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

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/aop

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

http://springframework.org/schema/tx

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

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

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/aop

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

http://springframework.org/schema/tx

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

Spring整合Mybatis

配置Mybatis(原来没用spring的)

)

(1)SqlMapConfig.xml

》》指定四大信息:账号密码ip端口

》》指定domain别名

》》指定映射文件

(2)编写测试

》》保存

》》查询

SqlMapConfig.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

TestMyBatis

public class TestMyBatis {

private SqlSession session;

@Before

public void init(){

// 加载配置文件

InputStream in = TestMyBatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");

// 创建SqlSessionFactory对象

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

// 创建SqlSession对象

session = factory.openSession();

}

@After

public void destory(){

session.commit();

session.close();

}

@Test

public void test01(){

//最核心对象是session

// System.out.println(session);

//Mybastis的特点是sql与代码是分开的,需要映射文件

IPersonDao dao = session.getMapper(IPersonDao.class);

List list = dao.findAll();

System.out.println(list);

}

@Test

public void test02(){

//最核心对象是session

IPersonDao dao = session.getMapper(IPersonDao.class);

dao.save(new Person("tony",200.00));

}

}

IPersonDao.xml

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

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

select * from person;

insert into person (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

package com.zx.dao;

import com.zx.domain.Person;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.jdbc.Sql;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class TestPersonDao {

private SqlSession sqlSession;

@Qualifier("IPersonDao")

@Autowired

IPersonDao personDao;

@Test

public void test01(){

System.out.println(personDao);

/* List list=personDao.findAll();

System.out.println(list);*/

personDao.save(new Person(3,"jackma","123456",12222222.00));

}

}

Spring管理事务

(1)表达式设置有哪些serivce方法需要事务管理(2)通知设置 增删改业务方法 都要事务管理 具体对应的事务

applicationContext.xml

TestPersonService

@Test

public void test02(){

List personList = new ArrayList<>();

personList.add(new Person("jack",100.00));

personList.add(new Person("rose",200.00));

personList.add(new Person("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

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

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页面进行展示数据

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

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

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

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

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

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

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

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

PersonController

@Controller

@RequestMapping("/person")

public class PersonController {

@Autowired

private IPersonService personService;

@RequestMapping(path="/list",method = RequestMethod.GET)

public String list(Model model){

//显示所有的person数据

List list = personService.findAll();

System.out.println("list() list= "+list);

//数据放在Model对象,由Model传给页面

model.addAttribute("list",list);//参1 key 参2 value

return "list";

}

}

list.jsp

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


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

上一篇:Java身份证号码校验工具类详解
下一篇:java 数组转list的两种方式
相关文章

 发表评论

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