Mybatis知识小汇(1)——简介、第一个Mybatis
作者:互联网
文章目录
Mybatis
简介
- MyBatis 是一款优秀的持久层框架
- 它支持自定义 SQL、存储过程以及高级映射
- MyBatis免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作
- MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
- MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了[google code](https://baike.baidu.com/item/google code/2346604),并且改名为MyBatis
- 2013年11月迁移到Github。
如何获得Mybatis
-
官网:https://github.com/mybatis/mybatis-3/releases/tag/mybatis-3.5.6
-
github:https://github.com/mybatis/mybatis-3
-
maven仓库:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency>
第一个Mybatis
搭建环境
- 数据库
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE `user`(
id INT PRIMARY KEY NOT NULL,
`name` VARCHAR(20) DEFAULT NULL,
pwd VARBINARY(20) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES(1,'张三','111111'),(2,'李四','222222'),(3,'王五','333333');
-
maven
-
创建一个maven空项目
-
删除src目录
-
导入依赖
<dependencies> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!-- mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
-
-
创建一个模块
-
编写mybatis-config.xml (resources目录下)
<?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?characterEncoding=utf-8&useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 每一个mapper.xml都需要在配置文件中注册--> <mappers> <mapper resource="com/wjq/dao/UserMapper.xml"/> </mappers> </configuration>
-
-
编写mybatis工具类
//sqlSessionFactory ---> sqlSession public class MybatisUtils { static { try{ // 第一步,获取SqlSessionFactory的对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (Exception e){ e.printStackTrace(); } } // 从sqlSessionFactory获取SqlSession对象,sqlSession完全包含了面向数据库执行所需的所有方法 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
-
编写代码
-
实体类
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; } }
-
mapper/dao接口
public interface UserMapper { List<User> getUserList(); }
-
-
mapper.xml (类似于mapper的实现类)
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace绑定对应的接口--> <mapper namespace="com.www.dao.UserMapper"> <!-- select查询语句--> <select id="getUserList" resultType="com.www.pojo.User"> select * from user </select> </mapper>
-
测试
public class UserMapperTest { @Test public void test(){ //获取SqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //获取UserMapper对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } //关闭 sqlSession.close(); } }
注意
防止resources资源导出失败,在pom.xml中配置
<!-- 防止资源导出失败--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
如有不对的地方欢迎指出,共同进步!
标签:name,小汇,简介,id,mybatis,pwd,Mybatis,public,String 来源: https://blog.csdn.net/weixin_45734378/article/details/113761150