spring-mybatis框架结合数据库的增删改查

在项目中创建lib包,导入jar包

spring-mybatis框架结合数据库的增删改查_第1张图片

 建立项目结构为

spring-mybatis框架结合数据库的增删改查_第2张图片

 创建数据库

#使用数据库
use mydb;

#创建表
create table car(
cid int primary key auto_increment,
cnumber varchar(20),
type varchar(20),
owner varchar(20),
phone varchar(11),
mark varchar(50)
);

insert into car(cnumber,type,owner,phone,mark) values('豫A34G87','轿车','张三','13666666666','临时车');
insert into car(cnumber,type,owner,phone,mark) values('豫B38379','SUV','李四','1566666666','月卡');
insert into car(cnumber,type,owner,phone,mark) values('豫CR9703','卡车','王五','1888888888','年卡');

select * from car;

各层代码

创建bean层(实体类Car)

package com.chai.bean;

public class Car {
    public int cid;
    public String cnumber;
    public String type;
    public String owner;
    public String phone;
    public String mark;

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getCnumber() {
        return cnumber;
    }

    public void setCnumber(String cnumber) {
        this.cnumber = cnumber;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    @Override
    public String toString() {
        return "User{" +
                "cid=" + cid +
                ", cnumber='" + cnumber + '\'' +
                ", type='" + type + '\'' +
                ", owner='" + owner + '\'' +
                ", phone='" + phone + '\'' +
                ", mark='" + mark + '\'' +
                '}';
    }
}

dao层(接口)

package com.chai.dao;

import com.chai.bean.Car;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CarDao {
    //全查
    @Select("select * from car")
    List selectAll();
    //添加
    @Insert("insert into car(cnumber,type,owner,phone,mark)values(#{cnumber},#{type},#{owner},#{phone},#{mark});")
    int tianjia(Car car);
    //删除
    @Delete("delete from car where cid=#{cid}")
    int shanchu(int cid);
    //单查
    @Select("select * from car where cid=#{cid}")
    List dancha(int cid);
    //修改
    @Update("update car set cnumber=#{cnumber},type=#{type},owner=#{owner},phone=#{phone},mark=#{mark} where cid=#{cid};")
    int xiugai(Car car);


}

service层

接口

package com.chai.service;

import com.chai.bean.Car;


import java.util.List;

public interface CarService {
    //全查
    List findAll();
    //添加
    int tianjia(Car car);
    //删除
    int shanchu(int cid);
    //单查
    List dancha(int cid);
    //修改
    int xiugai(Car car);
}

继承类

 

package com.chai.service.impl;
import com.chai.bean.Car;
import com.chai.dao.CarDao;
import com.chai.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CarServiceimpl implements CarService {
    @Autowired
    CarDao carDao;
    //全查
    @Override
    public List findAll() {

        return carDao.selectAll();

    }
    //添加
    @Override
    public int tianjia(Car car) {

        return carDao.tianjia(car);

    }
    //删除
    @Override
    public int shanchu(int cid) {

        return carDao.shanchu(cid);

    }
    //单查
    @Override
    public List dancha(int cid) {

        return carDao.dancha(cid);

    }
    //修改
    @Override
    public int xiugai(Car car) {

        return carDao.xiugai(car);

    }


}

 spring.xml文件



    
    
    
    
        
        
        
        
    

    
    
        
        
        
        
        
        
    

    
    
        
    

    

 mybatis文件




    
        
    

test测试类

package com.chai.test;


import com.chai.bean.Car;
import com.chai.service.CarService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class CarTset {
    CarService carService;
    ClassPathXmlApplicationContext context=null;
    
    @Before
    public void intu(){

        context=new ClassPathXmlApplicationContext("spring.xml");
        carService=context.getBean(CarService.class);
    }
    //全查
    @Test
    public void selectAll(){
        List carList=carService.findAll();
        for (Car car : carList) {
            System.out.println(car);
        }
    }
    //新增
    @Test
    public void tianjia(){
        Car car=new Car();
        car.setCnumber("豫88888");
        car.setType("黑车");
        car.setOwner("柴七");
        car.setPhone("88888888888");
        car.setMark("黑卡");
        carService.tianjia(car);

    }
    //删除
    @Test
    public void shanchu(){

        carService.shanchu(2);

    }
    //单查
    @Test
    public void dancha(){
        List carList=carService.dancha(1);

        for (Car car : carList) {
            System.out.println(car);
        }

    }
    //修改
    @Test
    public void xiugai(){
        Car car=new Car();
        car.setCid(1);
        car.setCnumber("豫78767");
        car.setType("大巴");
        car.setOwner("赵六");
        car.setPhone("1785444577");
        car.setMark("土卡");
        carService.xiugai(car);
        List carList=carService.dancha(1);
        for (Car car1 : carList) {
            System.out.println(car1);
        }

    }




}

你可能感兴趣的:(mybatis,spring,数据库)