1.1 搭建数据库

  1. DROP DATABASE IF EXISTS `mybatis`;
  2. CREATE DATABASE IF NOT EXISTS `mybatis`;
  3. USE `mybatis`;
  4. DROP DATABASE IF EXISTS `user`;
  5. CREATE TABLE `user`(
  6. `id` INT(20) NOT NULL,
  7. `name` VARCHAR(20) DEFAULT NULL,
  8. `pwd` VARCHAR(32) DEFAULT NULL,
  9. PRIMARY KEY(`id`)
  10. )ENGINE=INNODB DEFAULT CHARSET=utf8;
  11. INSERT INTO `user` (`id`,`name`,`pwd`)
  12. VALUES (1,'张三',MD5('123456')),
  13. (2,'李四' ,MD5('111')),
  14. (3,'王五',MD5('222'));

1.2 添加maven依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.mybatis</groupId>
  4. <artifactId>mybatis</artifactId>
  5. <version>3.5.7</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.11</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.projectlombok</groupId>
  15. <artifactId>lombok</artifactId>
  16. <version>1.18.22</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <version>8.0.27</version>
  22. </dependency>
  23. </dependencies>

1.3 配置mybatis文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC"/>
  9. <dataSource type="POOLED">
  10. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  12. <property name="username" value="root"/>
  13. <property name="password" value="123456"/>
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. </configuration>

1.4 定义实体类

  1. import lombok.Data;
  2. @Data
  3. public class User {
  4. private int id;
  5. private String name;
  6. private String pwd;
  7. }

1.5 编写工具类

  1. import org.apache.ibatis.io.Resources;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. // sqlSessionFactory --> sqlSession
  8. public class MybatisUtils {
  9. private static SqlSessionFactory sqlSessionFactory;
  10. static {
  11. String resources = "mybatis-config.xml";
  12. try {
  13. InputStream inputStream = Resources.getResourceAsStream(resources);
  14. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. public static SqlSession getSqlSession() {
  20. return sqlSessionFactory.openSession();
  21. }
  22. }

1.6 定义dao接口

  1. import java.util.List;
  2. public interface UserDao {
  3. List<User> getUserList();
  4. }

1.7 编写mapper文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--绑定对应的dao接口-->
  6. <mapper namespace="com.lu.dao.UserDao">
  7. <!--select查询语句-->
  8. <!--id对应方法名-->
  9. <!--resultType 返回值类型-->
  10. <select id="getUserList" resultType="com.lu.entity.User">
  11. select *
  12. from user;
  13. </select>
  14. </mapper>
  • namespace一定要正确绑定到dao上
  • id 就是 dao中的方法
  • resultType sql语句执行返回值类型
  • parameterType 传递参数类型

1.8 注册mapper到配置文件中

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC"/>
  9. <dataSource type="POOLED">
  10. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  12. <property name="username" value="root"/>
  13. <property name="password" value="lzf990507"/>
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. <mappers>
  18. <mapper resource="com/lu/dao/UserMapper.xml"/>
  19. </mappers>
  20. </configuration>

1.9 配置maven过滤

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. <includes>
  5. <include>**/*.properties</include>
  6. <include>**/*.xml</include>
  7. </includes>
  8. <filtering>true</filtering>
  9. </resource>
  10. <resource>
  11. <directory>src/main/java</directory>
  12. <includes>
  13. <include>**/*.properties</include>
  14. <include>**/*.xml</include>
  15. </includes>
  16. <filtering>true</filtering>
  17. </resource>
  18. </resources>

1.10 测试

  1. import com.lu.utils.MybatisUtils;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.junit.Test;
  4. public class UserDaoTest {
  5. @Test
  6. public void test(){
  7. SqlSession sqlSession = MybatisUtils.getSqlSession();
  8. UserDao mapper = sqlSession.getMapper(UserDao.class);
  9. mapper.getUserList().forEach(System.out::println);
  10. }
  11. }
  1. User(id=1, name=张三, pwd=e10adc3949ba59abbe56e057f20f883e)
  2. User(id=2, name=李四, pwd=698d51a19d8a121ce581499d7b701668)
  3. User(id=3, name=王五, pwd=bcbe3365e6ac95ea2c0343a2395834dd)