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
//查看詳情 根據id查詢信息
Brand selectById(int id);
/*條件查詢
參數接受:
1.散裝參數 :如果有多個參數 需要@Param("SQL參數占位符名稱")
2.對象參數
3.map集合參數
*/
List
/* List
List
//添加
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();
}
}

浙公網安備 33010602011771號