一、利用MyBatis完成CRUD
准备⼯作
创建module(Maven的普通Java模块):mybatis-002-crud
pom.xml
打包⽅式 jar
依赖:
mybatis依赖
mysql驱动依赖
junit依赖
logback依赖
mybatis-config.xml 放在类的根路径下
CarMapper.xml 放在类的根路径下
logback.xml 放在类的根路径下
提供com.powernode.mybatis.utils.SqlSessionUtil⼯具类
创建测试⽤例:com.powernode.mybatis.CarMapperTest
1、insert语句
package com.powernode.mybatis;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* 测试MyBatis的CRUD
* @author ⽼杜
* @version 1.0
* @since 1.0
*/
public class CarMapperTest {
@Test
public void testInsertCar(){
// 准备数据
Map<String, Object> map = new HashMap<>();
map.put("k1", "103");
map.put("k2", "奔驰E300L");
map.put("k3", 50.3);
map.put("k4", "2020-10-01");
map.put("k5", "燃油⻋");
// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执⾏SQL语句(使⽤map集合给sql语句传递数据)
int count = sqlSession.insert("insertCar", map);
System.out.println("插⼊了⼏条记录:" + count);
}
}
<?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">
<!--namespace先随便写-->
<mapper namespace="car">
<insert id="insertCar">
insert into t_car(car_num,brand,guide_price,produce_time,car_typ
e) values(#{k1},#{k2},#{k3},#{k4},#{k5})
</insert>
</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="jkljkljkl">
<select id="selectAll" 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
</select>
<select id="selectById" resultType="com.powernode.mybatis.pojo.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>
<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>
<delete id="deleteById">
delete from t_car where id = #{id}
</delete>
<insert id="insertCar">
<!--insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,#{k1},#{k2},#{k3},#{k4},#{k5});-->
<!--insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});-->
insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
</mapper>
测试程序
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CarMapperTest {
@Test
public void testNamespace(){
SqlSession sqlSession = SqlSessionUtil.openSession();
//List<Object> cars = sqlSession.selectList("selectAll");
// 正确完整的写法:namespace.id
List<Object> cars = sqlSession.selectList("aaaaaaaaa.selectAll");
cars.forEach(car-> System.out.println(car));
sqlSession.close();
}
@Test
public void testSelectAll(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
//List<Object> cars = sqlSession.selectList("selectAll");
List<Car> cars = sqlSession.selectList("selectAll");
// 遍历
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
@Test
public void testSelectById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行DQL语句。查询。根据id查询。返回结果一定是一条。
// mybatis底层执行了select语句之后,一定会返回一个结果集对象:ResultSet
// JDBC中叫做ResultSet,接下来就是mybatis应该从ResultSet中取出数据,封装java对象。
Object car = sqlSession.selectOne("selectById", 1);
System.out.println(car);
sqlSession.close();
}
@Test
public void testUpdateById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 准备数据
Car car = new Car(4L, "9999", "凯美瑞", 30.3, "1999-11-10", "燃油车");
// 执行SQL语句
int count = sqlSession.update("updateById", car);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testDeleteById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
//int count = sqlSession.delete("deleteById", 59);
int count = sqlSession.delete("deleteById", 66);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testInsertCarByPOJO(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 封装数据
Car car = new Car(null, "3333", "比亚迪秦", 30.0, "2020-11-11", "新能源");
// 执行SQL
int count = sqlSession.insert("insertCar", car); // ORM
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testInsertCar(){
SqlSession sqlSession = SqlSessionUtil.openSession();
// 前端传过来数据了。
// 这个对象我们先使用Map集合进行数据的封装。
Map<String, Object> map = new HashMap<>();
/*map.put("k1", "1111");
map.put("k2", "比亚迪汉");
map.put("k3", 10.0);
map.put("k4", "2020-11-11");
map.put("k5", "电车");*/
map.put("carNum", "1111");
map.put("brand", "比亚迪汉2");
map.put("guidePrice", 10.0);
map.put("produceTime", "2020-11-11");
map.put("carType", "电车");
// 执行SQL语句
// insert方法的参数:
// 第一个参数:sqlId,从CarMapper.xml文件中复制。
// 第二个参数:封装数据的对象。
int count = sqlSession.insert("insertCar", map);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
}