准备工作:mybatis依赖包和Maven依赖包
下载地址:
mybatis下载地址去GitHub上面直接搜即可下载,下载后安装解压到本地目录即可
创建一个文件夹:D:\repository
1.创建一个空项目
2.创建一个Maven模块
直接下一步不需要勾选别的
你只需要修改模块名即可
3.配置
3.打开pom.xml文档配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis03</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包方式-->
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--加载mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!--加载mybatis依赖,需要和你前面准备工作中下载的mybatis依赖版本一致-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<!--加载junit依赖(用来测试单元测试的)-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.在resources下配置mybatis核心文档"mysql-config.xml"这个名字是随意的
<?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>
<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:3306/powernode"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
</configuration>
4.在resources下配置一个CarMapper.xml,这个文档对应一个Car数据表,在里面的mapper标签下编写sql语句
<?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 sql:保存一个汽车信息-->
<insert id="insertCar">
insert into t_car
(id,car_num,brand,guide_price,produce_time,car_type)
values
(null,'102','丰田mirai',40.30,'2014-10-05','氢能源')
</insert>
</mapper>
5.回到mysql-config.xml中配置这个CarMapper.xml的sql映射文件
<?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>
<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:3306/powernode"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!--sql映射文件创建好之后,需要将该文件路径配置到这里-->
<mappers>
<mapper resource="CarMapper.xml"/>
</mappers>
</configuration>
6.在Test.java下编写CarMapperTest.java
public class CarMapperTest {
@Test
public void testInsertCar(){
SqlSession sqlSession = null;
try {
//1.获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
/**
* 2.获取SqlSessionFactory对象
* 获取方式:sqlSessionFactoryBuilder.Builder(需要传一个输入流);
* 我们采用Resources.getResourceAsStream("mybatis核心配置文件路径")
* 来获取输入流,Resources.getResourceAsStream默认从resources文件下查找
*/
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
/**3.获取SqlSession对象,通过该对象即可执行sql语句
* openSession(这里面可以填一个布尔值,默认是false,代表是否自动提交事务)
* 不建议里面填true,因为他底层有一个判断,如果这里为true他不会开启事务,
* 若不开启事务的话你写的sql语句是否提交就取决于在mybatis核心配置文件中的
* <transactionManager type="JDBC"/>这个标签配置,比如说你填的是JDBC
* 那么它就是会自动提交,因为JDBC就是自动提交机制
*/
sqlSession = sqlSessionFactory.openSession();
//4.执行sql语句
int i = sqlSession.insert("insertCar");
//5.提交事务
sqlSession.commit();
} catch (Exception e) {
//出现异常回滚事务
if(sqlSession != null){
sqlSession.rollback();
}
e.printStackTrace();
}finally {
//会话关闭,释放资源
if(sqlSession != null){
sqlSession.close();
}
}
}
}