Mybatis學習筆記-2
阿新 • • 發佈:2022-04-08
2、 第一個Mybatis程式
思路:搭建環境->匯入mybatis->編寫程式碼->測試
2.1 搭建環境
- 搭建資料庫:
CREATE SCHEMA `mybatis` DEFAULT CHARACTER SET utf8; CREATE TABLE `mybatis`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NOT NULL, `pwd` VARCHAR(45) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARACTER SET = utf8; INSERT INTO `mybatis`.`user` (`id`, `name`, `pwd`) VALUES ('1', '狂神', '123456'); INSERT INTO `mybatis`.`user` (`id`, `name`, `pwd`) VALUES ('2', '張三', '123456'); INSERT INTO `mybatis`.`user` (`id`, `name`, `pwd`) VALUES ('3', '李四', '123456');
-
新建普通的Maven專案
-
刪除src
-
加入Maven依賴(mybatis,juint,mysql)
2.2 搭建環境
參考官方文件[https://mybatis.org/mybatis-3/zh/index.html
- 編寫mybatis核心配置檔案(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.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=true"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </dataSource> </environment> </environments> <!-- 每一個mapper.xml都需要在該核心配置檔案中註冊--> <mappers> <mapper resource="com/bao/dao/UserMapper.xml"/> </mappers> </configuration>
- 編寫獲取sqlsession的工具類
package com.bao.utils; 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.InputStream; public class MybatisUtils { public static SqlSessionFactory sqlSessionFactor; // 從 XML 中構建 SqlSessionFactory static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactor = new SqlSessionFactoryBuilder().build(inputStream); }catch (Exception e){ e.printStackTrace(); } } // 獲取SqlSession //SqlSession中包含面向資料庫執行SQL命令的所有方法 public static SqlSession getSqlSession(){ return sqlSessionFactor.openSession(); } }
2.3 編寫程式碼
-
實體類
-
Dao介面
package com.bao.dao; import com.bao.pojo.User; import java.util.List; public interface UserDao { List<User> getUserList(); }
-
Dao介面實現類
<?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=繫結一個對應的介面--> <mapper namespace="com.bao.dao.UserDao"> <select id="getUserList" resultType="com.bao.pojo.User"> select * from mybatis.user </select> </mapper>
-
測試
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
// 獲取sqlSession物件
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 執行 Sql語句
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
可能遇到的問題
- 配置檔案沒註冊
- 繫結介面錯誤
- 方法名不對
- 返回型別不對
- Maven匯出資源問題,可在pom中新增以下配置資訊來結局
<!--在build中配置resources,來防止我們資源匯出失敗的問題--> <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>