實(shí)現(xiàn)步驟
1.創(chuàng)建一個(gè)student表
2.新建一個(gè)maven項(xiàng)目
3.創(chuàng)建一個(gè)實(shí)體類student彩匕,定義屬性财松,屬性名辽聊,和列名一致
package com.zwx.entity;
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
4.創(chuàng)建dao接口栋烤,定義操作數(shù)據(jù)庫的方法
package com.zwx.dao;
import com.zwx.entity.Student;
public interface StudentDao {
//查詢一個(gè)學(xué)生
Student selectStudentById(Integer id);
}
5.創(chuàng)建一個(gè)xml文件(mapper文件)寫sql語句
mybatis把代碼和sql語句分開
mapper文件:定義和dao接口在一個(gè)目錄谒养,一個(gè)表一個(gè)mapper文件
<?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.zwx.dao.StudentDao">
<select id="selectStudentById" resultType="com.zwx.entity.Student">
SELECT id,name,email,age FROM mybatis.student WHERE id = #{id}
</select>
</mapper>
6.創(chuàng)建mybatis的主配置文件(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>
<!-- 日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<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/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zwx/dao/StudentDao.xml"/>
</mappers>
</configuration>
定義創(chuàng)建鏈接實(shí)例的數(shù)據(jù)源對象(datasource)
指定其他mapper文件的位置
7.測試
import com.zwx.entity.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.InputStream;
public class Mytest {
@Test
public void test() throws IOException {
// mybatis核心類:SqlSessionFactory
//定義mybatis主配置文件的位置,從類路徑開始的相對路徑
String config="mybatis.xml";
// 讀取主配置文件使用mybatis框架中的Resource類
InputStream inputStream = Resources.getResourceAsStream(config);
//創(chuàng)建SqlSessionFactory對象明郭,使用SqlSessionFactoryBuilder()
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//獲取sqlsession對象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 指定要執(zhí)行的sql的id
String sqlId="com.zwx.dao.StudentDao"+"."+"selectStudentById";
// 通過sqlSession的方法 執(zhí)行sql語句
Student student = sqlSession.selectOne(sqlId,1101);
System.out.println(student);
// 關(guān)閉sqlsession對象
sqlSession.close();
}
}