通用mapper常用注解


通用mapper的作用:

自動實現單表的增刪改查

常用注解使用

@Table

作用:建立實體類和數據庫表之間的對應關系。
默認規則:實體類類名首字母小寫作為表名。Employee 類→employee 表。
用法:在@Table注解的 name 屬性中指定目標數據庫表的表名

@Column

作用:建立實體類字段和數據庫表字段之間的對應關系。
默認規則:
實體類字段:駝峰式命名
數據庫表字段:使用“_”區分各個單詞
用法:在@Column 注解的 name 屬性中指定目標字段的字段名

@Id

通用 Mapper 在執行 xxxByPrimaryKey(key)方法時,有兩種情況。
情況 1:沒有使用@Id 注解明確指定主鍵字段
SELECT emp_id,emp_name,emp_salary_apple,emp_age FROM tabple_emp WHERE emp_id = ?
AND emp_name = ? AND emp_salary_apple = ? AND emp_age = ?
之所以會生成上面這樣的 WHERE 子句是因為通用 Mapper 將實體類中的所有
字段都拿來放在一起作為聯合主鍵。
情況 2:使用@Id 主鍵明確標記和數據庫表中主鍵字段對應的實體類字段

@GeneratedValue

作用:讓通用 Mapper 在執行 insert 操作之后將數據庫自動生成的主鍵值回寫到實
體類對象中。
自增主鍵用法: @GeneratedValue(strategy = GenerationType.IDENTITY)

@Transient

用於標記不與數據庫表字段對應的實體類字段。
例:
@Transient
private String otherThings; //非數據庫表中字段

QBC查詢

概念

Query By Criteria
Criteria 是 Criterion 的復數形式。意思是:規則、標准、准則。在 SQL 語句中相當
於查詢條件。
QBC 查詢是將查詢條件通過 Java 對象進行模塊化封裝。

示例代碼

//目標:WHERE (emp_salary>? AND emp_age<?) OR (emp_salary<? AND emp_age>?)

//1.創建 Example 對象
Example example = new Example(Employee.class);

//設置排序信息
example.orderBy("empSalary").asc().orderBy("empAge").desc();

//設置“去重”
example.setDistinct(true);

//設置 select 字段
example.selectProperties("empName","empSalary");
//***********************
//2.通過 Example 對象創建 Criteria 對象

Criteria criteria01 = example.createCriteria();
Criteria criteria02 = example.createCriteria();

//3.在兩個 Criteria 對象中分別設置查詢條件
//property 參數:實體類的屬性名
//value 參數:實體類的屬性值
criteria01.andGreaterThan("empSalary", 3000).andLessThan("empAge", 25);
criteria02.andLessThan("empSalary", 5000).andGreaterThan("empAge", 30);

//4.使用 OR 關鍵詞組裝兩個 Criteria 對象
example.or(criteria02);

//5.執行查詢
List<Employee> empList = employeeService.getEmpListByExample(example);
for (Employee employee : empList) {
System.out.println(employee);
}

增刪改查


@Service
public class VirtualIpService {
  @Autowired
  IVirtualIPMapper vipMapper;
  public void test(){
    VirtualIpBean vipBean = new VirtualIpBean();
    vipBean.setBeginTime(new Date());
    //(1)mapper基礎接口
    //select接口
      List<VirtualIpBean> vipList = vipMapper.select(vipBean);//根據實體中的屬性值進行查詢,查詢條件使用等號
    VirtualIpBean vip = vipMapper.selectOne(vipBean);//根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號
    List<VirtualIpBean> vipList2 = vipMapper.selectAll();//查詢全部結果,select(null)方法能達到同樣的效果
    VirtualIpBean vip2 = vipMapper.selectByPrimaryKey(1);//根據主鍵字段進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號
    int count = vipMapper.selectCount(vipBean);//根據實體中的屬性查詢總數,查詢條件使用等號
    //insert接口
    int a = vipMapper.insert(vipBean);//保存一個實體,null的屬性也會保存,不會使用數據庫默認值
    int a1 = vipMapper.insertSelective(vipBean);//保存實體,null的屬性不會保存,會使用數據庫默認值
    //update接口
    int b = vipMapper.updateByPrimaryKeySelective(vipBean);//根據主鍵更新屬性不為null的值
    int c = vipMapper.updateByPrimaryKey(vipBean);//根據主鍵更新實體全部字段,null值會被更新
    //delete接口
    int d = vipMapper.delete(vipBean);//根據實體屬性作為條件進行刪除,查詢條件使用等號
    int e = vipMapper.deleteByPrimaryKey(1);//根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性
    //(2)Example方法
    Example example = new Example(VirtualIpBean.class);
    example.createCriteria().andEqualTo("id", 1);
    example.createCriteria().andLike("val", "1");
    //自定義查詢
    List<VirtualIpBean> vipList3 = vipMapper.selectByExample(example);
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM