虛擬函式詳解
阿新 • • 發佈:2022-04-17
1、建立測試表
2、建立maven專案
3、修改pom檔案 引入依賴 mybatis mysql驅動 測試程式碼 junit <build>中加入資源外掛<!--依賴列表--> <dependencies> <!--單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <versionpom檔案>4.11</version> <scope>test</scope> </dependency> <!--mybatis依賴--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> </dependencies> <build> <!--資源外掛 處理src/main/java目錄下的xml檔案--> <resources> <resource> <!--所在目錄--> <directory>src/main/java</directory> <!--包括目錄下的properties,xml檔案會被掃描--> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <!--編譯外掛使用版本--> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
4、建立實體類 student 定義屬性
package com.pojo; /** * @author lbon * @create 2022-04-13 21:49 */ public class Student { private Integer id; private String name; private String email; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + ", age=" + age + '}'; } }student類
5、建立Dao介面 定義操作資料庫方法
package com.dao; import com.pojo.Student; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @author lbon * @create 2022-04-13 21:51 */ public interface StudentDao { /** * 獲取學生列表 * @return */ List<Student> getStudentList(); /** * 新增學生 * @param student * @return */ Boolean insertStudent(@Param("student") Student student); }Dao介面 6、建立xml檔案(mapper檔案) 書寫sql語句 mybatis推薦將sql語句和java程式碼相分離 mapper檔案:定義和dao介面在同一目錄 一個表對應一個mapper檔案
<?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"> <!--<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 約束檔案 約束檔案作用:定義和限制當前檔案中可以使用的標籤和屬性,以及標籤出現的順序 mapper是根標籤 namespace 名稱空間 必須有值 不能為空 唯一值 推薦使用Dao介面的全限定名稱 作用:參與識別sql語句的作用 在mapper中可以寫 <insert> <update> <delete> <select> 等標籤 --> <mapper namespace="com.dao.StudentDao"> <!--若果傳給mybatis的物件 使用#{屬性名}獲取此屬性的值 mybatis執行此屬性 對應get 操作 --> <insert id="insertStudent"> insert into student values(#{id},#{name},#{email},#{age}) </insert> <!--id 標識執行sql語句的唯一標識,是一個自定義字串 推薦使用dao介面中的方法名稱 resultType 告訴mybatis執行sql語句 把資料賦值給哪個型別的java物件 resultType的使用物件的全路徑 --> <select id="getStudentList" resultType="com.pojo.Student"> select * from student </select> </mapper>mapper檔案 7、建立mybatis的主配置檔案(xml檔案) 一個專案僅一個 放在resources下 1)定義建立連線例項的資料來源物件DateSource物件 2)指定其他mapper的位置
<?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> <!--設定日誌--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!--配置資料來源:建立Connect物件--> <dataSource type="POOLED"> <!--驅動內容--> <property name="driver" value="com.mysql.jdbc.Driver"/> <!--資料庫地址--> <!--html字元實體不能直接使用會被誤認為標籤 需要換成對應的代替符號--> <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?useUnicode=true&characterEncodeing=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--指定其他mapper檔案位置 為了找到檔案中的可執行sql語句 --> <mappers> <!--使用mapper的resource 指定mapper檔案的路徑 從target/classes路徑開始的 注意:Resource="mapper檔案的路徑,使用/分割路徑 一個 mapper resource指定一個mapper檔案" --> <mapper resource="com/dao/StudentDao.xml"/> </mappers> </configuration>mybatis主配置檔案 8、建立測試內容 main方法 或單元測試Junit
import com.pojo.Student; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * @author lbon * @create 2022-04-14 15:06 */ public class MyTest { @Test public void testSelectStudentList() { //呼叫mybatis讀取學生資訊 //mybatis核心類 SqlSessionFactory //定義mybatis主配置檔案位置,從類路徑開始的相對路徑 String resource = "mybatis.xml"; try { //讀取配置檔案 使用mybatis中Resources類 InputStream inputStream = Resources.getResourceAsStream(resource); //建立SqlSessionFactory物件 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //獲取SqlSession物件 SqlSession sqlSession = sqlSessionFactory.openSession(); //指定要執行的sql語句id namespace+"."+ mapper語句標籤id 通過sqlSession方法獲取sql語句 List<Student> objects = sqlSession.selectList("com.dao.StudentDao.getStudentList", Student.class); for (Student s : objects) { System.out.println(s.toString()); } //關閉sqlSession物件 sqlSession.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }測試方法