CRUD(C:Create,R:Retrieve(查),U:Update,D:Delete)
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,'1003','丰田霸道',30.0,'2000-10-11','燃油车')
</insert>
这样写值写死到配置文件了,这个在实际开发中不能存在的,一定是前端的Form表单提交过来数据
将值传给sql语句
jdbc中sql语句insert into user(id,username) values(?,?)
prepareStatement.setString(1,zhangSan);
JDBC中采用的是?,mybatis中采用的是#{}
mybatis当中不能采用?占位符,要用#{}来做占位符
insert的时候?和#{}是等效的
#{}这里写key的值,如果key不存在,就是null
java中使用pojo类给sql语句传值,大括号内写pojo类的属性值
MYBATIS框架实质上是使用Get方法取出该值的
如果使用pojo传值的话,大括号内的写的是get方法的属性名把第一个字母改小写
getUserName()改为userName
通过XML解析取出需要的属性名字,拼接为getXX方法,通过反射机制执行方法
也就是说mybatis在底层给sql的?传值的时候调用了pojo对象的getXX方法
CRUD(C:Create,R:Retrieve(查),U:Update,D:Delete)
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,'1003','丰田霸道',30.0,'2000-10-11','燃油车')
</insert>
这样写值写死到配置文件了,这个在实际开发中不能存在的,一定是前端的Form表单提交过来数据
将值传给sql语句
jdbc中sql语句insert into user(id,username) values(?,?)
prepareStatement.setString(1,zhangSan);
JDBC中采用的是?,mybatis中采用的是#{}
mybatis当中不能采用?占位符,要用#{}来做占位符
insert的时候?和#{}是等效的
#{}这里写key的值,如果key不存在,就是null
java中使用pojo类给sql语句传值,大括号内写pojo类的属性值
MYBATIS框架实质上是使用Get方法取出该值的
如果使用pojo传值的话,大括号内的写的是get方法的属性名把第一个字母改小写
getUserName()改为userName
通过XML解析取出需要的属性名字,拼接为getXX方法,通过反射机制执行方法
也就是说mybatis在底层给sql的?传值的时候调用了pojo对象的getXX方法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="abc">
<!-- 这个id是这条SQL语句的唯一标识,这个ID就代表了这条SQL语句-->
<insert id="insertCar">
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})
-- 底层调用map集合的get方法
</insert>
<insert id="insertCar1">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
<!-- 大括号里面写Pojo类的属性名字-->
</insert>
<!-- <delete id=""></delete>-->
<!-- <update></update>-->
<!-- <select></select>-->
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="abc">
<!-- 这个id是这条SQL语句的唯一标识,这个ID就代表了这条SQL语句-->
<insert id="insertCar">
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})
-- 底层调用map集合的get方法
</insert>
<insert id="insertCar1">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
<!-- 大括号里面写Pojo类的属性名字-->
</insert>
<!-- <delete id=""></delete>-->
<!-- <update></update>-->
<!-- <select></select>-->
</mapper>
package com.powernode.mybatis.pojo;
public class Car
{
//这是一个封装汽车相关信息的普通的java类,数据库表中的字段应该和属性一一对应
//为什么使用包装类,因为当我们查值的时候,返回值有可能是null
//避免数据不兼容
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;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", carNum='" + carNum + '\'' +
", brand='" + brand + '\'' +
", guidePrice=" + guidePrice +
", produceTime='" + produceTime + '\'' +
", carType='" + carType + '\'' +
'}';
}
}
package com.powernode.mybatis.pojo;
public class Car
{
//这是一个封装汽车相关信息的普通的java类,数据库表中的字段应该和属性一一对应
//为什么使用包装类,因为当我们查值的时候,返回值有可能是null
//避免数据不兼容
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;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", carNum='" + carNum + '\'' +
", brand='" + brand + '\'' +
", guidePrice=" + guidePrice +
", produceTime='" + produceTime + '\'' +
", carType='" + carType + '\'' +
'}';
}
}
package com.powernode.mybatis.test;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
public class CarMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(CarMapperTest.class);
@Test
public void TestInsertCar()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//前端传过来数据了,用MAP封装
Map<String,Object> 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","新能源");
//执行sql语句,insert(sqlID,一个对象用来封装数据)
//两个参数,一个是sqlId,一个是数据封装的对象
//用这个封装了数据的对象来传值
//这个对象就体现了ORM(对象关系映射)
int insert = sqlSession.insert("insertCar",map);
logger.info("" + insert);
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestInsertCarByPOJO()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL
Car car = new Car(null,"3333","比亚迪秦",12.0,"2020-11-11","新能源");
int insertCar = sqlSession.insert("insertCar1", car);
//ORM对象,我们都是映射,要取值只能是调用get方法
logger.info(insertCar == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
}
package com.powernode.mybatis.test;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
public class CarMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(CarMapperTest.class);
@Test
public void TestInsertCar()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//前端传过来数据了,用MAP封装
Map<String,Object> 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","新能源");
//执行sql语句,insert(sqlID,一个对象用来封装数据)
//两个参数,一个是sqlId,一个是数据封装的对象
//用这个封装了数据的对象来传值
//这个对象就体现了ORM(对象关系映射)
int insert = sqlSession.insert("insertCar",map);
logger.info("" + insert);
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestInsertCarByPOJO()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL
Car car = new Car(null,"3333","比亚迪秦",12.0,"2020-11-11","新能源");
int insertCar = sqlSession.insert("insertCar1", car);
//ORM对象,我们都是映射,要取值只能是调用get方法
logger.info(insertCar == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
}