1、檔案結構
一共涉及3個檔案夾8個檔案,心累…
注意config檔案夾和lib檔案夾需要設定為資源檔案夾,不然找不到檔案
2、依賴下載下傳
放入lib檔案夾
mybatis:
https://github.com/mybatis/mybatis-3/releasesmysql-connector:
https://dev.mysql.com/downloads/connector/j/3、資料準備
demo的資料庫下有一個names的資料表,字段類型和資料如下
mysql> desc names;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | UNI | NULL | |
| age | int(10) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | 大紅 | 27 |
| 3 | 大壯 | 24 |
| 4 | 秀英 | 25 |
+----+--------+------+
4、Person類編寫
其實就是一個普通的java類
Person.class
public class Person {
private Integer id;
private String name;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String toString(){
return "[Person] id: "+ id + " name: " + name + " age :"+ age;
}
}
5、配置映射器
方式一 檔案配置映射器:
映射接口 PersonMapper.class
public interface PersonMapper {
public Person getPersonById(Integer id);
}
映射檔案 PersonMapper.xml
<?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">
<!--
1.PersonMapper為命名空間,為映射接口的全類名
2.id為唯一辨別,與PersonMapper接口中的對應方法名相同
3.resultType為查詢後的傳回類型
4.#{id}指從傳遞過來的參數中取出id值
-->
<mapper namespace="PersonMapper">
<select id="getPersonById" resultType="Person">
select id,name, age from names where id = #{id}
</select>
</mapper>
方式二 注解配置映射器:
PersonMapperAnnoation.class
import org.apache.ibatis.annotations.Select;
public interface PersonMapperAnnoation {
@Select("select id, name, age from names where id = #{id}")
public Person getPersonById(Integer id);
}
這樣看來,方式2更為簡潔和清晰
6、資料庫配置填入資料庫的位址,賬号,密碼
配置映射器,可以使用兩種方式resource 和 class
<?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">
<!-- 配置事務管理器,采用的是JDBC的管理器方式 -->
<transactionManager type="JDBC"/>
<!-- POOLED代表采用MyBatis内部提供的連接配接池方式 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 引入映射器 包名+檔案名-->
<mappers>
<mapper resource="PersonMapper.xml"/>
<mapper class="PersonMapperAnnoation"/>
</mappers>
</configuration>
7、代碼測試
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 mybatisDemo {
public static void main(String[] args) throws IOException{
//1.根據MyBatis的配置檔案,即mybatis-config.xml建立SqlSessionFactory
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.擷取session執行個體開啟會話,其能直接執行*已經映射的SQL語句*
SqlSession session = sqlSessionFactory.openSession();
try {
// 方式1 查詢
//3.擷取接口的實作類對象
PersonMapper personMapper = session.getMapper(PersonMapper.class);
//4.執行查詢操作
Person person = personMapper.getPersonById(2);
System.out.println(person);
// 方式2 查詢
//3.擷取接口的實作類對象
PersonMapperAnnoation personMapperAnnoation = session.getMapper(PersonMapperAnnoation.class);
//4.執行查詢操作
Person person1 = personMapperAnnoation.getPersonById(2);
System.out.println(person1);
} finally {
//4.關閉會話session
session.close();
}
}
//根據MyBatis的配置檔案,即mybatis-config.xml建立SqlSessionFactory
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
}
/*
兩種方式都能查到資料
[Person] id: 2 name: 大紅 age :27
[Person] id: 2 name: 大紅 age :27
*/
參考
(二)MyBatis學習筆記-HelloWorld