添加依赖
org.mybatis mybatis 3.5.10
mysql mysql-connector-java 5.1.37
ch.qos.logback logback-examples 1.2.11
junit junit 4.13.2 test
引入之前文件
CarMapper.xml
insert into t_car(id, car_num, brand, guide_price, produce_time, car_type)values (null, #{car_num}, #{brand}, #{guide_price}, #{produce_time}, #{car_type})
测试
@Testpublic void testInsertCar(){SqlSession sqlSession = SqlSessionUtil.openSession();Map map = new HashMap<>();map.put("car_num", 1111);map.put("brand", "比亚迪汉");map.put("guide_price", 10.0);map.put("produce_time", 2020-11-11);map.put("car_type", "电车");int count = sqlSession.insert("insertCar", map);System.out.println(count);sqlSession.commit();sqlSession.close();}
Car.java
package com.sdnu.mybatis.pojo;public class Car {private Long id;private String carNum;private String brand;private Double guidePrice;private String produceTime;private String carType;public Car() {}public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {this.id = id;this.carNum = carNum;this.brand = brand;this.guidePrice = guidePrice;this.produceTime = produceTime;this.carType = carType;}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;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public Double getGuidePrice() {return guidePrice;}public void setGuidePrice(Double guidePrice) {this.guidePrice = guidePrice;}public String getProduceTime() {return produceTime;}public void setProduceTime(String produceTime) {this.produceTime = produceTime;}public String getCarType() {return carType;}public void setCarType(String carType) {this.carType = carType;}@Overridepublic String toString() {return "Car{" +"id=" + id +", carNum='" + carNum + '\'' +", brand='" + brand + '\'' +", guidePrice=" + guidePrice +", produceTime='" + produceTime + '\'' +", carType='" + carType + '\'' +'}';}
}
CarMapper.xml
insert into t_car(id, car_num, brand, guide_price, produce_time, car_type)values (null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType})
测试类
@Testpublic void testInsertCarByPojo(){SqlSession sqlSession = SqlSessionUtil.openSession();Car car = new Car(null, "1001", "比亚迪泰", 12.00, "2000-02-09", "电车");int count = sqlSession.insert("insertCar", car);System.out.println(count);sqlSession.commit();sqlSession.close();}
使用POJO对象传递值的话,#{}这个大括号中写的是get方法的方法名去掉get,然后将剩下的单词首字母小写,然后放进去。
例如:getUsername() --> #{username}例如:getEmail() --> #{email}
CarMapper.xml
delete from t_car where id = #{id}
测试类
@Testpublic void testDeleteCar(){SqlSession sqlSession = SqlSessionUtil.openSession();int count = sqlSession.delete("deleteById", 10);System.out.println(count);sqlSession.commit();sqlSession.close();}
CarMapper.xml
update t_car setcar_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType}whereid=#{id}
测试类
@Testpublic void updateCarById(){SqlSession sqlSession = SqlSessionUtil.openSession();Car car = new Car(4L, "11111", "奔驰", 112.00, "2020-09-09", "燃油车");int count = sqlSession.update("updateById",car);System.out.println(count);sqlSession.commit();sqlSession.close();}
CarMapper.xml
测试类
@Testpublic void selectById(){SqlSession sqlSession = SqlSessionUtil.openSession();Object car = sqlSession.selectOne("selectById", 3);System.out.println(car);sqlSession.commit();sqlSession.close();}
CarMapper.xml
测试类
@Testpublic void selectAll(){SqlSession sqlSession = SqlSessionUtil.openSession();List car = sqlSession.selectList("selectAll");for (Car car1 : car){System.out.println(car1);}sqlSession.close();}
在SQL Mapper配置⽂件中标签的namespace属性可以翻译为命名空间,这个命名空间主要是为了防⽌sqlId冲突的。