1.1 搭建数据库
DROP DATABASE IF EXISTS `mybatis`;
CREATE DATABASE IF NOT EXISTS `mybatis`;
USE `mybatis`;
DROP DATABASE IF EXISTS `user`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL,
`name` VARCHAR(20) DEFAULT NULL,
`pwd` VARCHAR(32) DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user` (`id`,`name`,`pwd`)
VALUES (1,'张三',MD5('123456')),
(2,'李四' ,MD5('111')),
(3,'王五',MD5('222'));
1.2 添加maven依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
1.3 配置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>
<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"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
1.4 定义实体类
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
1.5 编写工具类
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 --> sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resources = "mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resources);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
1.6 定义dao接口
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
1.7 编写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">
<!--绑定对应的dao接口-->
<mapper namespace="com.lu.dao.UserDao">
<!--select查询语句-->
<!--id对应方法名-->
<!--resultType 返回值类型-->
<select id="getUserList" resultType="com.lu.entity.User">
select *
from user;
</select>
</mapper>
- namespace一定要正确绑定到dao上
- id 就是 dao中的方法
- resultType sql语句执行返回值类型
- parameterType 传递参数类型
1.8 注册mapper到配置文件中
<?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"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="lzf990507"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lu/dao/UserMapper.xml"/>
</mappers>
</configuration>
1.9 配置maven过滤
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
1.10 测试
import com.lu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.getUserList().forEach(System.out::println);
}
}
User(id=1, name=张三, pwd=e10adc3949ba59abbe56e057f20f883e)
User(id=2, name=李四, pwd=698d51a19d8a121ce581499d7b701668)
User(id=3, name=王五, pwd=bcbe3365e6ac95ea2c0343a2395834dd)