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
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 如果引入了三方可以不用写-->
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:13306/powernode" />
<property name="username" value="root" />
<property name="password" value="abc123" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 指定xml文件的路径,自动从根路径下找资源-->
<mapper resource="CarMapper.xml"></mapper>
<!-- url属性,是从绝对路径下加载资源的,语法格式是file:///加绝对路径-->
<!-- <mapper resource="CarMapper.xml" url="file:///d:/CarMapper.xml"></mapper>-->
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 如果引入了三方可以不用写-->
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:13306/powernode" />
<property name="username" value="root" />
<property name="password" value="abc123" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 指定xml文件的路径,自动从根路径下找资源-->
<mapper resource="CarMapper.xml"></mapper>
<!-- url属性,是从绝对路径下加载资源的,语法格式是file:///加绝对路径-->
<!-- <mapper resource="CarMapper.xml" url="file:///d:/CarMapper.xml"></mapper>-->
</mappers>
</configuration>
<?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>
<!-- <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>
<!-- <delete id=""></delete>-->
<!-- <update></update>-->
<!-- <select></select>-->
</mapper>
package com.powernode.mybatis.test;
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();
}
}
package com.powernode.mybatis.test;
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();
}
}