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增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)