【Mybatis学习笔记1】demo01-实现简单的select查询语句
一、什么是Mybatis
MyBatis 是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。
MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
注:官网文档:https://mybatis.org/mybatis-3/zh/index.html
二、demo具体操作
1.本地mysql数据库,创建数据库mybatis,表user,字段分别是id(int)、name(varchar)、pwd(varchar);
2.IDEA中创建一个module;
3.pom.xml文件中先后导入依赖:mysql-connector-java、mybatis、junit
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> </dependency> </dependencies>
4.创建工具类MybatisUtils
package com.coke.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; 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 (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
5.创建实体类User
package com.coke.pojo; // 实体类 public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
6.创建实体类对应的接口:查询全部用户接口
package com.coke.dao; import com.coke.pojo.User; import java.util.List; public interface UserMapper { // 查询全部用户 List<User> getUserList(); }
7.创建与接口对应的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"> <!-- namespace要和dao.Mapper的接口名一致 --> <mapper namespace="com.coke.dao.UserMapper"> <!-- id 对应namespace中的方法名 resultType sql执行的返回值 --> <select id="getUserList" resultType="com.coke.pojo.User"> select * from mybatis.user </select> </mapper>
8.配置jdbc参数
<?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?useUnicode=true&serverTimezone=Asia/Shanghai&allowMultiQueries=TRUE" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- org.apache.ibatis.binding.BindingException: Type interface com.coke.dao.UserDao is not known to the MapperRegistry. --> <mappers> <mapper resource="com/coke/dao/UserMapper.xml" /> </mappers> </configuration>
9.编写junit测试类
在test目录下创建与UserDao同级的测试类UserDaoTest
package com.coke.dao; import com.coke.pojo.User; import com.coke.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; public class UserDaoTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user : userList){ System.out.println(user); } sqlSession.commit(); } }
10.运行测试类,得到结果:
User{id=1, name='张三', pwd='123456'} User{id=2, name='李四', pwd='111111'} User{id=3, name='王五', pwd='666666'}
其中会有很多坑,下一篇会实现有where条件的查询、insert、update、delete。几行代码,几行配置,轻松实现!
---------------------------------------------------
立足软件测试领域,并重新定义测试!
---------------------------------------------------