使用MyBatis实现数据库的单表增删改查(CRUD)

(1)创建项目,导入项目需要的jar包
(2)编写MyBatis核心配置文件,如下:
MyBatis:



  
  	
  	
  		
  	
  	
  		
  	
  	
  		
  			
  			
  				
  				
  				
  				
  			
  		
  	
  	
  		
  	
  

(3)编写mapping映射文件
UserMapper.xml:



 
 	
 	 
 	 	insert into t_user values(default,#{username},#{password})
 	 
 	 
 	 
 	 	update t_user
 	 	
 	 		id=#{id},
 	 		
 	 		username=#{username},
 	 		
 	 		
 	 		password=#{password},
 	 		
 	 	
 	 	
 	 	id=#{id}
 	 	
 	 
 	 
 	 	delete from t_user where id = #{0}
 	 
 

(4)编写UserMapper接口:

package an.sz.mapper;

import java.util.List;

import an.sz.pojo.User;

public interface UserMapper {
	List selAll();
	int add(User user);
	int update(User user);
	int delete(int id);
}

(5)业务层的接口编写:
UserService:

package an.sz.service;

import java.util.List;

import an.sz.pojo.User;

public interface UserService {
	List selAll();
	int insert(User user);
	int update(User user);
	int delete(int id);
}

(6)业务层的实现类编写:
UserServiceImpl:

package an.sz.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import an.sz.mapper.UserMapper;
import an.sz.pojo.User;
import an.sz.util.MyBatisUtil;

public class UserServiceImpl implements UserService{
	
	/**
	 * 查询所有用户信息
	 */
	@Override
	public List selAll() {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		List list = mapper.selAll();
		session.close();
		return list;
	}
	
	/**
	 * 插入用户信息
	 */
	@Override
	public int insert(User user) {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		int num = mapper.add(user);
		if(num>0) {
			System.out.println("SUCCESS");
			session.commit();
		}else {
			System.out.println("FAILED");
			session.rollback();
		}
		session.close();
		return num;
	}

	@Override
	public int update(User user) {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		int num = mapper.update(user);
		if(num>0) {
			System.out.println("SUCCESS");
			session.commit();
		}else {
			System.out.println("FAILED");
			session.rollback();
		}
		session.close();
		return num;
	}

	@Override
	public int delete(int id) {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		int num = mapper.delete(id);
		if(num>0) {
			System.out.println("SUCCESS");
			session.commit();
		}else {
			System.out.println("FAILED");
			session.rollback();
		}
		session.close();
		return num;
	}

}

(7)测试类的编写:
TestCRUD:

package an.sz.test;

import java.util.List;

import org.junit.Test;

import an.sz.pojo.User;
import an.sz.service.UserService;
import an.sz.service.UserServiceImpl;

public class TestCRUD {
	@Test
	public void testSelAll() {
		UserService us = new UserServiceImpl();
		List list = us.selAll();
		for (User user : list) {
			System.out.println(user);
		}
	}
	@Test
	public void testInsert() {
		UserService us = new UserServiceImpl();
		User user = new User();
		user.setUsername("蜡笔小新");
		user.setPassword("222");
		int num = us.insert(user);
	}
	@Test
	public void testUpdate() {
		UserService us = new UserServiceImpl();
		User user = new User();
		user.setUsername("");
		user.setPassword("111");
		int num = us.insert(user);
	}
	@Test
	public void testDelete() {
		UserService us = new UserServiceImpl();
		int num = us.delete(4);
	}
	
}

你可能感兴趣的:(使用MyBatis实现数据库的单表增删改查(CRUD))