一、新建Maven项目
http://www.mybatis.org/mybatis-3/zh/index.html 该链接为MyBatis官方地址
创建MyBatis项目主要有两种办法,一种是导入jar包,可在github上下载https://github.com/mybatis/mybatis-3/releases。另一种方法就是现在比较常用的,也是官方推荐的maven导入。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
下面我以IDEA来构建一个简单的MyBatis入门案例
无需选择任何模板直接next即可
直接next到finish即可
二、mybatis配置相关xml文件
pom.xml需要加上必须的MyBatis、junit、mysql连接驱动,这些都可以在maven官网上找到https://mvnrepository.com/
<?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.mybatis.demo</groupId>
<artifactId>demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> </project>
然后在main目录下的resources文件夹内新建mybatis-config.xml文件用来存放mybatis配置信息,和mapper文件夹用户存放映射文件
下为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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>
<property name="username" value="root"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--映射配置文件路径-->
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
</configuration>
三、实现mybatis
我这里就使用navicat创建了一个简单的user表
在IDEA中创建一个user实体类,并且生成getter setter方法
public class User {
private int id;
private String username;
private String password; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
编写userMapper映射文件
<?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属性是用来配置命名空间
resultType是sql映射文件中定义返回值类型
-->
<mapper namespace="com.mybatis.demo.User">
<select id="userList" resultType="com.mybatis.demo.User">
select * from users
</select>
</mapper>
编写测试类
import com.mybatis.demo.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 org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class myBatisTestDemo {
@Test
public void demo1(){
try {
//引入配置文件
InputStream io=Resources.getResourceAsStream("mybatis-config.xml");
//创建sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(io);
SqlSession sqlSession=sqlSessionFactory.openSession();
List<User> lists=sqlSession.selectList("userList");
System.out.println(lists.size());
} catch (IOException e) {
e.printStackTrace();
}
} }