该注解的作用是在执行@Test方法前调用. 是测试方法提供的测试API.
public class TestMybatis2 {
//定义公共的属性
private SqlSessionFactory sqlSessionFactory;
/**
* mybatis的核心 SqlSessionFacotry对象
* @BeforeEach: 测试API中的注解 在执行@Test注解方法时,会提前执行!!!
*/
@BeforeEach
public void init() throws IOException {
//1.指定资源文件
String resource = "mybatis/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testMybatis01(){
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
List<DemoUser> list = demoUserMapper.findAll();
System.out.println(list);
sqlSession.close();
}
}
/**
* 作业:
* 1. 查询name="王昭君"的用户
*/
@Test
public void testFindByName(){
//保证每个线程都能获取一个链接
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
String name = "王昭君";
//如果不能保证结果唯一,则使用List集合接收数据.
List<DemoUser> list = demoUserMapper.findByName(name);
System.out.println(list);
sqlSession.close();
}
<select id="findByName" resultType="com.jt.pojo.DemoUser">
select * from demo_user where name = #{name}
select>
/**
* 需求 :2. 查询sex=女 and age > 18岁
* 条件 2个
*/
@Test
public void testFindBySA(){
//保证每个线程都能获取一个链接
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
//编程习惯: 面向对象
DemoUser user = new DemoUser();
user.setAge(18).setSex("女");
List<DemoUser> list = demoUserMapper.findBySA(user);
System.out.println(list);
sqlSession.close();
}
<select id="findBySA" resultType="com.jt.pojo.DemoUser">
select * from demo_user where sex= #{sex} and age > #{age}
select>
规则: mybatis如果遇到多值传参时,默认条件是采用下标的方式获取数据.
mybatis天生只支持单值传参,如果遇到多值的问题,则应该将多值封装为单值.
1. 封装为实体对象 user对象
2. 更为常用的方式 Map集合
3. 如果传递的数据有多个,则可以使用注解@Param("sex") String sex 封装为Map.
说明: 代码中的方式 将3种常见情景进行了整理,理解参数封装的原理.
/**
* 需求 :2. 查询sex=女 and age > 18岁
* 方式1: User对象封装
*/
@Test
public void testFindBySA(){
//保证每个线程都能获取一个链接
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
//编程习惯: 面向对象
DemoUser user = new DemoUser();
user.setAge(18).setSex(“女”);
List list = demoUserMapper.findBySA(user);
System.out.println(list);
sqlSession.close();
}
/**
* sex=女 and age > 18
* 方式2: @Param方式封装.
*/
@Test
public void testFindBySA2(){
//保证每个线程都能获取一个链接
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
String sex = "女";
int age = 18;
List<DemoUser> list = demoUserMapper.findBySA2(sex,age);
System.out.println(list);
sqlSession.close();
}
/**
* sex=女 and age > 18
* 方式3: map集合封装
*/
@Test
public void testFindBySA3(){
//保证每个线程都能获取一个链接
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("sex","女");
map.put("age",18);
List<DemoUser> list = demoUserMapper.findBySA3(map);
System.out.println(list);
sqlSession.close();
}
<select id="findBySA" resultType="com.jt.pojo.DemoUser">
select * from demo_user where sex= #{sex} and age > #{age}
select>
<select id="findBySA2" resultType="com.jt.pojo.DemoUser">
select * from demo_user where sex= #{sex} and age > #{age}
select>
<select id="findBySA3" resultType="com.jt.pojo.DemoUser">
select * from demo_user where sex= #{sex} and age > #{age}
select>
小结: 一般条件下能用#{}号,不用${}
/**
* 需求: 实现用户入库操作
* 关于事务说明:
* mybatis中的"更新"操作,默认事务都是开启的,如果进行更新操作,
* 则必须提交事务.
*/
@Test
public void testSaveUser(){
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
//数据库主键自增,所以对象的ID可以为null.
DemoUser user = new DemoUser(null,"佛媛",99,"女");
int rows = demoUserMapper.saveUser(user);
if(rows > 0){
System.out.println("影响的行数:"+rows);
//事务提交
sqlSession.commit();
}
sqlSession.close();
}
<insert id="saveUser">
insert into demo_user value (null,#{name},#{age},#{sex})
insert>
1.把id=1 的数据 name改为"守山大使" age=5000
2.将name="佛媛"的数据删除.
xml文件中的转义字符.
> > 大于
< < 小于
& & 号
说明:如果sql中有大量的转义字符 建议使用转义标签体
语法:
查询age> 18 and age< 100 的用户信息.
/**
* 需求: 查询age> 18 and age< 100 的用户信息.
* 规则: 如果不能使用对象封装,则一般使用Map集合
*/
@Test
public void testSelect01(){
SqlSession sqlSession = sqlSessionFactory.openSession();
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("minAge",18);
map.put("maxAge",100);
List<DemoUser> userList = demoUserMapper.findByAge(map);
System.out.println(userList);
sqlSession.close();
}
<select id="findByAge" resultType="com.jt.pojo.DemoUser">
#{minAge} and age < #{maxAge}]]>
select>
要求批量的删除数据库中的记录.
例如: 删除id=232/233/234的数据?
/**
* 例如: 删除id=232/233/234的数据?
* Sql: delete from demo_user where id in (232,233,234)
* 规则: 如果遇到相同的多个数据,则一般采用集合的方式封装数据.
* 封装方式:
* 1. array
* 2. list
* 3. map
*/
@Test
public void testDeleteIds(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
//将数据封装为数组
int[] ids = {232,233,234};
demoUserMapper.deleteIds(ids);
System.out.println("删除操作成功!!!");
}
<delete id="deleteIds">
delete from demo_user where id in (
<foreach collection="array" item="id" separator=",">
#{id}
foreach>
)
delete>
根据上述操作,练习mybatis集合的用法. 下列代码主要练习list/map的用法
@Test
public void testDeleteList(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
List list = new ArrayList();
list.add(232);
list.add(233);
list.add(234);
demoUserMapper.deleteList(list);
System.out.println("删除操作成功!!!");
}
/*
* 说明: 有时业务需求导致需要使用map封装list集合
*/
@Test
public void testDeleteMap(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
List list = new ArrayList();
list.add(232);
list.add(233);
list.add(234);
HashMap map = new HashMap();
map.put("ids",list);
demoUserMapper.deleteMap(map);
System.out.println("删除操作成功!!!");
}
<delete id="deleteList">
delete from demo_user where id in (
<foreach collection="list" item="id" separator=",">
#{id}
foreach>
)
delete>
<delete id="deleteMap">
delete from demo_user where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
foreach>
)
delete>
作业
/**
* 作业:
* 1.整理bug集,收集Mybatis中常见报错,并且持续维护 20%
* 2.mybatis案例练习
* 2.1 查询age<100岁的女性用户.
* 2.2 查询name中包含"精"的数据.并且按照年龄降序排列
* 2.3 查询age位于100-300的数据
* 2.4 将name为小乔/大乔/王昭君的年龄改为18岁,性别女
*
*/