主配置文件mybatis-config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置数据源:创建Connection对象-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/bjpowernode?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!--指定其他mapper文件的位置
找到其他mapper文件的目的是找到sql语句
使用注意:
resource="mapper.文件的路径,用/分割"-->
<mappers>
<mapper resource="Dao/StudentMapper.xml"/>
</mappers>
</configuration>
Dao接口
package Dao;
import domain.Student;
import java.util.List;
public interface StudentDao {
Student selectById(Integer id);
List<Student> selectStudents();
}
mapper.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">
<!--绑定接口-->
<mapper namespace="Dao.StudentDao">
<!--
mapper是根标签
mapper里面可以写<insert>,<update>,<select>等标签
标签的id属性:正常是需要重写的方法名
标签的resultType属性:将数据赋值给哪个类型的Java对象
标签内部都有同名的sql语句
-->
<select id="selectById" resultType="domain.Student">
select id,name,email,age from student where id=#{StudentId}
</select>
<select id="selectStudents" resultType="domain.Student">
select id,name,email,age from student
</select>
</mapper>
工具类
package 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 java.io.IOException;
import java.io.InputStream;
//工具类:先创建方法创建SqlSessionFactory
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
String config="mybatis-config.xml";
try{
InputStream inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch(IOException e){
e.printStackTrace();
}
}
//创建方法,返回值是SqlSession对象sqlSession
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
实体类
package domain;
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "学生实体{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
Test类
package Dao;
import domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;
import java.util.List;
//selectOne方法
public class MyTest {
@Test
public void testSelectById(){
//1.获取SqlSession对象
SqlSession sqlSession= MyBatisUtil.getSqlSession();
//2.指定sqlId
String sqlId="Dao.StudentDao.selectById";
//3.执行SqlSession的方法,执行SQL语句
Student student = sqlSession.selectOne(sqlId, 001);
System.out.println("查询的结果==="+student);
//4.关闭sqlSession对象
sqlSession.close();
}
//selectList方法
@Test
public void testSelectStudents(){
//1.获取SqlSession对象
SqlSession sqlSession= MyBatisUtil.getSqlSession();
//2.指定sqlId
String sqlId="Dao.StudentDao.selectStudents";
//3.执行SqlSession的方法,执行SQL语句
List<Student> students = sqlSession.selectList(sqlId);
for(Student student:students){
System.out.println(student);
}
//4.关闭sqlSession对象
sqlSession.close();
}
}
测试结果
// testSelectById
查询的结果===学生实体{id=1, name='张磊', email='zl@163.com', age=23}
//testSelectStudents
学生实体{id=1, name='张磊', email='zl@163.com', age=23}
学生实体{id=2, name='陆婷婷', email='luu@163.com', age=22}
学生实体{id=3, name='陆启赛', email='kwh@qq.com', age=19}
注:本地数据库已创建student表,并且添加了数据。