mybatis學習之路(一)IDE中mybatis環境的搭建並顯示資料庫中一個表中的所有資訊
阿新 • • 發佈:2019-02-14
①在IDE中建立Maven web專案
②匯入mybatis jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
③在專案的resources目錄下新建mybatis-config.xml配置檔案
配置檔案的內容可以參考mybatis原始碼包中的配置檔案
mybatis原始碼包的下載地址:
具體路徑為:
<?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/javajdbctest"/> <property name="username" value=""/> <property name="password" value=""/> </dataSource> </environment> </environments> </configuration>
④獲取資料庫最最核心的類SqlSession
public SqlSession getSqlSession() throws IOException { //通過配置檔案獲取資料庫連線資訊 Reader reader = org.apache.ibatis.io.Resources.getResourceAsReader("mybatis-config.xml"); //通過配置資訊構建一個SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //通過sqlSessionFactory開啟一個數據庫會話 SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession;}
⑤編寫customer簡單java類
public class Customer { private int id; private String name; private String address; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Customer() { } public Customer(int id, String name, String address, String phone) { this.id = id; this.name = name; this.address = address; this.phone = phone; } @Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", address='" + address + '\'' + ", phone='" + phone + '\'' + '}'; } }
⑥資料庫的中customer表
命名不是很規範(用於測試,忽略不計)
⑦建立資料庫與Javabean關聯的配置檔案
同樣可以參考在步驟③中的路徑下的User.xml檔案
新建CustomerMapper.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">
<!--namespace相當於包的含義-->
<mapper namespace="Customer">
<!--type是customer類所在包的路徑-->
<resultMap type="algorithm.offer.pojo.Customer" id="CustomerResult">
<id column="id" jdbcType="INTEGER" property="id"/><!--主鍵用id-->
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="address" jdbcType="VARCHAR" property="address"/>
<result column="phone" jdbcType="VARCHAR" property="phone"/>
</resultMap>
<!--id為此SQL語句的唯一標識,resultMap與resultMap相對應-->
<select id="findCustomerList" parameterType="long" resultMap="CustomerResult">
SELECT id,name,address,phone FROM customer
</select>
</mapper>
⑧mybatis-config.xml與CustomerMapper.xml的關聯
將以下程式碼寫在mybatis-config.xml檔案中:
<mappers>
<mapper resource="MybatisMapper/CustomerMapper.xml"/>
</mappers>
⑨編寫DAO層Service層
DAO層
/**
* Customer的DAO層
*/
public class CustomerDAO {
DBAccess dbAccess = new DBAccess();
/**
* 查詢所有Customer
* @return
*/
public List<Customer> findCustomerList(){
List<Customer> customerList = new ArrayList<>();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
customerList = sqlSession.selectList("Customer.findCustomerList");
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
return customerList;
}
}
service層
public class CustomerService {
/**
* 查詢所有消費者資訊
* @return
*/
public List<Customer> showCustomer(){
CustomerDAO customerDAO = new CustomerDAO();
return customerDAO.findCustomerList();
}
}
⑩測試
@Test
public void showCustomer() throws Exception {
CustomerService customerService = new CustomerService();
for (Customer customer:customerService.showCustomer()) {
System.out.println(customer.toString());
}
}