mybatis入门(八)
2024-09-07 18:59:14
mybatis入门---更新和删除
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete> <!-- 更新用户
必须指定id,否则将会将表的内容全部更新
-->
<update id="updateUser" parameterType="ql.mybatis.pojo.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update>
测试类:
// 删除用户
@Test
public void deleteUserTest() throws IOException {
// 通过工厂得到SqlSession
SqlSession sqlSession = null;
try {
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("test.deleteUser", 1);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
// 释放资源
sqlSession.close();
} }
} //更新用户
@Test
public void updateUserTest() throws IOException {
// 通过工厂得到SqlSession
SqlSession sqlSession = null;
try {
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
sqlSession = sqlSessionFactory.openSession();
User user=new User();
user.setId(22);
user.setUsername("陈大明");
user.setBirthday(new Date());
user.setAddress("福建福州");
user.setSex("2");
sqlSession.update("test.updateUser",user);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
// 释放资源
sqlSession.close();
} }
}
最新文章
- Django models .all .values .values_list 几种数据查询结果的对比
- [mysql]数据库基础知识
- NIO框架Mina学习
- lua 闭包
- Yii常用路径说明
- 使用usb+preseed在virtualbox上安装ubuntu(一)
- HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
- Matlab 如何绘制复杂曲线的包络线
- Jquery的bootstrap在线文本编辑器插件Summernote
- Android - 用Fragments实现动态UI - 使用Android Support Library
- Java和Flex积分误差(一个)
- 【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)
- EffectiveTensorflow:Tensorflow 教程和最佳实践
- 联发科安卓6.0项目的到来的第一个难题:tar的分包与并包
- hibernate 的API使用
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
- Hi3520DV200和Hi3520DV300
- 配置使用 NTP
- 用Python开始机器学习(2:决策树分类算法)
- phantomJS浏览器
热门文章
- 标准C程序设计七---56
- 标准C程序设计七---46
- py2exe多文件转换
- Adoquery的 moveby和GotoBookmark,RecNo
- java通过代码控制线程状态,解决线程不安全的问题。
- Purpose of XMLString::transcode
- AC日记——[ZJOI2009]假期的宿舍 cogs 1333
- Codeforces Gym101606 D.Deranging Hat (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
- 51nod 1092 回文字符串【LCS】
- 第一章 “我要点爆”微信小程序云开发之项目建立与我的页面功能实现