乐趣区

关于java:Mybatis学习日志第一天

1. 配置

  • jdk8(jdk1.7 以上均可)
  • Maven 3.6.3
  • MySQL 5.5(mysql5.6 以上均可)
  • 官网文档:https://mybatis.org/mybatis-3/zh/getting-started.html

配置 pom 文档

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <!-- 父工程 -->
 <groupId>org.example</groupId>
 <artifactId>Mybatis-Study</artifactId>
 <packaging>pom</packaging>
 <version>1.0-SNAPSHOT</version>
 <modules>
 <module>mybatis-01</module>
 </modules>
  <!-- 导入依赖 -->
 <dependencies>
   <dependency>
 <!-- 导入数据库依赖 -->
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.49</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>
  <!-- maven 无奈读取 src 目录下的配置文件,退出上面代码保障 maven 能读取到 Usermapper.xmi-->
 <build>
 <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>
 </build>
</project>

2. 连贯数据库

在 resources 下创立一个叫 mybatis-config.xml 文件

<?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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybaits?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
</configuration>
留神:手动连贯数据库可能呈现时区不对的状况
在 url3306 前面退出 /?serverTimezone=GMT 即可

3. 创立类和接口,编写代码

3.1 mybatisUtils 类

package com.kuang.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
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;
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();
 }
 } // 既然有了 SqlSessionFactory,顾名思义,咱们能够从中取得 SqlSession 的实例。org.apache.ibatis.session.SqlSession 提供了在数据库执行 SQL 命令所需的所有办法。你能够通过 SqlSession 实例来间接执行已映射的 SQL 语句。public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();
 }
}

3.2 实体类

package com.kuang.pojo;
public class User {
 private int id;
 private String name;
 private String password;
 public User() {}
 public User(int id, String name, String password) {
 this.id = id;
 this.name = name;
 this.password = password;
 }
 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 getPassword() {return password;}
 public void setPassword(String password) {this.password = password;}
 @Override
 public String toString() {
 return "User{" +
 "id=" + id +
 ", name='" + name + '''+", password='"+ password +''' +
 '}'; }
}

3.3 接口

package com.kuang.dao;
import com.kuang.pojo.User;
import java.util.List;
public interface UserDao {List<User>getUserList();
}

3.4 接口实现类(写在 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">
<mapper namespace="com.kuang.dao.UserDao">
 <select id="getUserList" resultType="com.kuang.pojo.User">
 select * from mybaits.user;
  </select>
</mapper>

每一个 xml 文件都须要在 mybatis-config 的 mappers 中配置

<mappers>
 <mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
#### 3.5 测试类 

package com.kuang.dao;
import com.kuang.pojo.User;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}

## 4. 测试后果
![image.png](/img/bVcKWcF)
附数据库语句
![image.png](/img/bVcKWcM)
退出移动版