Brand类
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 + '}'; } }
BrandMapper文件:一个接口用于执行
package com.itheima.mapper; import com.itheima.pojo.Brand; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface BrandMapper { public List<Brand> selectAll(); /* * 查看详情 * */ Brand selectById(int id); /* * 完成的是条件查询 * *参数接收 * 1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称") * 2.对象参数:对象的属性名称要和参数占位符名称一致 * 3.map集合参数 * */ //List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName,@Param("brandName") String brandName); //List<Brand> selectByCondition(Brand brand); List<Brand> selectByCondition(Map map); }
对应的BrandMapper.xml文件,用于写具体的sql语句
<?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="com.itheima.mapper.BrandMapper"> <resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"></result> <result column="company_name" property="companyName"></result> </resultMap> <select id="selectAll" resultMap="brandResultMap"> select * from tb_brand ; </select> <!-- 数据库的字段名称 和 实体类的属性名称不一样 , 则不能自动封装数据 *起别名:对不一样的列名起别名 让别名和实体类名字相同 *缺点 每次查询都要定义一次别名 *sql片段 *resultMap --> <!--<select id="selectAll" resultType="brand"> 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> --> <!-- *参数占位符: 1.#{}:会将其替换为?,为了防止sql注入 2.${}:拼sql。会存在sql注入问题 3.使用时机: * 参数传递的时候:#{} * 表名或者列名不固定的情况下:${} *参数类型:parameterType 可以省略 *特殊字符的处理: 1.转义字符: 2.CDATA区: --> <select id="selectById" resultMap="brandResultMap"> select * from tb_brand where id = #{id}; </select> <!-- 条件查询 --> <!--<select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName} </select>--> <!-- 动态条件查询 *if:条件判断 * test:逻辑表达式 *问题: *恒等式 加入条件 where 1 = 1 并在每个if逻辑表达式里面加上and *<where>标签 替换where关键字 --> <select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand <where> <if test="status != null"> and status = #{status} </if> <if test="companyName != null and companyName != ''"> and company_name like #{companyName} </if> <if test="brandName != null and brandName != ''"> and brand_name like #{brandName} </if> </where> </select> </mapper>
执行具体的主要java test示例
MybatisTest.java
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.HashMap; import java.util.List; import java.util.Map; 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(); } @Test public void testSelectById() throws IOException { int id = 1; //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.执行方法 Brand brand = brandMapper.selectById(id); System.out.println(brand); //5.释放资源 sqlSession.close(); } @Test public void testSelectByCondition() throws IOException { //接收参数 int status = 1; String companyName = "华为"; String brandName = "华为"; //处理 companyName = "%"+ companyName + "%"; brandName = "%"+ brandName + "%"; /* //封装对象 Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName);*/ Map map = new HashMap(); //map.put("status",status); map.put("companyName",companyName); //map.put("brandName",brandName); //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.selectByCondition(status,companyName,brandName); //List<Brand> brands = brandMapper.selectByCondition(brand); List<Brand> brands = brandMapper.selectByCondition(map); System.out.println(brands); //5.释放资源 sqlSession.close(); } }
标签:status,String,companyName,数据库,brand,brandName,具体,操作,public From: https://www.cnblogs.com/sxwgzx23/p/17321580.html