代码下载https://www.lanzouw.com/i28Jhw80b3e
这里使用的数据库是MySQL。这博客只演示了查询操作,其他操作会在其他的博客中继续写
先看目录结构,再看下面的内容,更容易理解。
1、创建数据库mybatis,并在数据库中创建User表
说明:创建表格User,包含一个自增列、姓名、年龄, 数据库引擎使用INNODB
DROP DATABASE IF EXISTS mybatis;
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE User(
id int(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL,
age int(3),
PRIMARY KEY (id)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2、导入数据
USE mybatis;
INSERT INTO User(name, age)
VALUES
("张三", 12),
("李四", 14),
("王华", 10),
("lucy", 20),
("mary", 22);
3、创建maven项目, 导入mysql驱动和mybatis坐标,并创建实体类User
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
package com.changqing.pojo;
//实体类,对应数据表User
public class User {
private int id;
private String name;
private int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4、创建mybatis工具类MybatisUtils,用于返回SqlSession对象
package com.changqing.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.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5、创建UserDao接口和映射文件UserMapper.xml
5.1 创建UserDao接口:在com.changqing.dao包先创建UserDao接口,定义查询的方法。
package com.changqing.dao;
import com.changqing.pojo.User;
import java.util.List;
public interface UserDao {
public List<User> getUserList();
}
5.2 创建UserMapper.xml文件:在resources目录下,创建文件夹mapper, 在mapper目录下创建UserMapper.xml文件用于绑定UserDao接口。绑定UserDao后,这个文件的实际作用相当于UserDao的实现类。
<?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">
<!--这里的namesapce的值位UserDao,即将这个UserMapper配置文件绑定到UserDao接口上,相当于这个xml文件实现UserDao接口, 其中的id对应UserDao中的方法名-->
<mapper namespace="com.changqing.dao.UserDao">
<select id="getUserList" resultType="com.changqing.pojo.User">
select * from User
</select>
</mapper>
6、创建mybatis的主配置文件mybatis-config.xml
在resouces目录下创建,这样编译时,编译器会自动将配置文件复制到target目录中。下面的配置文件包含了数据源 datasource和映射器mapper。请修改下面的用户名和密码为自己的mysql登录账户。
<?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核心配置文件-->
<configuration>
<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?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="199604"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--将UserMapper注册到mybatis中,-->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
7、编写测试类
package test;
import com.changqing.dao.UserDao;
import com.changqing.pojo.User;
import com.changqing.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
import java.util.List;
public class TestUser {
@Test
public void testUser(){
//获取SqlSession
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for(User user: userList){
System.out.println(user);
}
sqlSession.close();
}
}