mybatis項目過程中知識點記錄

關於配置文件

idea中mybatis可以自動將SQL中查出來的帶下劃線的字段,轉換爲駝峯標誌
mybatis.configuration.map-underscore-to-camel-case=true

 

mybits查詢返回集合

Sql查詢需要返回多行數據,可以使用List集合來進行存儲。首先需要在Mapper.xml文件中寫上需要的sql語句,例如:

  <select id="selectAllBusiness" resultMap="BaseResultMap" >
    select * from tb_business
  </select>

然後在Mapper文件中定義調用此sql語句的方法:

    List<Business> selectAllBusiness();

在service的實現類中使用

List<Business> businessList;
businessList = businessMapper.selectAllBusiness();

就可以把查詢出來的多行記錄放入到List集合中了

 

Mybatis+mysql執行插入操作返回插入記錄的Id

只需要在mapper文件的insert操作上加上useGeneratedKeys="true" keyProperty="userId"  

keyProperty是user類的屬性主鍵名稱

<insert id="insertSelective" useGeneratedKeys="true" keyProperty="userId"  parameterType="com.lanqiao.jd.entity.User" >
 insert into tb_user (user_id, user_name, password, 
      balance, phone_number)
    values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{balance,jdbcType=DECIMAL}, #{phoneNumber,jdbcType=VARCHAR})
  </insert>

執行Insert操作後直接在service層使用

 int userId = user.getUserId();

就可以獲取到插入的記錄的id了

 

Mybatis SpringBoot 註解 事務管理

首先在Application上添加@EnableTransactionManagement註解

@EnableTransactionManagement
@SpringBootApplication
public class JdApplication {

    public static void main(String[] args) {
        SpringApplication.run(JdApplication.class, args);
    }

}

然後再需要進行事務管理的方法上添加    @Transactional

    @Override
    @Transactional
    public Result register(User user) {}

注意:如果在方法中使用了try-catch操作,則註解不會生效

 

Mybatis多表連接查詢

首先先寫出正確的sql語句,因爲多表連接通常較爲複雜,所以需要在Mysql裏面先測試。這次使用到了四個表(數據庫設計有一點問題,血的教訓)

查詢語句如下,實現的模糊查詢:

  select a.product_id,product_name, product_price ,product_img_url ,business_name ,ifnull(comment_count,0)as comment_count from
  (select p.product_id,p.product_name,p.product_price,p.product_img_url,b.business_name
  from tb_product p NATURAL join tb_business b) as a
left join
  (select p.product_id, count(*) as comment_count
  from tb_comment c,tb_product p,tb_product_item pi
  where c.product_item_id = pi.product_item_id and pi.product_id = p.product_id
  GROUP BY p.product_id
  ) as b
on a.product_id = b.product_id
  where product_name LIKE '%蘋果%'

根據此查詢語句的查詢結果,新建一個實體類,我命名爲ProdBusiComm(這是不好的命名習慣,別學我,沒有見名知意),

實體類中的屬性和查詢結果的列一一對應:

public class ProdBusiComm {
    private Integer productId;

    private String productName;

    private Integer productPrice;

    private String productImgUrl;

    private String businessName;

    private  Integer commentCount;

    //省略getter and setter
}

注意數據庫中的查詢語句和屬性名稱的駝峯對應。

然後在任意一個Mapper中添加:

  <!-- 主頁->模糊查詢 -->
  <select id="fuzzyQueryProduct" resultType="com.lanqiao.jd.entity.ProdBusiComm" parameterType="java.lang.String" >
  select a.product_id,product_name, product_price ,product_img_url ,business_name ,ifnull(comment_count,0)as comment_count from
  (select p.product_id,p.product_name,p.product_price,p.product_img_url,b.business_name
  from tb_product p NATURAL join tb_business b) as a
left join
  (select p.product_id, count(*) as comment_count
  from tb_comment c,tb_product p,tb_product_item pi
  where c.product_item_id = pi.product_item_id and pi.product_id = p.product_id
  GROUP BY p.product_id
  ) as b
on a.product_id = b.product_id
  where product_name LIKE concat(concat("%",#{name,jdbcType=VARCHAR}),"%")
  </select>

注意resutlType以及模糊查詢在mapper.xml中描寫方式。這樣,查詢結果就可以用ProdBusiComm來存放。

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章