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

SpringBoot+Mybatis

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

SpringBoot+Mybatis

创建数据库mybatis,并在其中创建表customer和student。

customer表:

 student表:

 创建SpringBoot工程。

 创建完工程后更改为本地的maven。

 更改完后导入依赖。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hxic
    hb
    0.0.1-SNAPSHOT
    hb
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            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-starter-test
            test
        
    

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


在application.properties添加数据库连接和mapper扫描。

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

spring.web.resources.static-locations= classpath:/templates/,classpath:/templates/static/


 创建Student和Customer实体类(一定要和数据库字段名一致)。

Student类:

package com.hxic.hb.pojo;

public class Student {
    private Integer id;
    private String name;
    private String sno;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }
}

 Customer类:

package com.hxic.hb.pojo;

public class Customer {
    private Integer id;
    private String sname; // s_name,sName,s
    private String jobs;
    private String phone;

    public Integer getId() {
        return id;
    }

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

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    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;
    }


}

创建CustomerDao和StudentDao层。 

CustomerDao:

package com.hxic.hb.dao;

import com.hxic.hb.pojo.Customer;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CustomerDao {
    // dao ---> mapper.xml
    public List query(@Param("customer") Customer customer, @Param("pageNo") Integer pageNo);

    public void add(Customer customer);
}

StudentDao:

package com.hxic.hb.dao;

import com.hxic.hb.pojo.Student;

public interface StudentDao {

    public void add(Student student);
}

在resources下创建mapper文件夹并在下面创建studentMapper.xml和customerMapper.xml文件添加sql语句(这里加入了if判断可以根据姓名或者职业查询实现条件查询)。

studentMapper.xml:



         


    
      insert  into  student (id,name ) values (#{id},#{name})
    

customerMapper.xml:



         

    
       select * from customer
       
           
            
                and  sname=#{customer.sname}
            
            
               and jobs =#{customer.jobs}
            
            
               limit 0,#{pageNo}
            

        
    

  
    insert  into  customer (username,jobs,phone) values (#{sname},#{jobs},#{phone})
  

创建service层(这里把student和customer同时使用一个添加)。

package com.hxic.hb.service;

import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;

import java.util.List;

public interface CustomerService {
    public List query(Customer customer, Integer pageNo);
    public void add(Customer customer, Student student);
}

创建service的接口实现类(CustomerServiceImpl)(这里的@Transactional是spring提供的事务管理如果不使用这个数据就会出现添加错误)(这里调用的两个dao可能会出现红线但不会报错)。

package com.hxic.hb.service.impl;

import com.hxic.hb.dao.CustomerDao;
import com.hxic.hb.dao.StudentDao;
import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;
import com.hxic.hb.service.CustomerService;
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;

    @Autowired
    StudentDao studentDao ;
    public List query(Customer ci,Integer pageNo){
        //List list = dao.query();
        return dao.query(ci,pageNo);
    }

    @Transactional
    public void add(Customer customer, Student student) {
        dao.add(customer);
        System.out.println(1/0); //模拟异常
        studentDao.add(student);
    }
}

创建Controller层(@RestController是让controller不进入视图解析器返回字符串可以不使用@RestController要想让他不返回字符串就使用@Controller如果不想改动@Controller 可以在想要返回字符串的方法上加@ResponseBody)。

package com.hxic.hb.contoller;

import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;
import com.hxic.hb.service.CustomerService;
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 CustomerController {
    //CustomerService service = new CustomerServiceImpl();

    @Autowired
    CustomerService service;

    // 传递一个参数  参数名称 和 pojo类的属性名 和 数据库的列名一致
    //1 如果接收的参数名是 pojo的属性名,那么, dao层接口 直接有#{}或者${} 传递到mapper.xml
    //2 如果接收的参数名不在pojo的属性中 那么通过@Param("customer") 将值传递到mapper.xml 中
    @RequestMapping("query")
    public List  query(Customer customer,Integer pageNo){ // spring boot controller方法返回值看可以自动转json,ssm框架不行

        List a=service.query(customer,pageNo);

        return a;
    }

    @RequestMapping("add")
    public String add(Customer customer, Student student){
        service.add(customer,student);
        return null;
    }
}

在SpringBoot自带的启动类上加@MapperScan("xxxx.xxxx.xxx")扫描mapper接口(注意:SpringbootMybatisApplication是在创建项目时自动生成的他会创建在你自定义的包下)。

package com.hxic.hb;

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

@SpringBootApplication
@MapperScan({"com.hxic.**.dao"})
public class HbApplication {
    //spring boot == spring + spring mvc
    public static void main(String[] args) {
        SpringApplication.run(HbApplication.class, args);
    }
}

点击启动后在浏览器中输入网址:http://localhost:8080/query

就可以输出你数据库中的数据

想要添加数据就把路径后的query改为add并在后面拼接添加的值即可

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

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

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