Mybatis是一个持久层的框架,可以替代我们之前所写的Dao层的接口,类.便于我们的开发
快速入门:
-
环境配置
- mysql数据库
- 创建于数据库表字段的UserBean实体类,并实现Serializable接口(序列化)
- 创建UserDao接口,并声明findAll()方法,注意创建包路径
- 配置pom文件,加载Mybatis的jar和mysql的jar包
- 配置UserDao.xml文件
- 配置SqlMapConfig.xml,Mybatis的核心配置文件
代码实现
pom.xml
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
UserBean
package com.dawn.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
UserDao
package com.dawn.dao;
import com.dawn.domain.User;
import java.util.List;
public interface UserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
}
UserDao.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">
<!--加载对应包路径下的UserDao-->
<mapper namespace="com.dawn.dao.UserDao">
<!-- 配置查询所有用户-->
<select id="findAll" resultType="com.dawn.domain.User">
select * from user;
</select>
</mapper>
SqlMapConfig.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>
<!-- 配置mybatis-->
<environments default="mysql">
<!-- 配置数据库mysql-->
<environment id="mysql">
<!-- 配置jdbc-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置连接池-->
<dataSource type="POOLED">
<!-- 配置数据库连接-->
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<!-- 告知mybatis映射配置的位置-->
<mappers>
<mapper resource="com/dawn/dao/UserDao.xml"></mapper>
</mappers>
</configuration>
-
测试
- 读取资源文件SqlMapConfig.xml
- 创建构建工厂对象SqlSessionFactoryBuilder
- 创建工厂对象SqlSessionFactory,因为是接口不可以被直接创建
- 根据工厂对象factory生成SqlSession对象
- session对象进行代理对象处理
- 调用findAll()方法
- 关闭资源
代码实现
package com.dawn;
import com.dawn.dao.UserDao;
import com.dawn.domain.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;
import java.util.List;
public class MybatisTest {
public static void main(String[] args) throws Exception {
//1.读取配置文件资源
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建构建工厂对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.根据构建工厂对象创建工厂对象
SqlSessionFactory factory = builder.build(is);
//4.根据工厂对象生成SqlSession对象
SqlSession session = factory.openSession();
//5.根据SqlSession对象获取代理对象
UserDao mapper = session.getMapper(UserDao.class);
//6.执行方法
List<User> users = mapper.findAll();
for (User user : users) {
System.out.println(user);
}
//7.关闭资源
session.close();
is.close();
}
}