MyBatis中多條件查詢商品的三種方法及區(qū)別
一、Sql語句設(shè)置多個參數(shù)有幾種方式
1、散裝參數(shù):需要使用@Param標(biāo)記Sql語句中占位符處的名稱例如 #{name}
2、實體類封裝參數(shù)
只需要保證Sql中的參數(shù)名和實體類屬性名對應(yīng)上,即可設(shè)置成功
BrandMapper.xml中的SQL語句不用動,把TestBrandMapper中的代碼修改即可
3、Map集合
只需要保證Sql中的參數(shù)名和Map集合的鍵的名稱對應(yīng)上,即可設(shè)置成功。
BrandMapper.xml中Sql語句不用改
定義一個默認(rèn)的map即可
二、代碼附上
0、pom.xml中依賴
<dependencies> <!--mybatis 依賴--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!--mysql 驅(qū)動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--junit 單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- 添加slf4j日志api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <!-- 添加logback-classic依賴 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- 添加logback-core依賴 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> </dependencies>
1、數(shù)據(jù)庫創(chuàng)建
-- 刪除tb_brand表 drop table if exists tb_brand; -- 創(chuàng)建tb_brand表 create table tb_brand ( -- id 主鍵 id int primary key auto_increment, -- 品牌名稱 brand_name varchar(20), -- 企業(yè)名稱 company_name varchar(20), -- 排序字段 ordered int, -- 描述信息 description varchar(100), -- 狀態(tài):0:禁用 1:啟用 status int ); -- 添加數(shù)據(jù) insert into tb_brand (brand_name, company_name, ordered, description, status) values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0), ('華為', '華為技術(shù)有限公司', 100, '華為致力于把數(shù)字世界帶入每個人、每個家庭、每個組織,構(gòu)建萬物互聯(lián)的智能世界', 1), ('小米', '小米科技有限公司', 50, 'are you ok', 1);
2、Brand實體類
package com.ligong.popj; public class Brand { // id 主鍵 private Integer id; // 品牌名稱 private String brandName; // 企業(yè)名稱 private String companyName; // 排序字段 private Integer ordered; // 描述信息 private String description; // 狀態(tài):0:禁用 1:啟用 private Integer status; public Brand() { } public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) { this.id = id; this.brandName = brandName; this.companyName = companyName; this.ordered = ordered; this.description = description; this.status = status; } /** * 獲取 * @return id */ public Integer getId() { return id; } /** * 設(shè)置 * @param id */ public void setId(Integer id) { this.id = id; } /** * 獲取 * @return brandName */ public String getBrandName() { return brandName; } /** * 設(shè)置 * @param brandName */ public void setBrandName(String brandName) { this.brandName = brandName; } /** * 獲取 * @return companyName */ public String getCompanyName() { return companyName; } /** * 設(shè)置 * @param companyName */ public void setCompanyName(String companyName) { this.companyName = companyName; } /** * 獲取 * @return ordered */ public Integer getOrdered() { return ordered; } /** * 設(shè)置 * @param ordered */ public void setOrdered(Integer ordered) { this.ordered = ordered; } /** * 獲取 * @return description */ public String getDescription() { return description; } /** * 設(shè)置 * @param description */ public void setDescription(String description) { this.description = description; } /** * 獲取 * @return status */ public Integer getStatus() { return status; } /** * 設(shè)置 * @param status */ public void setStatus(Integer status) { this.status = status; } public String toString() { return "Brand{id = " + id + ", brandName = " + brandName + ", companyName = " + companyName + ", ordered = " + ordered + ", description = " + description + ", status = " + status + "}"; } //省略 setter and getter。自己寫時要補全這部分代碼 }
3、BrandMap接口
package com.ligong.mapper; import com.ligong.popj.Brand; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface BrandMapper { List<Brand> selectByCondition1( @Param("status")int status, @Param("companyName") String companyName, @Param("brandName") String brandName); List<Brand> selectByCondition2(Brand brand); List<Brand> selectByCondition3(Map map); }
4、BrandMapper.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"> <mapper namespace="com.ligong.mapper.BrandMapper"> <select id="selectByCondition1" resultType="com.itheima.popj.Brand"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName}; </select> <select id="selectByCondition2" resultType="com.itheima.popj.Brand"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName}; </select> <select id="selectByCondition3" resultType="com.itheima.popj.Brand"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName}; </select> </mapper>
5、封裝好的連接數(shù)據(jù)庫的工具類
package com.ligong.util; 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 SqlSessionUtils { private static SqlSession sqlSession=null; private SqlSessionUtils() { } static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSession; } }
6、Test類中的特使代碼
package com.ligong.mapper; import com.itheima.popj.Brand; import com.itheima.popj.Dept; import com.itheima.util.SqlSessionUtils; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; public class TestBrandMapper { private static BrandMapper mapper = null; private static SqlSession sqlSession = null; @Before public void init() { sqlSession = SqlSessionUtils.getSqlSession(); mapper = sqlSession.getMapper(BrandMapper.class); } @After public void finallyOp() { sqlSession.close(); } @Test public void selectByCondition3() { int status = 1; String brand_name = "華"; String company_name = "華"; String brandName = "%" + brand_name + "%"; String companyName = "%" + company_name + "%"; Map map = new HashMap(); map.put("status", status); map.put("brandName", companyName); map.put("companyName", brandName); List<Brand> brands = mapper.selectByCondition3(map); for (Brand brand1 : brands) { System.out.println(brand1); } } @Test public void selectByCondition2() { int status = 1; String brand_name = "華"; String company_name = "華"; String brandName = "%" + brand_name + "%"; String companyName = "%" + company_name + "%"; Brand brand = new Brand(); brand.setStatus(1); brand.setBrandName(brandName); brand.setCompanyName(companyName); List<Brand> brands = mapper.selectByCondition2(brand); for (Brand brand1 : brands) { System.out.println(brand1); } } @Test public void selectByCondition1() { int status = 1; String brand_name = "華為"; String company_name = "華"; String brandName = "%" + brand_name + "%"; String companyName = "%" + company_name + "%"; List<Brand> brands = mapper.selectByCondition1( status, companyName, brandName); for (Brand brand : brands) { System.out.println(brand); } } @Test public void findAll() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); DeptMapper mapper = sqlSession.getMapper(DeptMapper.class); List<Dept> all = mapper.findAll(); for (Dept dept : all) { System.out.println(dept); } } }
到此這篇關(guān)于MyBatis中多條件查詢商品的三種方法及區(qū)別的文章就介紹到這了,更多相關(guān)MyBatis 多條件查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java非阻塞I/O模型之NIO相關(guān)知識總結(jié)
在了解NIO (Non-Block I/O) 非阻塞I/O模型之前,我們可以先了解一下原始的BIO(Block I/O) 阻塞I/O模型,NIO模型能夠以非阻塞的方式更好的利用服務(wù)器資源,需要的朋友可以參考下2021-05-05elasticsearch數(shù)據(jù)信息索引操作action?support示例分析
這篇文章主要為大家介紹了elasticsearch數(shù)據(jù)信息索引操作action?support示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04Mybatis中${param}與#{param}的區(qū)別說明
這篇文章主要介紹了Mybatis中${param}與#{param}的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06