回顾
首先得掌握这些
官方文档:https://mybatis.org/mybatis-3/zh/index.html
如何获得Mybatis?
<!--https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency>
数据持久化
为什么需要持久化?
Dao层,service层,Controller层
最重要的一点:使用的人多
思路:搭建环境–>导入Mybatis–>编写代码–>测试
搭建数据库
CREATE DATABASE `mybatis`; USE `mybatis`; CREATE TABLE `user`( `id` INT(20)NOT NULL, `name` VARCHAR(30) DEFAULT NULL, `pwd` VARCHAR(30) DEFAULT NULL, PRIMARY KEY(`id`) )ENGINE = INNODB DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES(1,'张三','123456'),(2,'李四','123456'),(3,'王五','123456');
新建项目
<!--导入依赖--> <dependencies> <!--Mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
<?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? useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册--> <mappers> <mapper resource="文件路径"/> </mappers> </configuration>
public class MybatisUtil { //sqlSessionFactory (工厂函数) -- >(构建sqlSession) sqlSession private static SqlSessionFactory sqlSessionFactory; static { //获取Mybatis第一步获取sqlSessionFactory的对象 String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //既然有了SqlSessionFactory,顾名思义,我们就可以从中获得SqlSession的实例了 //SqlSession 完全包含了面向数据库执行SQL命令所需的所有方法 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; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
/** * 获取所有的User对象 * * @return */ List<User> getUserList();
<?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.zyx.dao.UserDao"> <!--这里的id对应方法名--> <select id="getUserList" resultType="com.zyx.pojo.User"> select * from mybatis.user;//这里写sql语句 </select> </mapper>
注意点:有可能找不到配置文件,maven项目可以在pom.xml文件中写入下面代码
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
@Test public void test() { //获得SqlSession对象,执行Sql //第一步 方法一: SqlSession sqlSession = MybatisUtil.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
结果
User{id=1, name='张三', pwd='123456'} User{id=2, name='李四', pwd='123456'} User{id=3, name='王五', pwd='123456'}