栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring事务管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring事务管理

先创建maven web

导入依赖


      com.baomidou
      mybatis-plus
      3.3.2
    
    
    
      commons-dbcp
      commons-dbcp
      1.4
    
    
      commons-pool
      commons-pool
      1.6
    
    
      mysql
      mysql-connector-java
      5.1.45
    
    
      org.springframework
      spring-tx
      5.1.9.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.1.9.RELEASE
    
    
      org.springframework
      spring-webmvc
      5.1.9.RELEASE
    
    
    
      javax.servlet
      servlet-api
      2.5
    

创建数据库mybatis并创建表customer

 在resources下创建database.properties添加数据库连接池配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=x5
jdbc.minIdle=45
jdbc.maxIdle=50
jdbc.initialSize=5
jdbc.maxActive=100
jdbc.maxWait=100
jdbc.removeAbandonedTimeout=180
jdbc.removeAbandoned=true

在resources下创建spring-mybatis.xml添加数据库连接池并配置spring扫描包、配置sqlsession工厂、mapper动态代理和开启spring注解式事务




    
    
    
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    
    
    
        
    
    
    
        
    

    
    
    
    


在resources下创建mapper文件夹并在mapper下创建customerMapper.xml




    
       select * from customer
    
    
        insert  into customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
    

创建Customer实体类添加get/set,toString方法

package com.zhang.hxci.pojo;

public class Customer {
    private Integer id;
    private String username;
    private String jobs;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getJobs() {
        return jobs;
    }

    public void setJobs(String jobs) {
        this.jobs = jobs;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", jobs='" + jobs + ''' +
                ", phone='" + phone + ''' +
                '}';
    }
}

创建dao层

package com.zhang.hxci.Dao;

import com.zhang.hxci.pojo.Customer;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface CustomerDao {
    public List query();

    public void add(Customer customer);
}

创建service层代码与dao层相同

创建serviceimpl

package com.zhang.hxci.Service.Impl;

import com.zhang.hxci.Dao.CustomerDao;
import com.zhang.hxci.Service.CustomerService;
import com.zhang.hxci.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerDao dao ;

    public List query() {
        List list = dao.query();
        return list;
    }

    @Transactional
    public void add(Customer customer) {
        dao.add(customer);
    }
}

创建controller层实现调用

package com.zhang.hxci.controller;

import com.zhang.hxci.Service.CustomerService;
import com.zhang.hxci.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
@Controller
public class CustomerController {
    @Autowired
    CustomerService service;

    public String query(){
        List list = service.query();
        System.out.println(list);
        return null;
    }

    public String add(Customer customer){
        service.add(customer);
        return null;
    }

}

最后创建测试类测试

package com.zhang.hxci;

import com.zhang.hxci.controller.CustomerController;
import com.zhang.hxci.pojo.Customer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
        CustomerController controller = (CustomerController)context.getBean("customerController");
        Customer customer =new Customer();
        customer.setUsername("张三");
        customer.setPhone("12222222");
        customer.setJobs("java");
        controller.add(customer);
        controller.query();
    }

}

运行成功

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号