CRUD:Create Retrieve Update Delete
1、insert
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,'1003','五菱宏光',30.0,'2020-09-18','燃油车');
</insert>
这样写显然是写死的,在实际开发中是不存在的,一定是前端 form 表单提交过来数据,再将值赋值给 SQL 语句
在 JDBC 中可以使用 “?” 当占位符,而 mybatis 中需要使用 “#{}”,当作占位符,不能使用 “?”
Java 程序中使用 Map 给 SQL 语句的占位符传值:
一般 map 集合的 key 命名要见名知意
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});
</insert>
@Test
public void testInsertCar(){
SqlSession sqlSession = SqlSessionUtil.openSession();
/**
* 执行 SQL 语句
*
* insert 方法参数:
* 1> sqlId
* 2> 封装数据的对象
*
* 使用 Map 集合封装数据
*/
Map<String, Object> map = new HashMap<>();
map.put("k1","1004");
map.put("k2","比亚迪");
map.put("k3",10.0);
map.put("k4","2020-12-01");
map.put("k5","新能源");
int count = sqlSession.insert("insertCar",map);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
Java 程序中使用 POJO 给 SQL 语句的占位符传值:
Car car = new Car(null,"1005","红旗H9",35.0,"2020-09-09","燃油车");
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});
</insert>
#{} 里面对应的是该类的 getter 方法
@Test
public void testInsertCarPOJO(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 封装数据
Car car = new Car(null,"1005","红旗H9",35.0,"2020-09-09","燃油车");
int count = sqlSession.insert("insertCar", car);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
2、delete
<delete id="deleteById">
delete from t_car where id = #{id}
</delete>
如果占位符只有一个,#{} 里的内容可以随便写,最好还是见名知意
@Test
public void testDeleteById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
int count = sqlSession.delete("deleteById", 5);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
3、update
<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>
@Test
public void testUpdateById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
Car car = new Car(4L,"1004","五菱宏光Mini",20.0,"2020-11-11","燃油车");
int count = sqlSession.update("updateById", car);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
4、select
- 查一个
<!--resultType:指定结果集要封装的Java对象类型,全限定类型-->
<select id="selectById" resultType="com.qiuxuan.pojo.Car">
select * from t_car where id = #{id}
</select>
Object o = sqlSession.selectOne("selectById", 4);
存在问题:结果存在 null 值
Car{id=4, carNum='null', brand='五菱宏光Mini', guidePrice=null, produceTime=null, carType='null'}
原因:
select * from t_car where id = 1
执行结果:
部分字段与 Car 类的属性名并没有对应上,所以在查询完后并未赋值
解决方法:给 SQL 语句的表字段起别名,使其与 Car 类的属性一致
<select id="selectById" resultType="com.qiuxuan.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>
@Test
public void testSelectById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
Object o = sqlSession.selectOne("selectById", 4);
System.out.println(o);
sqlSession.close();
}
- 查全部
<!--selectAll-->
<select id="selectAll" resultType="com.qiuxuan.pojo.Car">
select id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from t_car
</select>
@Test
public void testSelectAll(){
SqlSession sqlSession = SqlSessionUtil.openSession();
List<Car> list = sqlSession.selectList("selectAll");
for (Object o : list) {
System.out.println(o);
}
sqlSession.close();
}
SQLMapper 映射文件的 namespace 作用
在 SQLMapper 配置文件中 <mapper>
标签的 namespace 属性可以翻译为命名空间,这个命名空间主要是为了防止 sqlId 冲突的
当两个 SQLMapper 文件中有各有一个 SQL 语句的 id 一样,就需要在使用时加上命名空间加以区分,否则会报如下异常:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException:
selectCarAll is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)
【翻译】selectCarAll 在 Mapped Statements 集合中不明确
(请尝试使用包含名称空间的全名,或重命名其中一个条目)
大致意思是 selectCarAll 重名了,需要在 selectCarAll 前添加一个名称空间,或者改个其它名字
一 叶 知 秋,奥 妙 玄 心