Git for Windows下載、安裝步驟
阿新 • • 發佈:2021-06-23
一、第一個Mybatis案例
1.搭建資料庫環境
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 DEFAULTCHARSET=utf8; insert into `user`(`id`,`name`,`pwd`) values (1,'Autumn27','123456'),(2,'法外狂徒張三','333333'),(3,'西裝暴徒李四','444444')
建立好資料庫後,重新整理資料庫
2.匯入Mybatis相關jar包,推薦用Maven進行專案管理
Maven倉庫:https://mvnrepository.com/
<dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.0-M1</version> <scope>test</scope> </dependency> </dependencies>
相關jar包:
- mybatis需要jar包:mybatis-3.5.7.jar(必備)
- mysql驅動jar包:mysql-connector-java-8.0.25.jar(必備)
- JUnit jar包:junit-jupiter-api-5.8.0-M1.jar(推薦,用於測試程式碼)
3.編寫mybatis核心配置檔案
MyBatis 的xml配置檔案(configuration XML)中包含了對 MyBatis 系統的核心設定,包含獲取資料庫連線例項的資料來源(DataSource)和決定事務作用域和控制方式的事務管理器(TransactionManager)
編寫配置檔案mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?> <!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?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/autumn/mapper/UserMapper.xml"/> </mappers> </configuration>
4.編寫工具類檔案MybatisUtils.java
//工具類 sqlSessionFactory 生產 sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { //獲取SqlSessionFactory 物件 String resource = "mybatis-config.xml"; InputStream 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(); } }
5.建立實體類及其對應Mapper介面
實體類User.java
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 + '\'' + '}'; } }
UserMapper.java
public interface UserMapper { //查詢所有使用者 List<User> getUserList(); }
6.編寫UserMapper.xml,xml檔案中存放對應的Sql語句
<?xml version="1.0" encoding="UTF8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.autumn.mapper.UserMapper"> <!-- id 等價於 重寫介面的方法名 --> <!-- select標籤 內寫 sql語句 --> <!-- 查詢所有使用者 --> <select id="getUserList" resultType="com.autumn.pojo.User"> select * from mybatis.user; </select> </mapper>
其中<mapper namespace="com.autumn.mapper.UserMapper"> 用來繫結xml對應的Mapper介面
有關select相關語句在Mybatis中文文件下的XML 對映器檔案有說明。
7.編寫測試檔案UserMapperTest.java
查詢所有使用者
@Test
public void testGetUserList(){
//獲取sqlSession物件
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
//執行
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
}finally {
//關閉sqlSession
sqlSession.close();
}
}