package Test;
import Test.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;
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 users = sqlSession.selectList("test.selectAll");
System.out.println(users);
// 4、释放资源
sqlSession.close();
}
}
package Test;
import Test.mapper.AccountMapper;
import Test.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;
public class MybatisDemo2 {
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 users = sqlSession.selectList("test.selectAll");
// 3、1 获取 UserMapper 接口的代理对象
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
List users = accountMapper.selectAll();
System.out.println(users);
// 4、释放资源
sqlSession.close();
}
}
select *
from tb_brand;
select *
from tb_brand where id = #{id};
package test.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import test.pojo.Brand;
import java.util.List;
import java.util.Map;
public interface BrandMapper {
List selectAll();
@Select(value = "SELECT * FROM tb_brand WHERe id = #{id}")
Brand selectById(@Param("id") int id);
List selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
List selectByCondition(Brand brand);
List selectByCondition(Map map);
List selectByConditionSingle(Brand brand);
void add(Brand brand);
int update(Brand brand);
void deleteById(Integer id);
void deleteByIds(@Param("ids")int[] ids);
}
select *
from tb_brand;
select *
from tb_brand
and status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
select *
from tb_brand
status = #{status}
company_name like #{companyName}
brand_name like #{brandName}
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
update tb_brand
brand_name = #{brandName},
company_name = #{companyName},
ordered = #{ordered},
description = #{description},
status = #{status}
where id = #{id}
delete from tb_brand where id = #{id}
delete from tb_brand where id in
#{id}
package test;
import test.mapper.BrandMapper;
import test.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 mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
List brands = mapper.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 mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
Brand brand = mapper.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.setBrandName(brandName);
// brand.setCompanyName(companyName);
Map map = new HashMap();
map.put("status",status);
map.put("brandName",brandName);
map.put("companyName",companyName);
// 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 mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
// 散装参数查询
// List brands = mapper.selectByCondition(status, companyName, brandName);
// 对象参数查询
// List brands = mapper.selectByCondition(brand);
// map集合查询
List brands = mapper.selectByCondition(map);
System.out.println(brands);
// 5、释放资源
sqlSession.close();
}
@Test
public void testSelectByConditionSingle() throws IOException {
// 就收参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
// 处理参数
companyName = "%" + companyName + "%";
brandName = "%" + brandName + "%";
// 封装对象
Brand brand = new Brand();
// brand.setStatus(status);
// brand.setBrandName(brandName);
// brand.setCompanyName(companyName);
// 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 mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
List brands = mapper.selectByConditionSingle(brand);
System.out.println(brands);
// 5、释放资源
sqlSession.close();
}
@Test
public void testAdd() throws IOException {
// 就收参数
int status = 1;
String companyName = "鸭梨";
String brandName = "鸭梨牌山寨手机";
String description = "美国有苹果,中国有鸭梨";
int ordered = 2;
// 封装对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setDescription(description);
brand.setOrdered(ordered);
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 3、获取Mapper接口的代理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
mapper.add(brand);
// 提交事务
// sqlSession.commit();
List brands = mapper.selectAll();
System.out.println(brands);
// 5、释放资源
sqlSession.close();
}
@Test
public void testAdd2() throws IOException {
// 就收参数
int status = 1;
String companyName = "鸭梨";
String brandName = "鸭梨牌山寨手机";
String description = "美国有苹果,中国有鸭梨";
int ordered = 2;
// 封装对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setDescription(description);
brand.setOrdered(ordered);
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 3、获取Mapper接口的代理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
mapper.add(brand);
Integer id = brand.getId();
System.out.println(id);
// 5、释放资源
sqlSession.close();
}
@Test
public void testUpdate() throws IOException {
// 就收参数
int status = 1;
String companyName = "菠萝";
String brandName = "菠萝牌山寨手机";
String description = "美国有苹果,中国有菠萝";
int ordered = 2;
int id = 8;
// 封装对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
// brand.setDescription(description);
brand.setOrdered(ordered);
brand.setId(id);
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 3、获取Mapper接口的代理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
int count = mapper.update(brand);
System.out.println(count);
// 5、释放资源
sqlSession.close();
}
@Test
public void testDeleteById() throws IOException {
// 就收参数
int id = 8;
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 3、获取Mapper接口的代理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
mapper.deleteById(id);
// 5、释放资源
sqlSession.close();
}
@Test
public void testDeleteByIds() throws IOException {
// 就收参数
int[] ids = {4,9,10};
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 3、获取Mapper接口的代理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
mapper.deleteByIds(ids);
// 5、释放资源
sqlSession.close();
}
}