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();
} }
}

最新文章

  1. Django models .all .values .values_list 几种数据查询结果的对比
  2. [mysql]数据库基础知识
  3. NIO框架Mina学习
  4. lua 闭包
  5. Yii常用路径说明
  6. 使用usb+preseed在virtualbox上安装ubuntu(一)
  7. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
  8. Matlab 如何绘制复杂曲线的包络线
  9. Jquery的bootstrap在线文本编辑器插件Summernote
  10. Android - 用Fragments实现动态UI - 使用Android Support Library
  11. Java和Flex积分误差(一个)
  12. 【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)
  13. EffectiveTensorflow:Tensorflow 教程和最佳实践
  14. 联发科安卓6.0项目的到来的第一个难题:tar的分包与并包
  15. hibernate 的API使用
  16. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
  17. Hi3520DV200和Hi3520DV300
  18. 配置使用 NTP
  19. 用Python开始机器学习(2:决策树分类算法)
  20. phantomJS浏览器

热门文章

  1. 标准C程序设计七---56
  2. 标准C程序设计七---46
  3. py2exe多文件转换
  4. Adoquery的 moveby和GotoBookmark,RecNo
  5. java通过代码控制线程状态,解决线程不安全的问题。
  6. Purpose of XMLString::transcode
  7. AC日记——[ZJOI2009]假期的宿舍 cogs 1333
  8. Codeforces Gym101606 D.Deranging Hat (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
  9. 51nod 1092 回文字符串【LCS】
  10. 第一章 “我要点爆”微信小程序云开发之项目建立与我的页面功能实现