搭建文件目錄
image.png
搭建步驟
- 鏈接數(shù)據(jù)庫并在pom.xml中導入Mybatis以及Mysql的jar包
- 在父工程中統(tǒng)一導入依賴
- 父工程pom.xml文件
<?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>idea_Mybatis_study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis_01</module>
</modules>
<!-- 導入依賴-->
<dependencies>
<!-- mysql-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</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>
<!-- 在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>
</project>
- 子工程pom.xml文件
<?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">
<parent>
<artifactId>idea_Mybatis_study</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis_01</artifactId>
<!-- 在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>
</project>
- 建造工具類:獲取sqlSessionFactory類
package com.howe.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.IOException;
import java.io.InputStream;
//SqlSessionFactory--->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis 1.獲取sqlSessionFactory對象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 既然有了 SqlSessionFactory孵稽,顧名思義,我們可以從中獲得 SqlSession 的實例乎澄。
// SqlSession 提供了在數(shù)據(jù)庫執(zhí)行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession() {
// SqlSession sqlSession = sqlSessionFactory.openSession();
// return sqlSession;
return sqlSessionFactory.openSession();
}
}
- 編寫mybatis-config.xml配置文件
- 鏈接數(shù)據(jù)庫
- 注冊每一個Mapper.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/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="1998"/>
</dataSource>
</environment>
</environments>
<!--每一個Mapper.xml文件都需要注冊,注意中間分割不是“.”-->
<mappers>
<mapper resource="com/howe/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
- 實體類書寫
package com.howe.pojo;
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;
}
<!--此處省略getter()甲捏、setter()方法-->
}
- 接口書寫
package com.howe.dao;
import com.howe.pojo.User;
import java.util.List;
public interface UserDao {//類名稱建議寫成和相應的xml文件名相同--->UserMapper
List<User> getUserList();
}
- Mapper.xml文件書寫--->代替原來的實現(xiàn)類
<!-- 名字要盡可能和接口名稱相同-->
<?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.howe.dao.UserDao">
<!--id 綁定方法,resultType綁定方法返回值類型-->
<select id="getUserList" resultType="com.howe.pojo.User">
<!-- 示例執(zhí)行的sql語句-->
select * from mybatis.user
</select>
</mapper>
- 測試文件
package com.howe.dao;
import com.howe.pojo.User;
import com.howe.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void Test(){
//第一步獲得getSqlSession對象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//執(zhí)行Sql 方式一:getMapper
// UserDao mapper = sqlSession.getMapper(UserDao.class);
// List<User> userList = mapper.getUserList();
//方式二:老方法selectList
List<User> userList = sqlSession.selectList("com.howe.dao.UserDao.getUserList");
//輸出結(jié)果
for (User user : userList) {
System.out.println(user);
}
//關閉sqlSession
sqlSession.close();
}
}
常見問題
1.配置文件沒有注冊
2.綁定接口錯誤(沒有namespace)
3.方法名存在問題
4.返回類型存在問題
5.maven導出資源問題
重點內(nèi)容
image.png
image.png