mybatis的資料庫連線和mapper代理開發
點選檢視程式碼
package com.itheima.mapper;
import com.itheima.pojo.Brand;
import java.util.List;
public interface BrandMapper {
public List<Brand>selectAll();
}
點選檢視程式碼
package com.itheima.pojo;
/**
* 品牌
*
* alt + 滑鼠左鍵:整列編輯
*
* 在實體類中,基本資料型別建議使用其對應的包裝型別
*/
public class Brand {
// id 主鍵
private Integer id;
// 品牌名稱
private String brandName;
// 企業名稱
private String companyName;
// 排序欄位
private Integer ordered;
// 描述資訊
private String description;
// 狀態:0:禁用 1:啟用
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
點選檢視程式碼
<?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">
<mapper namespace="com.itheima.mapper.BrandMapper">
<!--
資料庫表的欄位名稱 和 實體類的屬性名稱 不一樣,不能自動封裝資料
*起別名
*resultMap
-->
<!--
id:唯一標識
type:對映型別,支援別名
-->
<resultMap id="brandResultMap" type="brand">
<!--
id:主鍵對映
result:一般欄位
column:表列名
property:實體類屬性名
-->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
<!--<select id="selectAll" resultType="brand">
select id,brand_name as brandName,company_name as companyName,ordered,description,status
from tb_brand;
</select>-->
<!--<select id="selectAll" resultType="brand">
select *
from tb_brand;
</select>-->
</mapper>
點選檢視程式碼
package com.itheima.test;
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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;
public class MyBatisTest {
@Test
public void testSelectAll() throws IOException {
//1.獲取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.獲取SqlSession物件
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.獲取Mapper介面代理物件
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.執行方法
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
//5.釋放
sqlSession.close();
}
}