1 添加依赖
添加mybatis相关依赖和mysql连接相关依赖
<?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.star</groupId>
<artifactId>mybatis_01_helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!-- 描述存放资源的目录,该路径相对POM路径-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
注意:这里有个坑,mysql连接依赖大家最好用和数据库对应的版本
2 编写配置文件
2.1 编写全局配置文件mybatis-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>
<!-- 引入数据源-->
<properties resource="db.properties"/>
<typeAliases>
<!--类别名:别名为类名称的小写-->
<package name="com.star.mybatis.pojo"/>
</typeAliases>
<!--数据源配置 -->
<environments default="development"> <!--默认开发环境 可通过environment配置多环境-->
<environment id="development">
<transactionManager type="jdbc" /> <!--配置事务管理器-->
<dataSource type="POOLED"> <!--开启数据库连接池-->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 使用映射器接口实现类的完全限定类名 -->
<!--<mapper class="com.star.mybatis.mapper.UserMapper"/>-->
<!-- 使用相对于类路径的资源引用 -->
<!--<mapper resource="com/star/mybatis/mapper/UserMapper.xml"/>-->
<!-- 将包内的映射器接口实现全部注册为映射器 -->
<package name="com.star.mybatis.mapper"/>
</mappers>
</configuration>
2.2 编写mapper映射文件usermapper.xml
这个配置文件主要是用来编写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接口的全路径-->
<mapper namespace="com.star.mybatis.mapper.UserMapper">
<!-- id要和mapper的方法名一样-->
<!--resultType="com.star.mybatis.pojo.User" 推荐写返回值类全名 方便别人阅读
user:类别名
-->
<select id="selectUserById" resultType="user" >
select * from user where id = #{id}
</select>
</mapper>
3 编写mapper接口
package com.star.mybatis.mapper;
import com.star.mybatis.pojo.User;
public interface UserMapper {
User selectUserById(String id);
}
4 测试
package com.star.mybatis;
import com.star.mybatis.mapper.UserMapper;
import com.star.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class HelloWorld {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
//获取SqlSessionFactory工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用反射获得mapper代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectUserById("1");
System.out.println(user);
//关闭连接
sqlSession.close();
}
}