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>com.wsd</groupId><artifactId>mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--dom4j依赖--><dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version></dependency><!--jaxen依赖--><dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.2.0</version></dependency><!--mysql驱动依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><!--junit依赖--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>17</source><target>17</target></configuration></plugin></plugins></build></project>
配置文件
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="UNPOOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment>
</environments>
<mappers><mapper resource="CarMapper.xml"/>
</mappers>
</configuration>
mapper文件
CarMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="car"><!--insert sql:保存一个汽车信息--><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><delete id="deleteByCarNum">delete from t_car where car_num = #{carNum}</delete><update id="updateCarById">update t_car setcar_num = #{carNum},brand = #{brand},guide_price = #{guidePrice},produce_time = #{produceTime},car_type = #{carType}where id = #{id}</update><select id="selectCarById" resultType="com.wsd.pojo.Car">select id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carTypefrom t_carwhere id = #{id}</select><!--虽然结果是List集合,但是resultType属性需要指定的是List集合中元素的类型。--><select id="selectCarAll" resultType="com.wsd.pojo.Car"><!--记得使用as起别名,让查询结果的字段名和java类的属性名对应上。-->selectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefromt_car</select>
</mapper>
test:
package com.wsd.core;import com.wsd.core.util.Resources;
import org.dom4j.DocumentException;
import org.junit.Test;/*** @description: test* @author: Mr.Wang* @create: 2023-06-24 15:40**/
public class TestMyBatis {@Testpublic void testSqlSessionFactory(){SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();try {SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourcesAsStream("mybatis-config.xml"));System.out.println(sqlSessionFactory);} catch (DocumentException e) {e.printStackTrace();}}
}
debug: