java中使用pojo类给sql语句传值,大括号内写pojo类的属性值
MYBATIS框架实质上是使用Get方法取出该值的
如果使用pojo传值的话,大括号内的写的是get方法的属性名把第一个字母改小写
getUserName()改为userName
通过XML解析取出需要的属性名字,拼接为getXX方法,通过反射机制执行方法
也就是说mybatis在底层给sql的?传值的时候调用了pojo对象的getXX方法
Update实现也是通过Bean(POJO)类的get方法取值并拼接到sql语句之上实现的
需求根据ID进行查询的话,需要指定resultType="com.powernode.mybatis.pojo.Car"的值
用来告知框架,我们要把查询出来的信息封装成什么类型的POJO对象
如果查询出来的数据库结果的字段名字与类的属性名不一样,就没法赋值
select语句执行的时候将结果以AS的形式转换为我们POJO的属性名
selectOne和SelectList方法都是指定返回值类型
SelectList方法本身就会返回一个list集合的,但是集合对象的类型就需要我们指明
java中使用pojo类给sql语句传值,大括号内写pojo类的属性值
MYBATIS框架实质上是使用Get方法取出该值的
如果使用pojo传值的话,大括号内的写的是get方法的属性名把第一个字母改小写
getUserName()改为userName
通过XML解析取出需要的属性名字,拼接为getXX方法,通过反射机制执行方法
也就是说mybatis在底层给sql的?传值的时候调用了pojo对象的getXX方法
Update实现也是通过Bean(POJO)类的get方法取值并拼接到sql语句之上实现的
需求根据ID进行查询的话,需要指定resultType="com.powernode.mybatis.pojo.Car"的值
用来告知框架,我们要把查询出来的信息封装成什么类型的POJO对象
如果查询出来的数据库结果的字段名字与类的属性名不一样,就没法赋值
select语句执行的时候将结果以AS的形式转换为我们POJO的属性名
selectOne和SelectList方法都是指定返回值类型
SelectList方法本身就会返回一个list集合的,但是集合对象的类型就需要我们指明
<?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="deleteById">
-- 一个占位符可以随便写,它能够识别
delete from
t_car
where
id = #{id};
</delete>
<update id="updateById">
update
t_car
set
car_num = #{carNum},
brand = #{brand},
guide_price = #{guidePrice},
produce_time = #{produceTime},
car_type = #{carType}
where
id = #{id};
<!-- Update同理-->
</update>
<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
select
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</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="deleteById">
-- 一个占位符可以随便写,它能够识别
delete from
t_car
where
id = #{id};
</delete>
<update id="updateById">
update
t_car
set
car_num = #{carNum},
brand = #{brand},
guide_price = #{guidePrice},
produce_time = #{produceTime},
car_type = #{carType}
where
id = #{id};
<!-- Update同理-->
</update>
<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
select
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</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.Iterator;
import java.util.List;
import java.util.Map;
public class CarMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(CarMapperTest.class);
@Test
public void TestSelectAll()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL语句
List<Object> selectAll = sqlSession.selectList("selectAll");
for (int i = 0; i < selectAll.size(); i++)
{
logger.info(selectAll.toString());
}
Iterator iterator = selectAll.iterator();
while (iterator.hasNext())
{
logger.info(iterator.next().toString());
}
selectAll.forEach(car -> logger.info(car.toString()));
sqlSession.close();
}
@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();
}
@Test
public void TestDeleteById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL语句
int deleteById = sqlSession.delete("deleteById", 2);
logger.info(deleteById == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestUpdateById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
Car car = new Car(3L,"3333","比亚迪秦",12.0,"2020-11-11","新能源");
int updateById = sqlSession.update("updateById", car);
logger.info(updateById == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestSelectById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行DQL语句
Car car = sqlSession.selectOne("selectById", 5);
//mybatis底层执行了select后会返回一个ResultSet结果对象
//接下来mybatis应该从ResultSet中取值封装成java对象
//封装完成返回一个java对象
logger.info(car.toString());
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.Iterator;
import java.util.List;
import java.util.Map;
public class CarMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(CarMapperTest.class);
@Test
public void TestSelectAll()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL语句
List<Object> selectAll = sqlSession.selectList("selectAll");
for (int i = 0; i < selectAll.size(); i++)
{
logger.info(selectAll.toString());
}
Iterator iterator = selectAll.iterator();
while (iterator.hasNext())
{
logger.info(iterator.next().toString());
}
selectAll.forEach(car -> logger.info(car.toString()));
sqlSession.close();
}
@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();
}
@Test
public void TestDeleteById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行SQL语句
int deleteById = sqlSession.delete("deleteById", 2);
logger.info(deleteById == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestUpdateById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
Car car = new Car(3L,"3333","比亚迪秦",12.0,"2020-11-11","新能源");
int updateById = sqlSession.update("updateById", car);
logger.info(updateById == 1?"成功" : "失败");
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestSelectById()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
//执行DQL语句
Car car = sqlSession.selectOne("selectById", 5);
//mybatis底层执行了select后会返回一个ResultSet结果对象
//接下来mybatis应该从ResultSet中取值封装成java对象
//封装完成返回一个java对象
logger.info(car.toString());
sqlSession.close();
}
}