使用mybatis完成CRUD
1.什么是CRUD
C:Creste增
R:Retrieve查(检索)
U:Update(改)
D:Delete(删)
2.先实现insert
<insert id="insertCar">
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,'1007','丰田',20.0,'2020-12-30',"燃油车")
</insert>
这样写的问题?
值 写死到配置文件中的,
这个在实际开发中不存在的
一定是前端from表单提交过来数据,然后将值传给sql语句
例如JDBC的代码是怎样写的?
String sql="insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,?,?,?,?,?)";
ps.setString(1,xxx);
ps.setString(2,yyy);
.....
在JDBC当中占位符采用的是? 在mybatis当中采用的是?
和?等效的写法:#{}
在mybatis不能用?占位符 ,必须使用 #{} 来代替JDBC当中的?
#{}和JDBC当中的?是等效的
java程序中使用Map可以给SQL语句的占位符传值
Map<String, Object> map=new HashMap<>();
map.put("k1","11111");
map.put("k2","比亚迪");
map.put("k3","12.0");
map.put("k4","2020-11-28");
map.put("k5","电车");
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,#{k1},#{k2},#{k3},#{k4},#{k5});
注意:#{这里写map集合的k} 如果k不存在 获取的是null
一般map集合的k起名的时候要简明知意
map.put("carNum","11111");
map.put("brand","比亚迪");
map.put("guidePrice","12.0");
map.put("produceTime","2020-11-28");
map.put("carType","电车");
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});
java程序中使用POJO类给SQL语句占位符传值:
Car car=new Car(null,"3333","大众",30.0,"2020-01-05","新能源");
注意:占位符#{} 大括号内些什么 pojo类的属性名
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});
把sql语句写成这个德行:
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{xyz},#{brand},#{guidePrice},#{produceTime},#{carType});
出现什么问题
There is no getter for property named 'xyz' in 'class com.powernode.Car'
mybatis去找:Car类中的xyz方法去了 没找到报错了
怎么解决?
可以在Car类中提供一个getXyz()方法 问题就解决了
通过这个测试:如果使用POJO对象传值的话。#{}这个大括号中写什么?
写get方法的方法名去掉get。然后将剩下的单词首字母小写,让后放进入
例如 getUsername()--->#{username}
例如 getEmail()--->#{email}
,,,,
也就是说mybatis在底层给?传值的时候,首先获取值 怎么获取值
调用pojo对象的get方法 例如car.getCarNum(). car.getCarType(). car.getBrand().
3.删除delete
根据id删除数据 将id=27的删除
实现:
int count = sqlSession.delete("deleteById", 27);
<delete id="deleteById">
delete from t_car where id=#{id}
</delete>
注意:如果占位符只有一个,那么#{}的大括号里边随便写,但是最好见名知意。
4.改 update
根据id修改某条记录
<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>
Car car=new Car(2L,"9090","丰田",30.0,"1999-12-30","燃油车");
int count = sqlSession.delete("updateById", car);
5.select (查一个,根据主键查询的话,返回的结果一定是一个)
* 需求 根据id查询
实现:
<select id="selectById" resultType="com.powernode.Car">
select * from t_car where id=#{id}
</select>
Object car = sqlSession.selectOne("selectById", 1);
特别注意是:select标签中resultType属性,这个属性来告诉mybatis,查询结果集封装成什么类型的Java对象,你需要告诉mybatis
resultType通常写全限定类名
输出结果不对劲:
Car{id=1, carNum='null', brand='宝马520', guidePrice=null, produceTime='null', carType='null'}
id和brand有值 其他无值
carNum以及其他的几个属性没有赋上值的原因是什么
select * from t_car where id=1
执行结果:
+----+---------+---------+-------------+--------------+----------+
| id | car_num | brand | guide_price | produce_time | car_type |
+----+---------+---------+-------------+--------------+----------+
| 1 | 1001 | 宝马520 | 10.00 | 2020-11-12 | 燃油车 |
+----+---------+---------+-------------+--------------+----------+
car_num guide_price produce_time car_type这是查询结果的列名;
这些类名和Car类中的属性名对不上:
Car类的是 carNum guidePrice produceTime carType
这个问题怎么解决呢?
select语句查询的时候,在查询结果集的列名是可以使用as关键字起别名的,
<select id="selectById" resultType="com.powernode.Car">
<!-- select * from t_car where id=#{id}-->
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>
执行之后:
+----+---------+---------+-------------+--------------+----------+
| id | carNum | brand | guidePrice | produceTime | carType |
+----+---------+---------+-------------+--------------+----------+
| 1 | 1001 | 宝马520 | 10.00 | 2020-11-12 | 燃油车 |
+----+---------+---------+-------------+--------------+----------+
6.select(查所有)
<select id="selectAll" resultType="com.powernode.Car">
<!-- select * from t_car where id=#{id}-->
select id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
List<Object> cars = sqlSession.selectList("selectAll");
注意:resultType 还是指定要封装的结果集的类型,不是List类型,是指定List集合中元素类型
selectList方法:mybatis通过这个方法就可以得知你需要一个List集合,它会自动给你返回一个List集合
7.在sql mapper.xml文件当中有一个namespace,这个属性是用来指定命名的。用来防止id重复
怎么用?
在xml文件中:
<mapper namespace="aaaaaa">
<select id="selectAll" resultType="com.powernode.Car">
<!-- select * from t_car where id=#{id}-->
select id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
在Java程序中写法:
List<Object> cars = sqlSession.selectList("aaaaaa.selectAll");
实际上,本质上,mybatis中sqlId的完整写法:namespace.id