springboot整合mybatis 事务管理

1.创建项目

springboot整合mybatis 事务管理_第1张图片

 springboot整合mybatis 事务管理_第2张图片

 springboot整合mybatis 事务管理_第3张图片

 写项目名字和项目地址

springboot整合mybatis 事务管理_第4张图片

 2.修改maven地址

springboot整合mybatis 事务管理_第5张图片

3.添加pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hxci
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            mysql
            mysql-connector-java
            5.1.45
        

        
            com.baomidou
            mybatis-plus
            3.3.2
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


4.创建application.properties配置文件

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=x5

mybatis.mapper-locations=classpath:mapper/*.xml

5.创建4个包

controller(控制层)

dao(放置 DAO 数据交互层处理类)

pojo(放置 Java 实体类)

service放置 Mapper 代理接口)

6.创建实体pojo类Student并为其添加get set和to string方法

package com.hxci.sy.pojo;

public class Student {
    private Integer id;
    private String username;
    private String clazz;

    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 getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }


}

7.写controller控制层

package com.hxci.sy.controller;

import com.hxci.sy.pojo.Student;
import com.hxci.sy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {

@Autowired
StudentService service;

@RequestMapping("query")
public List query(){// spring boot controller方法返回值看可以自动转json,ssm框架不行
    List list =service.query();
   return list;
}
    @RequestMapping("add")
public String add(Student student){
    service.add(student);
    return null;
}
    @RequestMapping("update")
    public String update(Student student){
        service.update(student);
        return null;
    }
   /* @RequestMapping("queryById")
    Student student = service.queryById(id);
        System.out.println(student);
        return student;
}     */
@RequestMapping("delete")
public void delete(Integer id){
    service.delete(id);
}
}

8.添加业务逻辑层StudentService

package com.hxci.sy.service;

import com.hxci.sy.pojo.Student;
import java.util.List;

public interface StudentService {
    public List query();

    void add(Student student);

    void update(Student student);

    Student queryById(Integer id);

    void delete(Integer id);
}

9.实现类impl

package com.hxci.sy.service.impl;

import com.hxci.sy.dao.StudentDao;

import com.hxci.sy.pojo.Student;
import com.hxci.sy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    StudentDao dao;
    @Override
    public List query(){
        List list= dao.query();
        return list;
    }
    @Override
    public void add (Student student){
        dao.add(student);
    }
    @Override
    public void update(Student student){
        dao.update(student);
    }
    @Override
    public Student queryById(Integer id){
        Student Byid = dao.queryById(id);
        return Byid;
    }
    @Override
    public void delete(Integer id){
        dao.delete(id);
    }
}

10.持久层dao

package com.hxci.sy.dao;

import com.hxci.sy.pojo.Student;
import java.util.List;

public interface StudentDao {
    public  List query();
    void add(Student student);

    public void update(Student student);

    public Student queryById(Integer id);

    public void delete(Integer id);
}

11.DemoApploication

package com.hxci;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan({"com.hxci.sy.dao"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

12.创建数据库

springboot整合mybatis 事务管理_第6张图片

 13.使用postman测试

springboot整合mybatis 事务管理_第7张图片

 springboot整合mybatis 事务管理_第8张图片

 14.项目列表

springboot整合mybatis 事务管理_第9张图片

 

你可能感兴趣的:(spring,boot,java,spring)