细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简
SOL映射文件的加载
方法一:起别名
Sql片段
方法二:
实体类属性名和数据库表列名不一致,不能自动封装数据
1)起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样
*可以定义< sql >片段,提升复用性
2)resultMap:定义 < resultMap > 完成不一致的属性名和列名的映射
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/3-14:56
*/
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
<?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.yang.mapper.UserMapper">
<!--statement-->
<select id="selectAll" resultType="com.yang.pojo.User">
select * from tb_user;
</select>
</mapper>
package com.yang;
import com.yang.pojo.User;
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.List;
/**
* @author 缘友一世
* @date 2022/7/3-15:03
* mybatis快速入门
*/
public class MyBatisDemo {
public static void main(String[] args) 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
List<User> users = sqlSession.selectList("test.selectAll");
System.out.println(users);
//4.释放资源
sqlSession.close();
}
}
/**
*条件查询
* *参数接受
* 1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
* 2.对象参数:对象的属性名称要和参数占位符名称一致
* 3.map集合参数:
*
* @param //status
* @param //companyName
* @param //brandName
* @return
*/
//List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName);
//List<Brand> selectByCondition(Brand brand);
List<Brand> selectByCondition(HashMap map);
<resultMap id="brandResultMap" type="brand">
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
<!--
动态条件查询
*if:条件判断
*test:逻辑表达式
*问题:
* 恒等式
* <where> 替换where 关键字
-->
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
/* where 1=1*/
<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>
public static 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);*/
HashMap 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
//如果没有设置,默认是false,这时候我们需要手动设置事务提交了。
//如果我们设置了true那么就是自动提交了
SqlSession sqlSession = sqlSessionFactory.openSession(false);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
//List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
List<Brand> brands = brandMapper.selectByCondition(map);
//List<Brand> brands = brandMapper.selectByCondition(map);
System.out.println(brands);
//5.释放资源
sqlSession.close();
}
/**
* 单条件动态查询
* @param brand
* @return
*/
List<Brand> selectByConditionSingle(Brand brand);
<resultMap id="brandResultMap" type="brand">
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
where
<choose> <!--类似switch-->
<when test="status!=null"> <!--类似于case-->
status=#{status}
</when>
<when test="companyName!=null and company!=''">
company_name like #{companyName}
</when>
<when test="branName!=null and brandName!=''">
brand_name like #{brandName}
</when>
<otherwise> <!--类似与default-->
1=1
</otherwise>
</choose>
</select>
/**
* 添加
* @param brand
*/
void add(Brand brand);
<!--修改所有字段-->
<update id="update">
update tb_brand
set brand_name=#{brandName},
company_name=#{companyName},
ordered=#{ordered},
description=#{description},
status=#{status}
where id=#{id};
</update>
public static void testAdd() throws IOException {
//接受参数
int status=1;
String companyName="导波手机";
String brandName="导波手机";
String description="手机中的战斗机";
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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.add(brand);
Integer id=brand.getId();
System.out.println(id);
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
<!--动态修改字段-->
<update id="ActiveUpdate">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name=#{brandName},
</if>
<if test="companyName != null and companyName !=''">
companyName=#{companyName}
</if>
<if test="ordered != null">
ordered=#{ordered},
</if>
<if test="description != null and description != ''">
description=#{description},
</if>
<if test="status != null">
status=#{status}
</if>
</set>
where id = #{id};
</update>
public static void testActiveUpdate() throws IOException {
//接受参数
int status=0;
String companyName="先科手机";
String brandName="先科手机";
String description="导波手机,手机中的战斗机";
int ordered=150;
int id=768;
//封装对象
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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
int count=brandMapper.ActiveUpdate(brand);
if(count>0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
<!--
批量删除
mybatis会将数组参数,封装为一个Map对象
* 默认:Array 数组
* 使用@Param注解改变map集合的默认Key的名称
* separator="," 分隔符
* open="(" close=")" 拼接字符
-->
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
public static void testDeleteByIds() throws IOException {
//接受参数
int[] ids={3,4};
//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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteByIds(ids);
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
}
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/2-15:09
* 品牌
* 快捷键 alt+鼠标左键——整列编辑
* Ctrl+r——快速替换
*/
public class Brand {
// id主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
//在实体类中,基本数据类型建议使用对应的包装类型 默认值是NULL
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 +
'}';
}
}
package com.yang.mapper;
import com.yang.pojo.Brand;
import com.yang.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.HashMap;
import java.util.List;
/**
* @author 缘友一世
* @date 2022/7/3-19:53
*/
public interface BrandMapper {
/**
* 查询所有
*/
List<Brand> selectAll();
/**
* 查询详情
*/
Brand selectById(int id);
/**
*条件查询
* *参数接受
* 1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
* 2.对象参数:对象的属性名称要和参数占位符名称一致
* 3.map集合参数:
*
* @param //status
* @param //companyName
* @param //brandName
* @return
*/
//List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName);
//List<Brand> selectByCondition(Brand brand);
List<Brand> selectByCondition(HashMap map);
/**
* 单条件动态查询
* @param brand
* @return
*/
List<Brand> selectByConditionSingle(Brand brand);
/**
* 添加
* @param brand
*/
void add(Brand brand);
/**
* 更新所有字段
*/
int update(Brand brand);
/**
* 动态更新字段
*/
int ActiveUpdate(Brand brand);
/**
* 删除
*/
void deleteById(int id);
/**
* 批量删除
*/
void deleteByIds(@Param("ids") int[] ids);
}
package com.yang.test;
import com.yang.mapper.BrandMapper;
import com.yang.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;
import java.util.Scanner;
/**
* @author 缘友一世
* @date 2022/7/3-22:05
*/
public class MyBatisTest {
public static void main(String[] args) throws IOException {
System.out.println("1……查询所有");
System.out.println("2……条件查询");
System.out.println("3……动态条件查询");
System.out.println("4……添加记录");
System.out.println("5……更新所有记录");
System.out.println("6……动态更新记录");
System.out.println("7……删除记录");
System.out.println("8……批量删除");
System.out.println("请输入您的选择:");
Scanner scanner = new Scanner(System.in);
int select=scanner.nextInt();
scanner.close();
switch (select) {
case 1:
testSelectAll();
break;
case 2:
testSelectById();
break;
case 3:
testSelectByCondition();
break;
case 4:
testAdd();
break;
case 5:
testUpdate();
break;
case 6:
testActiveUpdate();
break;
case 7:
testDeleteById();
break;
case 8:
testDeleteByIds();
break;
default:
System.out.println("请重新输入!");
break;
}
}
public static 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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
//5.释放资源
sqlSession.close();
}
public static 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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
Brand brand = brandMapper.selectById(id);
System.out.println(brand);
//5.释放资源
sqlSession.close();
}
public static 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);*/
HashMap 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
//如果没有设置,默认是false,这时候我们需要手动设置事务提交了。
//如果我们设置了true那么就是自动提交了
SqlSession sqlSession = sqlSessionFactory.openSession(false);
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
//List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
List<Brand> brands = brandMapper.selectByCondition(map);
//List<Brand> brands = brandMapper.selectByCondition(map);
System.out.println(brands);
//5.释放资源
sqlSession.close();
}
public static void testAdd() throws IOException {
//接受参数
int status=1;
String companyName="导波手机";
String brandName="导波手机";
String description="手机中的战斗机";
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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.add(brand);
Integer id=brand.getId();
System.out.println(id);
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
public static void testUpdate() throws IOException {
//接受参数
int status=1;
String companyName="导波手机";
String brandName="导波手机";
String description="导波手机,手机中的战斗机";
int ordered=200;
int id=768;
//封装对象
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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
int count=brandMapper.update(brand);
if(count>0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
public static void testActiveUpdate() throws IOException {
//接受参数
int status=0;
String companyName="先科手机";
String brandName="先科手机";
String description="导波手机,手机中的战斗机";
int ordered=150;
int id=768;
//封装对象
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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
int count=brandMapper.ActiveUpdate(brand);
if(count>0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
public static void testDeleteById() throws IOException {
//接受参数
int id=768;
//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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteById(id);
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
public static void testDeleteByIds() throws IOException {
//接受参数
int[] ids={3,4};
//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.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteByIds(ids);
//5.注意:需要手动提交事务,否则自动回滚
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
}
<?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.yang.mapper.BrandMapper">
<!--
数据库的字段名和实体类的属性名 不一致,则不能自动封装数据
*起别名:对不一样的列名起别名,让别名和实体类的属性名一样
*缺点:每次查询都要定义一次别名
*sql片段
*缺点:不灵活
*resultMap:
1.定义<resultMap>标签
//id:唯一标识
//Type:映射的类型,支持别名
<resultMap id="" type="">
/*
id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
*/
<id column="" property=""></id>
<result column="" property=""></result>
</resultMap>
2.在<select>标签中,使用resultMap属性替换resultType属性
-->
<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片段–>
<sql id="brand_column">id, brand_name brandName, company_name companyName, ordered, description, status</sql>
<select id="selectAll" resultType="Brand">
select
<include refid="brand_column"></include>
from tb_brand;
</select>
-->
<!--<select id="selectAll" resultType="Brand">
select *
from tb_brand;
</select>-->
<!--
*参数占位符
1.#{}:会将其替换为?,为了防止SQL注入
2.${}:拼SQL,会存在SQL注入问题
3.使用时机:
*参数传递的时候使用:#{}
*表名或者列名不固定的情况下${},会存在SQL注入的问题
*特殊字符处理
1.转义字符: <等价于<
2.CDATA区
-->
<select id="selectById" resultMap="brandResultMap">
select *
from tb_brand where id
<![CDATA[
<=
]]>
#{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> 替换where 关键字
-->
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
/* where 1=1*/
<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>
<!--单条件动态查询-->
<!--一级形态-->
<!--二级形态-->
<!--也可以使用<where>标签-->
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
where
<choose> <!--类似switch-->
<when test="status!=null"> <!--类似于case-->
status=#{status}
</when>
<when test="companyName!=null and company!=''">
company_name like #{companyName}
</when>
<when test="branName!=null and brandName!=''">
brand_name like #{brandName}
</when>
<otherwise> <!--类似与default-->
1=1
</otherwise>
</choose>
</select>
<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>
<!--动态修改字段-->
<update id="ActiveUpdate">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name=#{brandName},
</if>
<if test="companyName != null and companyName !=''">
companyName=#{companyName}
</if>
<if test="ordered != null">
ordered=#{ordered},
</if>
<if test="description != null and description != ''">
description=#{description},
</if>
<if test="status != null">
status=#{status}
</if>
</set>
where id = #{id};
</update>
<!--删除-->
<delete id="deleteById">
delete from tb_brand
where id=#{id};
</delete>
<!--
批量删除
mybatis会将数组参数,封装为一个Map对象
* 默认:Array 数组
* 使用@Param注解改变map集合的默认Key的名称
* separator="," 分隔符
* open="(" close=")" 拼接字符
-->
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yang</groupId>
<artifactId>mybits-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--添加slf4j日志api-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!--添加logback-class依赖-->
<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>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--CONSOLE :表示当前的日志信息是可以输出到控制台的。-->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>【%level】 %blue(%d{HH:mm:ss.SSS}) %cyan(【%thread】) %boldGreen(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.yang" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
</logger>
<root level="DEBUG">
<appender-ref ref="Console"/>
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.yang.pojo"/>
</typeAliases>
<environments default="development">
<!--environment:配置数据库连接环境,可以配置多个environment,通过default属性切换不同的environment-->
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="??????????"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载sql映射文件-->
<!--Mapper代理方式-->
<package name="com.yang.mapper"/>
</mappers>
</configuration>
@Select("select*from tb_user whereid=#(id)")
publicUser selectByld(intid):
查询:@Select
添加:@lnsert
修改:@Update
删除:@Delete