思路
搭建環境 --> 導入MyBatis --> 編寫代碼 --> 測試
代碼示範
環境說明:
JDK 8
MySQL 5.7.19
maven-3.6.0
IDEA
1. 建立資料庫并插入資料
CREATE DATABASE `mybatis`;
USE `mybatis`;
DROP TABLE IF EXISTS `user` (
`id` int(10) AUTO_INCREMENT PRIMARY KEY,
`name` varchar(30) DEFAULT NULL,
`pwd` varchar(30) DEFAULT NULL
) ENGIN=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` (`name`,`pwd`) values ("linux","linux123") ("windows","windows123","macos","macos123");
2. 導入MyBatis相關jar包
org.mybatis
mybatis
3.5.4
mysql
mysql-connector-java
8.0.19
3. 編寫配置檔案
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4. 編寫MyBatis工具類
import org.apache.ibatis.io.Resources;
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();
}
}
//擷取SqlSession連接配接
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
5. 建立實體類
public class User {
private int id;
private String name;
private String pwd;
//有參構造器
//無參構造器
//set方法
//get方法
//toString重寫
}
上面注釋的部分自己寫,或使用IDEA就能生成,在編輯區快捷鍵Alt + Insert,toString要重寫,不然第8步列印會輸出該對象的引用,格式為包名.類名.@八位字元。
6. 編寫Mapper接口類
import com.geek.pojo.User;
import java.util.List;
public interface UserMapper {
List selectUser();
}
7. 編寫Mapper.xml配置檔案
注意:namespace不要寫錯,否則會報錯
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from user
8. 編寫測試類
Junit包測試
public class MyTest {
@Test
public void selectUser() {
SqlSession session = MybatisUtils.getSession();
//方法一:
//List users = session.selectList("com.geek.mapper.UserMapper.selectUser");
//方法二:
UserMapper mapper = session.getMapper(UserMapper.class);
List users = mapper.selectUser();
for (User user: users){
System.out.println(user);
}
session.close();
}
}
9. 運作測試
可能出現的問題
Maven靜态資源過濾問題
src/main/java
***.xml
false
src/main/resources
***.xml
false
第7步Mapper.xml中命名空間寫錯,可能會報類似這樣的錯誤
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for xxdfly.mapping.userMaper.updateUser
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for xxdfly.mapping.userMaper.updateUser
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154)
at test.MybatisTest.main(MybatisTest.java:37)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for xxdfly.mapping.userMaper.updateUser