MyBatis三种开发方式
注意:
注意:
注意:
INSERT INTO user
VALUES (null, #{userName}, #{birthday}, #{sex}, #{address});
@Test
public void test06() throws IOException {
//测试mybatis的搭建情况
//1.标记mybatis的配置环境
String resource = "mybatis-config.xml";
//2.加载配置环境信息
InputStream inputStream = Resources.getResourceAsStream(resource);
//3.根据配置信息生成sqlSessionFactory对象,相当于连接池
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.从连接池获取连接
//4.1可以设置自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//5.获取执行sql语句的对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//6.调用方法:执行sql语句
User user = new User(null, "王母娘娘", Date.valueOf("2022-11-10"), "女", "云宵殿");
usermapper.add(user);
//返回自增的id
System.out.println(user.getId());
//查询id对应的对象信息,做登录操作
// usermapper.selectById(user.getId());
//6.手动提交事务
//sqlSession.commit();
//7.释放资源
sqlSession.close();
}
注意多参数查询默认只能按照两种方式传参:方式1:arg0,arg1;
方式2:param1,param2,param3
自己配置的话比较有意义,有三种方式:
//方案1:散装的方式
/*List selectByCondition(@Param("username")String username,
@Param("sex")String sex);*/
List userList1 = usermapper.selectByCondition("王%", "女");
//方案2:对象参数
/* List selectByCondition(User user);*/
//方案二
/*User user = new User();
user.setUsername("王%");
user.setSex("女");
List userList = usermapper.selectByCondition(user);*/
//方案3:Map键值对
List selectByCondition(Map map);
//方案三
Map map=new HashMap<>();
map.put("username","王%");
map.put("sex","女");
List userList = usermapper.selectByCondition(map);
@Test //运行测试
public void test08() throws IOException {
//测试mybatis的搭建情况
//1.标记mybatis的配置环境
String resource = "mybatis-config.xml";
//2.加载配置环境信息
InputStream inputStream = Resources.getResourceAsStream(resource);
//3.根据配置信息生成sqlSessionFactory对象,相当于连接池
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.从连接池获取连接
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.获取执行sql语句的对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//6.调用方法:执行sql语句
List userList = usermapper.selectByIf("", "");
userList.forEach(System.out::println);
//7.释放资源
sqlSession.close();
}
set标签修改数据
update user
username=#{username},
birthday=#{birthday},
sex=#{sex},
address=#{address}
where id = #{id};
@Test
public void test09() throws IOException {
//测试mybatis的搭建情况
//1.标记mybatis的配置环境
String resource = "mybatis-config.xml";
//2.加载配置环境信息
InputStream inputStream = Resources.getResourceAsStream(resource);
//3.根据配置信息生成sqlSessionFactory对象,相当于连接池
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.从连接池获取连接
//4.1可以设置自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//5.获取执行sql语句的对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//6.调用方法:执行sql语句
User user = new User();
user.setId(10);
user.setUsername("王大姐");
usermapper.updateByIf(user);
//6.手动提交事务
//sqlSession.commit();
//7.释放资源
sqlSession.close();
}
delete from user where id in
#{id}
@Test
public void test10() throws IOException {
//测试mybatis的搭建情况
//1.标记mybatis的配置环境
String resource = "mybatis-config.xml";
//2.加载配置环境信息
InputStream inputStream = Resources.getResourceAsStream(resource);
//3.根据配置信息生成sqlSessionFactory对象,相当于连接池
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.从连接池获取连接
//4.1可以设置自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//5.获取执行sql语句的对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//6.调用方法:执行sql语句
usermapper.deleteByArray(new int[]{12,13,14,15,16});
//6.手动提交事务
//sqlSession.commit();
System.out.println("删除成功");
//7.释放资源
sqlSession.close();
}
id,username,birthday,sex,address
@Test
public void test12() throws IOException {
//测试mybatis的搭建情况
//1.标记mybatis的配置环境
String resource = "mybatis-config.xml";
//2.加载配置环境信息
InputStream inputStream = Resources.getResourceAsStream(resource);
//3.根据配置信息生成sqlSessionFactory对象,相当于连接池
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.从连接池获取连接
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.获取执行sql语句的对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//6.调用方法:执行sql语句
List userList = usermapper.selectBySQL();
userList.forEach(System.out::println);
//7.释放资源
sqlSession.close();
}