(MyBatis)动态代理Dao开发方法

相关文件:

(MyBatis)动态代理Dao开发方法_第1张图片


UserMapper.xml 映射文件(记得与接口同名):







	
	
	
	
	
	
	
	
		
			select LAST_INSERT_ID()
		
		insert into user(username, sex, birthday, address)
		 value(#{username},#{sex},#{birthday},#{address})
	
	
	
	
		update user set sex = #{sex} where id = #{id}
	
	
	
	
		delete from user where id = #{id}
	

(MyBatis)动态代理Dao开发方法_第2张图片


UserMapper.java 接口文件:

package com.jadan.mapper;

import java.util.List;
import com.jadan.po.User;

public interface UserMapper {

	// 根据id查询用户
	public User findUserById(Integer id);
	
	// 根据用户名模糊查询用户
	public List findUserByUsername(String username);
	
	// 插入用户
	public void insertUser(User user);
	
	// 更新用户
	public void updateUserById(User user);
	
	// 删除用户
	public void delUserById(Integer id);
}

SqlMapConfig.xml 核心配置文件:





	
	
		
			
			
			
			
			
				
				
				
				
			
		
	
	
	
	
		
				
	

(MyBatis)动态代理Dao开发方法_第3张图片


UserMapperTest 测试文件:

package mybatis_day01;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.jadan.mapper.UserMapper;
import com.jadan.po.User;

public class UserMapperTest {
	
	private SqlSessionFactory factory;
	// 作用: 在测试方法前执行这个初始化方法
	@Before
	public void setUp() throws Exception {
		// 核心配置文件名
		String resource = "SqlMapConfig.xml";
		// 通过流将核心配置文件读取进来
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 通过核心配置文件输入流来创建会话工厂
		factory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	// 测试: 通过id查用户
	@Test
	public void testFindUserById() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		User user = mapper.findUserById(1);
		System.out.println(user);
	}
	
	// 测试: 通过用户名模糊查询用户
	@Test
	public void testFindUserByUsername() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		List list = mapper.findUserByUsername("王");
		System.out.println(list);
	}

	// 测试: 插入用户
	@Test
	public void testInsertUser() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		User user = new User();
		user.setUsername("Jadan");
		user.setBirthday(new Date());
		user.setSex("男");
		user.setAddress("China");
		
		mapper.insertUser(user);
		
		// 提交事务
		openSession.commit();
	}
	
	// 测试: 更新
	@Test
	public void testUpdate() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		User user = new User();
		user.setId(42);
		user.setSex("男");
		mapper.updateUserById(user);
		
		openSession.commit();
	}
	
	// 测试: 删除
	@Test
	public void testDelUserById() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		mapper.delUserById(42);
		openSession.commit();
	}
}

下一篇:

你可能感兴趣的:(MyBatis)