1.引入相应的jar 包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
2.编写Mybatis核心配置文件·
1.路径在 项目的 resources
<?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/day2?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456789"/>
</dataSource>
</environment>
</environments>
//每个Mapper 都需要注册到核心配置文件中
<mappers>
<mapper resource="com/example/dao/UserMapper.xml"/>
</mappers>
</configuration>
3.编写MyBatis 工具类
工具类放在utils 包下
package com.example.utils;
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 javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try{
//使用mybatis 第一步 获取sqlsessionFactory 对象
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e){
e.printStackTrace();
}
//既然有了sqlsessionFactory 获取 sqlsession实例
//sqlsession 完全面向数据库执行sql 命令
}
public static SqlSession getSqlSession(){
SqlSession sqlSession= sqlSessionFactory.openSession();
return sqlSession;
}
}
4.编写实体
package com.example.entity;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
5.编写Mapper 接口类/UserDao
package com.example.dao;
import com.example.entity.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
6.编写Mapper.xml 配置文件
namespace 不能写错
<?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 绑定一个对于的Dao/MAPPER 接口-->
<mapper namespace="com.example.dao.UserDao">
<!--id对应方法名 resultType /resultMap 经常用 resultType 对应实体·-->
<select id="getUserList" resultType="com.example.entity.User">
select * from user
</select>
</mapper>
7.编写测试类
package com.example.dao;
import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import javax.annotation.Resource;
import java.util.List;
public class UserDaoTest {
@Test
public void test() {
//第一步获取Sqlsession 对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
// sqlSession.close();
//执行sql
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userlist = userDao.getUserList();
for (User user : userlist) {
System.out.println(user);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
//关闭sqlsession
sqlSession.close();
}
}
}
注:需放在pom <build></build>
···
可能出现问题说明:Maven静态资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>/.properties</include>
<include>/.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>/.properties</include>
<include>/.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
···