SSM11.2【Mybatis:Mybatis的增删改查操作】

2021/7/22 23:06:35

本文主要是介绍SSM11.2【Mybatis:Mybatis的增删改查操作】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

 

 

 

 

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="userMapper">
 5 
 6     <!--查询操作-->
 7     <select id="findAll" resultType="com.haifei.domain.User">
 8         select * from user
 9     </select>
10     
11     <!--插入操作-->
12     <insert id="save" parameterType="com.haifei.domain.User">
13         insert into user values(#{id},#{username},#{password})
14     </insert>
15 
16     <!--修改操作-->
17     <update id="update" parameterType="com.haifei.domain.User">
18         update user set username=#{username},password=#{password} where id=#{id}
19     </update>
20 
21     <!--删除操作-->
22     <delete id="del" parameterType="java.lang.Integer">
23         delete from user where id=#{id} /*这个输入变量写啥都行,习惯上也跟属性名一致*/
24     </delete>
25 </mapper>
  1 package com.haifei.test;
  2 
  3 import com.haifei.domain.User;
  4 import org.apache.ibatis.io.Resources;
  5 import org.apache.ibatis.session.SqlSession;
  6 import org.apache.ibatis.session.SqlSessionFactory;
  7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8 import org.junit.Test;
  9 
 10 import java.io.IOException;
 11 import java.io.InputStream;
 12 import java.util.List;
 13 
 14 public class MybatisTest {
 15 
 16     /**
 17      * 查询操作
 18      *  Mybatis快速入门测试
 19      * @throws IOException
 20      */
 21     @Test
 22     public void test1() throws IOException {
 23         //加载Mybatis核心配置文件
 24         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
 25         //获取session工厂对象
 26         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
 27         //获取session会话对象
 28         SqlSession sqlSession = sqlSessionFactory.openSession();
 29         //执行操作(参数:namespace+.+id)
 30         List<User> userList = sqlSession.selectList("userMapper.findAll");
 31         //打印结果
 32         System.out.println(userList); //[User{id=1, username='zhangsan', password='123'}, User{id=2, username='lisi', password='345'}, User{id=3, username='wangwu', password='789'}]
 33         //释放资源
 34         sqlSession.close();
 35     }
 36 
 37     /**
 38      * 插入操作
 39      *  对数据库表中数据有影响的操作(增删改)执行完后要提交事务,查询这种对数据无影响的操作不需提交事务
 40      *         原始JDBC中的事务是默认开启的,即执行完update后事务自动提交(查询用query,增删改用update)
 41      *         Mybatis中的事务是默认关闭的,需要手动提交事务
 42      * @throws IOException
 43      */
 44     @Test
 45     public void test2() throws IOException {
 46         //模拟User对象
 47         User user = new User();
 48         user.setUsername("tom");
 49         user.setPassword("abc");
 50 
 51         //加载Mybatis核心配置文件
 52         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
 53         //获取session工厂对象
 54         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
 55         //获取session会话对象
 56         SqlSession sqlSession = sqlSessionFactory.openSession();
 57         //执行操作
 58         sqlSession.insert("userMapper.save", user);
 59         //提交事务
 60         sqlSession.commit();
 61         //释放资源
 62         sqlSession.close();
 63     }
 64 
 65     /**
 66      * 修改操作
 67      * @throws IOException
 68      */
 69     @Test
 70     public void test3() throws IOException {
 71         //模拟User对象
 72         User user = new User();
 73         user.setId(5);
 74         user.setUsername("lucy");
 75         user.setPassword("def");
 76 
 77         //加载Mybatis核心配置文件
 78         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
 79         //获取session工厂对象
 80         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
 81         //获取session会话对象
 82         SqlSession sqlSession = sqlSessionFactory.openSession();
 83         //执行操作
 84         sqlSession.update("userMapper.update", user);
 85         //提交事务
 86         sqlSession.commit();
 87         //释放资源
 88         sqlSession.close();
 89     }
 90 
 91     /**
 92      * 删除操作
 93      * @throws IOException
 94      */
 95     @Test
 96     public void test4() throws IOException {
 97         //加载Mybatis核心配置文件
 98         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
 99         //获取session工厂对象
100         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
101         //获取session会话对象
102         SqlSession sqlSession = sqlSessionFactory.openSession();
103         //执行操作
104         sqlSession.delete("userMapper.del", 5);
105         //提交事务
106         sqlSession.commit();
107         //释放资源
108         sqlSession.close();
109     }
110 
111 }

 



这篇关于SSM11.2【Mybatis:Mybatis的增删改查操作】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程