MyBatis第二天-Mybatis增删改查
2022/1/12 23:03:40
本文主要是介绍MyBatis第二天-Mybatis增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.dao层
mapper
public interface UserMapper{ List<User> gstUser(); List<User> getUserById(int id); boolean delete(String username); int Insert(User user); int updata(User user); }
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.nly.dao.UserMapper"> <!-- id 绑定方法名--> <select id="gstUser" resultType="com.nly.pojo.User"> select * from mybatis.user; </select> <select id="getUserById" parameterType="int" resultType="com.nly.pojo.User"> select * from mybatis.user where id = #{id}; </select> <delete id="delete" parameterType="String" > delete from mybatis.user where username = #{username} </delete> <insert id="Insert" parameterType="com.nly.pojo.User"> insert into mybatis.user (id,username,password) values (#{id},#{username},#{password}) </insert> <update id="updata" parameterType="com.nly.pojo.User"> update mybatis.user set username=#{username},password =#{password} where id = #{id}; </update> </mapper>
2.Test
注意点增删改需要提交事务,才能对数据库内元素造成影响
sqlSession.commit();
package com.nly.dao; import com.nly.pojo.User; import com.nly.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void Test(){ // 第一步:获取sqlSession SqlSession sqlSession = MybatisUtils.getSqlSession(); // 方式一:第二步:执行sql UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.gstUser(); for (User user : users) { System.out.println(user); } // 第三步:关闭sqlSession sqlSession.close(); } @Test public void getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> user = mapper.getUserById(2); System.out.println(user); }catch (Exception e){ e.printStackTrace(); } sqlSession.close(); } @Test public void delete(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); boolean flag = mapper.delete("lizzy"); //提交事务!!!!! sqlSession.commit(); Test(); if(flag){ System.out.println("ok"); }else{ System.out.println("error"); } }catch (Exception e){ e.printStackTrace(); } sqlSession.close(); } @Test public void updata(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); Test(); mapper.updata(new User(2,"awei","1313")); //提交事务!!!!! sqlSession.commit(); Test(); }catch (Exception e){ e.printStackTrace(); } sqlSession.close(); } @Test public void Insert(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.Insert(new User(4,"add","4646")); //提交事务!!!!! sqlSession.commit(); Test(); }catch (Exception e){ e.printStackTrace(); } sqlSession.close(); } }
这篇关于MyBatis第二天-Mybatis增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?