完整代碼在這裏php
MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。(來自Mybatis官網)html
建立數據庫java
在這裏我使用了mySql數據庫,具體過程不表,最終的建立結果以下: traveldb爲庫名,user爲表名mysql
建立maven項目sql
打開idea ,建立一個maven項目,添加兩個依賴:數據庫
mysql-connector-javaapache
mybatisapi
<?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.learn.mybatis</groupId>
<artifactId>mybatisFirstDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.7-dmr</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
</project>
複製代碼
在開始使用Mybatis 操做數據庫以前,咱們要先創建和數據庫表對應的模型類,類中的變量成員和數據庫表中的字段一一對應,而且要有getter和setter方法。(就是建立和數據庫表對應的JavaBean)session
package com.learn.pojo;
public class User {
private String user_name;
private String user_id;
private String user_pwd;
private String user_phone;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_pwd() {
return user_pwd;
}
public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}
public String getUser_phone() {
return user_phone;
}
public void setUser_phone(String user_phone) {
this.user_phone = user_phone;
}
}
複製代碼
<typeAliases> 標籤能夠用來配置別名,配置 <package> 後,mybatis會自動搜索該package下全部的JavaBean。詳細請看這裏mybatis
完整配置文件:
<?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>
<typeAliases>
<package name="com.learn.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/traveldb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="user.xml"/>
</mappers>
</configuration>
複製代碼
**/**標籤是映射器,用來映射sql語句,在此例中使用了相對資源定位來定位映射的配置文件,除此以外還有其它的定位方法,詳情請看這裏
Mybatis實現了不少種不一樣的sql映射,好比說 select update insert 等等,詳細請看這裏
<?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.learn.pojo">
<select id="listUser" resultType="User">
select * from user
</select>
</mapper>
複製代碼
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.learn.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;
public class Test {
public static void main(String[] args) throws IOException {
//聲明配置文件地址
String resource = "mybatis-config.xml";
//獲得輸入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//建立sql會話工廠
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//打開sql會話
SqlSession session = sqlSessionFactory.openSession();
//獲取查詢結果
List<User> lu = session.selectList("listUser");
for (User u : lu) {
System.out.println(u.getUser_name());
System.out.println(u.getUser_id());
System.out.println(u.getUser_phone());
System.out.println("------------------------");
}
}
}
複製代碼
運行結果: