mybatis-使用原始dao

SqlMapConfig.xml
  
 


	
	
		
		
	
	
	
	

	
	
		
		
		
		
		
	

	
		
			
			
				
				
				
				
			
		
	
	
		
		
		
		
		
		
		
		 
	

User.xml





	
	
	
	
	
	
	
	
	
	
		insert into Employee(email,hiredate,name,salary,pwd,grade,department_id) values(#{email},#{hiredate},#{name},#{salary},#{pwd},#{grade},#{department_id})
	
	
	
	 
	 	delete from Employee where id =#{id}
	 
	 
	 
	 
		 update Employee set department_id=#{department_id},email=#{email},grade=#{grade},hiredate=#{hiredate},name=#{name},pwd=#{pwd},salary=#{salary} where id =#{id}
	 
EmployeeDao.java
package com.itcast.mybatis.dao;

import java.util.List;

import com.itvast.mybatis.po.Employee;

public interface EmployeeDao {
	//根据id查询用户信息
	public Employee findUserById(int id) throws Exception;
	//添加用户信息
	public void insertEmployee(Employee employee) throws Exception;
	//删除用户信息
	public void delectEmployee(int id)throws Exception;
	//根据用户名称查询用户列表
	public List findUserByName(String name) throws Exception;
}
EmployeeDaoImp.java
package com.itcast.mybatis.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.itvast.mybatis.po.Employee;

public class EmployeeDaoImp implements EmployeeDao {
	
	//需要想dao实现类注入SqlSessionFactory
	//这里通过构造函数注入
	private SqlSessionFactory sqlSessionFactory;
	public EmployeeDaoImp(SqlSessionFactory sessionfactory){
		sqlSessionFactory=sessionfactory;
	}
	
	@Override
	public Employee findUserById(int id) throws Exception {
		// TODO Auto-generated method stub
		SqlSession sqlSession=sqlSessionFactory.openSession();
		Employee employee=sqlSession.selectOne("test.findUserById", id);
		sqlSession.close();
		return employee;
	}

	@Override
	public void insertEmployee(Employee employee) throws Exception {
		// TODO Auto-generated method stub
		SqlSession sqlSession=sqlSessionFactory.openSession();
		sqlSession.insert("test.insertUser", employee);
		sqlSession.commit();
		sqlSession.close();
	}

	@Override
	public void delectEmployee(int id) throws Exception {
		// TODO Auto-generated method stub
		SqlSession sqlSession=sqlSessionFactory.openSession();
		sqlSession.delete("test.delectEmployee", id);
		sqlSession.commit();
		sqlSession.close();
	}

	@Override
	public List findUserByName(String name) throws Exception {
		// TODO Auto-generated method stub
		SqlSession sqlSession=sqlSessionFactory.openSession();
		List list=sqlSession.selectList("test.findUserByName", name);
		sqlSession.close();
		return list;
	}

}
test.java
package com.itcast.mybatis.dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class test {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		InputStream inputStream=Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory sessionfactory=new SqlSessionFactoryBuilder().build(inputStream);
		EmployeeDao employeeDao=new EmployeeDaoImp(sessionfactory);
		System.out.println(employeeDao.findUserById(1007).getName());
	}

}




你可能感兴趣的:(mybatis)