海外网站加速器免费,企业综合信息服务平台,软件开发就业前景走向,服装网站建设准备文章目录 1. aim2.环境准备3.查询3.1 查所有3.2 查看详情3.3 条件查询3.3.1 Mybatics如何接收参数#xff1f;3.3.2 多条件查询3.3.3 动态条件查询3.3.4 单条件查询 4.添加主键返回 5.修改5.1 修改全部字段5.2 修改动态字段 6.删除6.1 删除1个6.2 批量删除 JDBC完成#xff1… 文章目录 1. aim2.环境准备3.查询3.1 查所有3.2 查看详情3.3 条件查询3.3.1 Mybatics如何接收参数3.3.2 多条件查询3.3.3 动态条件查询3.3.4 单条件查询 4.添加主键返回 5.修改5.1 修改全部字段5.2 修改动态字段 6.删除6.1 删除1个6.2 批量删除 JDBC完成
https://blog.csdn.net/meini32/article/details/131981238 1. aim 要完成的功能列表清单: 1. 查询 查询所有数据查看详情条件查询 2. 添加 3. 修改 修改全部字段修改动态字段 4. 删除 删除一个批量删除 2.环境准备 数据库表tb_brand实体类Brand测试用例安装MyBatis×插件 数据库表
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(-- id 主键id int primary key auto_increment,-- 品牌名称brand_name varchar(20),-- 企业名称company_name varchar(20),-- 排序字段ordered int,-- 描述信息description varchar(100),-- 状态0禁用 1启用status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (三只松鼠, 三只松鼠股份有限公司, 5, 好吃不上火, 0),(华为, 华为技术有限公司, 100, 华为致力于把数字世界带入每个人、每个家庭、每个组织构建万物互联的智能世界, 1),(小米, 小米科技有限公司, 50, are you ok, 1);SELECT * FROM tb_brand;实体Brand类
package com.itheima.pojo;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;}Overridepublic String toString() {return Brand{ id id , brandName brandName \ , companyName companyName \ , ordered ordered , description description \ , status status };}
}
测试用例 MybatisX插件 功能 XML和接口方法互相跳转根据接口方法生成statement 3.查询 步骤 编写接口Mapper接口编写sql语句sql映射文件执行方法测试 3.1 查所有 注意数据库表的字段名称和―实体类的属性名称不一样则不能自动封装数据
方法1起别名 方法2映射 1定义标签 2在标签中使用resultMap属性替换resultType属性 3.2 查看详情 查看某一条数据的详情信息 1.编写mapper接口
2.定义接受参数
3. 编写sql语句
select idselectById resultMapbrandResultMapselect * from tb_brand where id #{id};/select4.执行sql 参数占位符 1#{}:会将其替换为 ?为了防止SQL注入 2${}:拼sql。会存在SQL注入问题 3.3 条件查询
3.3.1 Mybatics如何接收参数
1.单个参数 可以直接将参数作为方法的参数进行传递。在 SQL 语句中可以通过 #{paramName} 的形式引用该参数。 // Java 代码
public interface UserMapper {User getUserById(int id);
}!-- Mapper XML --
select idgetUserById resultTypeUserSELECT * FROM user WHERE id #{id}
/select2.使用 Param 注解来指定参数的名称 可以使用 Param 注解来指定参数的名称然后在 SQL 语句中通过该名称引用参数。 // Java 代码
public interface UserMapper {User getUserByIdAndName(Param(id) int id, Param(name) String name);
}!-- Mapper XML --
select idgetUserByIdAndName resultTypeUserSELECT * FROM user WHERE id #{id} AND name #{name}
/select3.使用 Map 传递参数 可以使用 Map 类型的参数传递多个参数。在 SQL 语句中通过键来引用对应的值 // Java 代码
public interface UserMapper {User getUserByMap(MapString, Object params);
}!-- Mapper XML --
select idgetUserByMap resultTypeUserSELECT * FROM user WHERE id #{id} AND name #{name}
/select4.使用对象传递参数 可以使用自定义的对象作为参数MyBatis 会自动将对象的属性与 SQL 语句中的参数进行映射。例如 // Java 代码
public class UserQuery {private int id;private String name;// 省略 getter 和 setter 方法
}public interface UserMapper {User getUserByQuery(UserQuery query);
}!-- Mapper XML --
select idgetUserByQuery resultTypeUserSELECT * FROM user WHERE id #{id} AND name #{name}
/select3.3.2 多条件查询 步骤 编写多条件查询的接口mapper添加到映射文件里编写sql语句执行测试 1.定义接口
public interface BrandMapper {ListBrand selectAll();Brand selectById(int id); //通过id查看商品详情//使用 Param 注解来指定参数的名称ListBrand selectByMutiCondition(Param(status)int status,Param(companyName)String companyName,Param(brandName)String brandName);//使用对象来指定参数ListBrand selectByMutiCondition(Brand brand); //使用map来指定参数ListBrand selectByMutiCondition(HashMap map);}2.添加到映射文件里
!--
namespace:名称空间
--mapper namespacecom.itheima.mapper.BrandMapperresultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMap!-- 多条件查询--select idselectByMutiCondition resultMapbrandResultMapSELECT * FROM tb_brandWHERE status #{status} AND company_name LIKE #{companyName} AND brand_name LIKE #{brandName};/select/mapper3.执行测试
//多条件查询-使用参数查询
public class MyBatisTest3 {public static void main(String[] args) throws IOException {int s 0;String cn 三只松鼠股份有限公司;String bn 三只松鼠;//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);ListBrand brand brandMapper.selectByMutiCondition(s,cn,bn);System.out.println(brand);//4.释放资源sqlSession.close();}
} //多条件查询-使用对象
public class MyBatisTest3 {public static void main(String[] args) throws IOException {int s 0;String cn 三只松鼠股份有限公司;String bn 三只松鼠;Brand brand1 new Brand();brand1.setStatus(s);brand1.setBrandName(bn);brand1.setCompanyName(cn);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);ListBrand brand brandMapper.selectByMutiCondition(brand1);System.out.println(brand);//4.释放资源sqlSession.close();}
} //多条件查询-使用Map
public class MyBatisTest3 {public static void main(String[] args) throws IOException {int s 0;String cn 三只松鼠股份有限公司;String bn 三只松鼠;HashMap map new HashMap();map.put(status,s);map.put(companyName,cn);map.put(brandName,bn);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);ListBrand brand brandMapper.selectByMutiCondition(map);System.out.println(brand);//4.释放资源sqlSession.close();}
}
结果
3.3.3 动态条件查询 动态条件查询是指根据不同的条件组合构建动态的 SQL 查询语句。 方法 使用 if 元素使用 choose, when, otherwise 元素使用 trim, where, set元素 使用if
select idgetUserByCondition resultTypeUserSELECT * FROM userWHERE 11if testid ! nullAND id #{id}/ifif testname ! nullAND name #{name}/if
/select使用 choose, when, otherwise 元素
select idgetUserByCondition resultTypeUserSELECT * FROM userWHERE 11choosewhen testid ! nullAND id #{id}/whenwhen testname ! nullAND name #{name}/whenotherwiseAND status ACTIVE/otherwise/choose
/select使用 trim, where, set元素
update idupdateUser parameterTypeUserUPDATE usersetif testname ! nullname #{name},/ifif testage ! nullage #{age},/if/setWHERE id #{id}
/update1.编写接口
ListBrand selectByMutiConditionActivate(Brand brand);2.编写动态查询映射文件
!-- 动态查询--select idselectByMutiConditionActivate resultMapbrandResultMapSELECT * FROM tb_brandWHERE 11if teststatus ! nullAND status #{status}/ifif testcompanyName ! null and companyName! AND company_name LIKE #{companyName} /ifif testbrandName ! null and brandName! AND brand_name LIKE #{brandName} /if/select3.测试
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 java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;public class MyBatisTest4 {public static void main(String[] args) throws IOException {
// int s 1;String cn 三只松鼠股份有限公司;
// String bn 三只松鼠;Brand brand1 new Brand();
// brand1.setStatus(s);
// brand1.setBrandName(bn);brand1.setCompanyName(cn);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);ListBrand brand brandMapper.selectByMutiConditionActivate(brand1);System.out.println(brand);//4.释放资源sqlSession.close();}
}
4.结果
3.3.4 单条件查询 MyBatis 单条件动态查询是指根据单个条件的存在与否动态地构建 SQL 查询语句。根据不同的条件值选择是否添加该条件到查询语句中从而实现根据单个条件进行灵活查询的功能。 使用 choose《when 和 《otherwise 元素可以实现条件选择逻辑根据不同的条件选择其中一个分支进行处理 select idgetUserByCondition resultTypeUserSELECT * FROM userWHERE 11choosewhen testname ! nullAND name #{name}/whenwhen testage ! nullAND age #{age}/whenotherwiseAND 11/otherwise/choose
/select4.添加 步骤 编写接口方法编写sql语句添加映射文件执行方法测试 MyBatis事务: openSession: 默认开启事务进行增删改操作后需要使用 sqlSession.commit;手动提交事务openSession(true): 可以设置为自动提交事务 (关闭事务) 1.编写接口
public interface BrandMapper {//添加void addBrand(Brand brand);
}
2.编辑映射文件
!--
namespace:名称空间
--mapper namespacecom.itheima.mapper.BrandMapperresultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMapinsert idaddBrandinsert into tb_brand (brand_name, company_name, ordered,description, status)values (#{brandName},#{companyName},#{ordered},#{description},#{status});/insert/mapper3.测试 结果 //添加
public class MybatisTest5 {public static void main(String[] args) throws IOException {String bn 百度;String cn 百度公司;int od 18;String ds 百度一下你就知道;int st 0;Brand brand new Brand();brand.setBrandName(bn);brand.setStatus(st);brand.setCompanyName(cn);brand.setDescription(ds);brand.setOrdered(od);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);brandMapper.addBrand(brand);//4.提交事务sqlSession.commit();//5.关闭sqlSession.close();}
} 主键返回 是什么 在数据添加成功后需要获取插入数据库数据的主键的值 比如: 添加订单和订单项 1.添加订单 2.添加订单项订单项中需要设置所属订单的id 原因执行完id没有绑定到对象上 解决MyBatis 框架可以使用 useGeneratedKeys 和 keyProperty 属性来自动获取生成的主键值并将其设置到相应的属性上。 insert idaddBrand useGeneratedKeystrue keyPropertyidinsert into tb_brand (brand_name, company_name, ordered,description, status)values (#{brandName},#{companyName},#{ordered},#{description},#{status});/insert5.修改
sql语句
UPDATE 表名 SET 列名1值1,列名2值2,... [ WHERE 条件];mybatis标签
update idupdateOrder parameterTypeOrderUPDATE ordersSET order_number #{orderNumber}, order_date #{orderDate}WHERE id #{id}
/update5.1 修改全部字段
public interface BrandMapper {//修改void updateBrand(Brand brand);
}
update idupdateBrandupdate tb_brandset brand_name#{brandName},company_name#{companyName},ordered#{ordered},description#{description},status#{status}where id #{id};/update//返回修改全部字段
public class MybatisTest7 {public static void main(String[] args) throws IOException {String bn 快手;String cn fasthand公司;int od 666;String ds 老铁加油哇;int st 1;int id 7;Brand brand new Brand();brand.setId(id);brand.setBrandName(bn);brand.setStatus(st);brand.setCompanyName(cn);brand.setDescription(ds);brand.setOrdered(od);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);brandMapper.updateBrand(brand);//4.提交事务sqlSession.commit();//5.关闭sqlSession.close();}
} 5.2 修改动态字段 在 MyBatis 中你可以使用 元素来实现动态修改字段。 元素根据条件的成立与否决定是否包含某字段的修改语句片段。 void updateBrandActivate(Brand brand);update idupdateBrandActivateupdate tb_brandsetif testbrandName ! null and brandName! brand_name#{brandName}/ifif testcompanyName ! null and companyName! company_name#{companyName}/ifif testordered ! null and ordered! ordered#{ordered}/ifif testdescription ! null and description! description#{description}/ifif teststatus ! null and status! status#{status}/if/setwhere id #{id};/update//返回动态修改字段
public class MybatisTest8 {public static void main(String[] args) throws IOException {String bn 快手;String cn fasthand公司;int od 666;String ds 先穿裤子在穿鞋先当孙子再当爷记住这句话;int st 0;int id 7;Brand brand new Brand();brand.setId(id);
// brand.setBrandName(bn);brand.setStatus(st);
// brand.setCompanyName(cn);brand.setDescription(ds);
// brand.setOrdered(od);//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);brandMapper.updateBrandActivate(brand);//4.提交事务sqlSession.commit();//5.关闭sqlSession.close();}
} 6.删除
sql语句
DELETE FROM 表名 [WHERE 条件] ;6.1 删除1个
void deleteOne(int id);delete iddeleteOnedelete from tb_brand where id #{id};/delete//删除1public class MybatisTest9 {public static void main(String[] args) throws IOException {int id 6;//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);brandMapper.deleteOne(id);//4.提交事务sqlSession.commit();//5.关闭sqlSession.close();}
}
6.2 批量删除
void deleteMuti(Param(ids)int[] ids);delete iddeleteMutidelete from tb_brandwhere idin(foreach collectionids itemid separator,#{id}/foreach);/delete//删除1public class MybatisTest9 {public static void main(String[] args) throws IOException {int[] ids {1,5,7};//1.加载核心文件获取SqlSessionFactoryInputStream inputStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.获取对应的SqlSession对象,用来执行ssqlSqlSession sqlSession sqlSessionFactory.openSession();//3.执行sqlBrandMapper brandMapper sqlSession.getMapper(BrandMapper.class);brandMapper.deleteMuti(ids);//4.提交事务sqlSession.commit();//5.关闭sqlSession.close();}
} 文章转载自: http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.leeong.com.gov.cn.leeong.com