本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(1)

初窥MyBatis-普通的CRUD操作

发布于2021-03-10 18:10     阅读(869)     评论(0)     点赞(23)     收藏(5)


  • 编写接口
  • 编写对应的Mapper.xml中的sql语句
  • 测试(增删改需要提交事务)
<mapper namespace="com.perwrj.dao.UserMapper">
<!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” -->
  <select id="getUserList" resultType="com.perwrj.pojo.User">
    select * from mybatis.user
  </select>
</mapper>
  • id:就是对应的namespace中的方法名
  • resultType:sql语句的返回值类型
  • parameterType:传入参数的类型
package com.perwrj.dao;

import java.util.List;

import com.perwrj.pojo.User;

public interface UserMapper {
//	查询全部用户
	List<User> getUserList();
//	根据id查询用户
	User getUserById(int id);
//	添加一个用户
	int addUser(User user);
//	修改用户
	int updateUser(User user);
//	删除用户
	int deleteUser(int id);
}

<?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">
  <!-- namespace绑定一个对应的Mapper/Dao接口 -->
<mapper namespace="com.perwrj.dao.UserMapper">
<!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” -->
  <select id="getUserList" resultType="com.perwrj.pojo.User">
    select * from mybatis.user
  </select>
  
   <select id="getUserById" parameterType="int" resultType="com.perwrj.pojo.User">
    select * from mybatis.user where id = #{id}
  </select>
  <!-- 对象中的属性可以直接取出来 -->
  <insert id="addUser" parameterType="com.perwrj.pojo.User">
  	insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
  </insert>
  
  <update id="updateUser" parameterType="com.perwrj.pojo.User">
  	update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}
  </update>
  
  <delete id="deleteUser"  parameterType="int">
  	delete from mybatis.user where id = #{id}
  </delete>
</mapper>
package com.perwrj.dao;



import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.perwrj.pojo.User;
import com.perwrj.utils.MyBatisUtil;

public class UserMapperTest {
	@Test
	public void test() {
		//获得SqlSession对象,
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		
		try {
//			方式一:getMapper执行sql:更加简洁,类型更加安全,推荐使用
			
			  UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User>
			  userList = userMapper.getUserList();
			
			  for (User user : userList) { System.out.println(user); }
	
			//方式一结束
			
//			方式二:老方法,不建议使用,
			/*
			 * List<User> list =
			 * sqlSession.selectList("com.perwrj.dao.UserMapper.getUserList");
			 * 
			 * for (User object : list) { System.out.println(object); }
			 */
			//方式二结束
		}catch(Throwable e) {
			e.printStackTrace();
		}finally {
//			关闭sqlSession
			sqlSession.close();
		}	
	}
	/**
	 * 删除操作
	 */
	@Test
	public void deleteUserTest() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		
		try {
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 
			int num = userMapper.deleteUser(4);
			if(num > 0) {
//				提交事务
				System.out.println(num);//1
				sqlSession.commit();
				sqlSession.close();
			}
		}catch(Throwable e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}		
	}
					
					
	
	
	/**
	 * 改操作
	 */
	@Test
	public void updateUserTest() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		
		try {
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 
			int num = userMapper.updateUser(new User(2,"杰瑞","987654"));
			if(num > 0) {
//				提交事务
				System.out.println(num);//1
				sqlSession.commit();
				sqlSession.close();
			}
		}catch(Throwable e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}			
	}
	
	/**
	 * 增删改需要提交事务
	 */
	@Test
	public void addUserTest() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
				
		try {
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 
			int num = userMapper.addUser(new User(4,"王瑞杰","123568"));
			if(num > 0) {
//				提交事务
				System.out.println(num);//1
				sqlSession.commit();
				sqlSession.close();
			}
		}catch(Throwable e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}	
	}
	
	@Test
	public void testGetUserById() {
		//获得SqlSession对象,
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		
		try {
			  UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 
			  User user = userMapper.getUserById(1);
			  System.out.println(user);
		}catch(Throwable e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}	
	}
	

}


所属网站分类: 技术文章 > 博客

作者:咿呀咿呀哟

链接:http://www.javaheidong.com/blog/article/112345/24d04c380eb5a28c66a6/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)