1.14

回顾javaweb敲代码
package com.it.mapper;

import com.it.pojo.Brand;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface BrandMapper {

//查询所有
List selectAll ();

//查看详情 根据id查询信息
Brand selectById(int id);

/*条件查询
参数接受:
1.散装参数 :如果有多个参数 需要@Param("SQL参数占位符名称")
2.对象参数
3.map集合参数
*/
List selectByConditon(@Param("status") int status,@Param("companyName")String companyName,@Param("brandName") String brandName);

/* List selectByCondition(Brand brand);

List selectByCondition(Map map);*/

//添加
void add(Brand brand);

//修改全部字段
void update(Brand brand);

//删除一条数据
void deleteById(int id);
}

package com.it.pojo;

public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
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 +
            '}';
}

}

<!--当数据库表的字段名称 与 实体类的属性名称不一样 则不能自动封装数据

起别名 as
特殊字符处理 sql语句 <号 不可用 需要CDATA区
edg:
-->

<select id="selectAll" resultType="brand">
    select id, brand_name as brandName, company_name as companyName, ordered, description, status
    from tb_brand ;
</select>

<select id="selectById" resultType="Brand">
    select *
    from tb_brand where id = #{id};
</select>

<!--条件查询-->
<select id="selectByConditon" resultType="brand">
    select id, brand_name as brandName, company_name as companyName, ordered, description, status
    from tb_brand
    where status = #{status}
    and company_name like #{companyName}
    and brand_name like #{brandName};
</select>

<!--添加-->
<!--当id为主键时 需要 useGeneratedKeys="true" keyProperty="id"-->
<insert id="add" useGeneratedKeys="true" keyProperty="id">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName},#{companyName},#{ordered},#{description},#{status});
</insert>

<!--修改-->
<update id="update">
    update tb_brand
    set brand_name = #{brandName},
        company_name = #{companyName},
        ordered = #{ordered},
        description = #{description},
        status = #{status}
        where id = #{id} ;
</update>

<!--删除一条数据 -->
<delete id="deleteById">
    delete
    from tb_brand
    where id = #{id};
</delete>

package com.it.test;

import com.it.mapper.BrandMapper;
import com.it.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 BrandMapperTest {

//查询所有
@Test
public  void testSelectAll() throws IOException {
    //1.加载mybatis核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //执行方法
    List<Brand> brands = brandMapper.selectAll();
    System.out.println(brands);

    //4.释放资源
    sqlSession.close();
}


//查看详情 根据id查询信息
@Test
public  void testSelectById() throws IOException {
    //接受参数
    int id = 1;

    //1.加载mybatis核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //执行方法
    Brand brand = brandMapper.selectById(id);
    System.out.println(brand);

    //4.释放资源
    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 map = new HashMap();
    map.put("status",status);
    map.put("companyName",companyName);
    map.put("brandName",brandName)

*/
//1.加载mybatis核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
     //执行方法
    List<Brand> brands = brandMapper.selectByConditon(status, companyName, brandName);
    //List<Brand> brands = brandMapper.selectByConditon(brand);
   //List<Brand> brands = brandMapper.selectByConditon(map);

    System.out.println(brands);

    //4.释放资源
    sqlSession.close();
}

//添加
@Test
public  void testAdd() throws IOException {
    //接受参数
    int status = 1;
    String companyName = "111";
    String brandName = "11";
    String description ="aaa";
    int ordered = 100;

 //封装对象
   Brand brand = new Brand();
    brand.setStatus(status);
    brand.setCompanyName(companyName);
    brand.setBrandName(brandName);
    brand.setDescription(description);
    brand.setOrdered(ordered);

    //1.加载mybatis核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //执行方法
    brandMapper.add(brand);
    int id = brand.getId();
    System.out.println(id);
    //提交事务
    sqlSession.commit();

    //4.释放资源
    sqlSession.close();
}

//修改
@Test
public  void testUpdate() throws IOException {
    //接受参数
    int status = 1;
    String companyName = "111";
    String brandName = "11";
    String description ="aaa";
    int ordered = 100;
    int id = 1;
    //封装对象
    Brand brand = new Brand();
    brand.setStatus(status);
    brand.setCompanyName(companyName);
    brand.setBrandName(brandName);
    brand.setDescription(description);
    brand.setOrdered(ordered);
    brand.setId(id);

    //1.加载mybatis核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //执行方法
    brandMapper.update(brand);
    //提交事务
    sqlSession.commit();

    //4.释放资源
    sqlSession.close();
}

//删除
@Test
public  void testDeleteById() throws IOException {
    //接受参数
    int id = 6;

    //1.加载mybatis核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2.获取SqlSession对象,用他来执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3.执行sql语句
    //获取BrandMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //执行方法
    brandMapper.deleteById(id);
    //提交事务
    sqlSession.commit();

    //4.释放资源
    sqlSession.close();
}

}

posted @ 2025-01-14 17:43  霸王鸡  阅读(36)  评论(0)    收藏  举报