实现步骤
1.创建一个student表
2.新建一个maven项目
3.创建一个实体类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.创建dao接口,定义操作数据库的方法
package com.zwx.dao;
import com.zwx.entity.Student;
public interface StudentDao {
//查询一个学生
Student selectStudentById(Integer id);
}
5.创建一个xml文件(mapper文件)写sql语句
mybatis把代码和sql语句分开
mapper文件:定义和dao接口在一个目录,一个表一个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.创建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>
定义创建链接实例的数据源对象(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);
//创建SqlSessionFactory对象,使用SqlSessionFactoryBuilder()
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlsession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 指定要执行的sql的id
String sqlId="com.zwx.dao.StudentDao"+"."+"selectStudentById";
// 通过sqlSession的方法 执行sql语句
Student student = sqlSession.selectOne(sqlId,1101);
System.out.println(student);
// 关闭sqlsession对象
sqlSession.close();
}
}