(MyBatis)原生Dao开发方法

       原生Dao开发方法需要程序员编写Dao接口Dao实现类


目录结构(有些文件没有,可以在上一篇查看):

(MyBatis)原生Dao开发方法_第1张图片


User.xml 映射文件:






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


SqlMapConfig.xml 核心配置文件:





	
	
		
			
			
			
			
			
				
				
				
				
			
		
	
	
	
		
	


UserDao接口:

package com.jadan.dao;

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

public interface UserDao {
	
	// 通过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);
	
}

UserDaoImpl 实现类:

package com.jadan.dao;

import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.jadan.po.User;

public class UserDaoImpl implements UserDao {

	private SqlSessionFactory sqlSessionFactory;
	// 通过构造方法注入会话工厂
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	// 通过id查询用户
	public User findUserById(Integer id) {
		// sqlSession是线程不安全的,所以它的最佳使用范围在方法体内
		SqlSession openSession = sqlSessionFactory.openSession();
		User user = openSession.selectOne("test.findUserById", 1);
		return user;
	}

	// 通过用户名查询用户
	public List findUserByUsername(String username) {
		SqlSession openSession = sqlSessionFactory.openSession();
		List list = openSession.selectList("test.findUserByUsername", username);
		return list;
	}

	// 插入用户数据
	public void insertUser(User user) {
		SqlSession openSession = sqlSessionFactory.openSession();
		openSession.insert("test.insertUser", user);
		// 记得提交事务
		openSession.commit();
	}

	// 更改用户数据
	public void updateUserById(User user) {
		SqlSession openSession = sqlSessionFactory.openSession();
		openSession.update("test.updateUserById", user);
		openSession.commit();
	}
	
	// 删除用户
	public void delUserById(Integer id) {
		SqlSession openSession = sqlSessionFactory.openSession();
		openSession.delete("test.delUserById", id);
		openSession.commit();
	}
}

UserDaoTest 测试类:

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.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.jadan.dao.UserDao;
import com.jadan.dao.UserDaoImpl;
import com.jadan.po.User;

public class UserDaoTest {

	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() throws Exception {
		UserDao userDao = new UserDaoImpl(factory);
		User user = userDao.findUserById(1);
		System.out.println(user);
	}
	
	// 通过用户名模糊查询用户
	@Test
	public void testFindUserByUsername() throws Exception {
		UserDao userDao = new UserDaoImpl(factory);
		List list = userDao.findUserByUsername("王");
		System.out.println(list);
	}
	
	// 测试: 插入用户数据
	@Test
	public void testInsertUser() throws Exception {
		UserDao userDao = new UserDaoImpl(factory);
		
		User user = new User();
		user.setUsername("Jadan");
		user.setBirthday(new Date());
		user.setSex("男");
		user.setAddress("福建平潭");
		
		userDao.insertUser(user);
	}
	
	// 测试: 更新用户数据
	@Test
	public void testUpdateUserById() throws Exception {
		UserDao userDao = new UserDaoImpl(factory);
		User user = new User();
		user.setId(41);
		user.setSex("女");
		userDao.updateUserById(user);
	}
	
	// 测试: 删除用户
	@Test
	public void testDelUserById() throws Exception {
		UserDao userDao = new UserDaoImpl(factory);
		userDao.delUserById(41);
	}
}


下一篇:动态代理Dao开发方法

你可能感兴趣的:(MyBatis)