一、创建工程
使用IDEA创建普通maven项目(章末有项目结构)
二、引入依赖(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.lofun</groupId>
<artifactId>lianxi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
三、数据库准备
MySQL为例:在test数据库下创建student表,字段为name,hoby
四、创建pojo实体(student类)
在src/main/Java下创建自己的项目结构,然后创建pojo文件夹存放实体类
package com.lofun.pojo;
public class student {
String name,hoby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHoby() {
return hoby;
}
public void setHoby(String hoby) {
this.hoby = hoby;
}
}
四、创建mybatis核心配置文件
在resource文件夹下创建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://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&userSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
五、创建映射文件StudentMapper.xml(操作student表)
(1)在resource文件夹下创建mapper文件夹并创建Student Mapper.xml
<?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="com.lofun.pojo.student">
<select id="findStudent" resultType="com.lofun.pojo.student">
select * from student where name = #{id}
</select>
</mapper>
(2)将StudentMapper.xml映射文件加载到程序中,在mybatis-config.xml添加
<mappers> <mapper resource="mapper/studentMapper.xml"/> </mappers>
<?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://127.0.0.1:3306/lianxi?useUnicode=true&characterEncoding=UTF-8&userSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/studentMapper.xml"/>
</mappers>
</configuration>
六、测试(编写测试类)
在test文件夹下创建Test文件夹并创建StudentTest类,代码如下
package Test;
import com.lofun.pojo.student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;
import java.io.IOException;
import java.io.Reader;
public class StudentTest {
@Test
public void findStudent(){
String resources = "mybatis-config.xml";
//创建流
Reader reader = null;
try {
//读取mybatis-config.xml
reader = Resources.getResourceAsReader(resources);
} catch (IOException e) {
e.printStackTrace();
}
//创建SqlSessionFactory类的实例
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
//创建SqlSession的实例
SqlSession session = sqlMapper.openSession();
//传参
student stu = session.selectOne("findStudent","11");
//输出结果
System.out.print(stu.getName()+":"+stu.getHoby());
//关闭session
session.close();
}
}