Mybatis注解开发实现DML语句的功能-----Mybatis框架

package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;

public class Test
{
    @org.junit.Test
    public void update()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(52L,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        mapper.update(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void deleteById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(49L);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void TestInsert()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(null,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        int count = mapper.insert(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;

public class Test
{
    @org.junit.Test
    public void update()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(52L,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        mapper.update(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void deleteById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(49L);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void TestInsert()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(null,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        int count = mapper.insert(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

public interface CarMapper {
    long countByExample(CarExample example);
    @Update("update t_car set car_num = #{carNum},brand = #{brand},guide_price = #{guidePrice}," +
            "produce_time = #{produceTime},car_type = #{carType} where id = #{id}")
    int update(Car car);
    int deleteByExample(CarExample example);
    @Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);
    int deleteByPrimaryKey(Long id);
    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}

package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

public interface CarMapper {
    long countByExample(CarExample example);
    @Update("update t_car set car_num = #{carNum},brand = #{brand},guide_price = #{guidePrice}," +
            "produce_time = #{produceTime},car_type = #{carType} where id = #{id}")
    int update(Car car);
    int deleteByExample(CarExample example);
    @Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);
    int deleteByPrimaryKey(Long id);
    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}
package com.powernode.mybatis.pojo;

public class Car {
    private Long id;

    private String carNum;

    private String brand;

    private Long guidePrice;

    private String produceTime;

    private String carType;

    public Car(Long id, String carNum, String brand, Long guidePrice, String produceTime, String carType) {
        this.id = id;
        this.carNum = carNum;
        this.brand = brand;
        this.guidePrice = guidePrice;
        this.produceTime = produceTime;
        this.carType = carType;
    }

    public Car() {
    }

    public Long getId() {
        return id;
    }

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

    public String getCarNum() {
        return carNum;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum == null ? null : carNum.trim();
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand == null ? null : brand.trim();
    }

    public Long getGuidePrice() {
        return guidePrice;
    }

    public void setGuidePrice(Long guidePrice) {
        this.guidePrice = guidePrice;
    }

    public String getProduceTime() {
        return produceTime;
    }

    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime == null ? null : produceTime.trim();
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType == null ? null : carType.trim();
    }
}

package com.powernode.mybatis.pojo;

public class Car {
    private Long id;

    private String carNum;

    private String brand;

    private Long guidePrice;

    private String produceTime;

    private String carType;

    public Car(Long id, String carNum, String brand, Long guidePrice, String produceTime, String carType) {
        this.id = id;
        this.carNum = carNum;
        this.brand = brand;
        this.guidePrice = guidePrice;
        this.produceTime = produceTime;
        this.carType = carType;
    }

    public Car() {
    }

    public Long getId() {
        return id;
    }

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

    public String getCarNum() {
        return carNum;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum == null ? null : carNum.trim();
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand == null ? null : brand.trim();
    }

    public Long getGuidePrice() {
        return guidePrice;
    }

    public void setGuidePrice(Long guidePrice) {
        this.guidePrice = guidePrice;
    }

    public String getProduceTime() {
        return produceTime;
    }

    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime == null ? null : produceTime.trim();
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType == null ? null : carType.trim();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值