Mybatis的开发方式(Mybatis与数据库交互方式)

Mybatis工具类


public class MybatisUtil {

	static SqlSessionFactory sqlSessionFactory;

	static {		
		String resource = "mybatis-config.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}	
	/**
	 * 该方法调用必须在应用程序的成员方法中调用
	 * @return
	 */
	public static SqlSession findSqlSession(){
		
		return sqlSessionFactory.openSession();
	}
}

mapper.xml




	

	
	
	
	
	
	
	
	
		insert into USER(username,password) values(#{username},#{password})
	
	
	
		delete from USER where id = #{id}
	
	
	
		update USER set username=#{username},password=#{password} where id = #{id}
	

dao层代码

public class UserDao {

	public void insert(User user) {

		SqlSession session = MybatisUtil.findSqlSession();
		try {
			session.insert("dao.UserMapper.insert", user);
			// 提交事务
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			session.close();
		}
	}

	public void delete(User user) {

		SqlSession session = MybatisUtil.findSqlSession();
		try {
			session.delete("dao.UserMapper.delete", user);
			// 提交事务
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			session.close();
		}
	}

	public void update(User user) {
		SqlSession session = MybatisUtil.findSqlSession();
		try {
			session.update("dao.UserMapper.update", user);
			// 提交事务
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			session.close();
		}
	}

	public User selectById(int id) {

		SqlSession session = MybatisUtil.findSqlSession();
		User user = null;
		try {
			user = session.selectOne("dao.UserMapper.selectById", id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	public List selectAll() {		
		SqlSession session = MybatisUtil.findSqlSession();
		List users = null;
		try {
			users = session.selectList("dao.UserMapper.selectAll");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return users;
	}

	public User selectByItem(User user) {
		SqlSession session = MybatisUtil.findSqlSession();
		try {
			user = session.selectOne("dao.UserMapper.selectByItem", user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}
}

service层代码

public class UserService {

	UserDao dao = new UserDao();

	public void save(User user) {

		dao.insert(user);

	}

	public void remove(User user) {

		dao.delete(user);
	}

	public void modify(User user) {
		dao.update(user);
	}

	public User findById(int id) {
		return dao.selectById(id);
	}

	public List findAll() {
		return dao.selectAll();
	}

	public User findByItem(User user) {

		return dao.selectByItem(user);
	}
}

单元测试类 省略...

你可能感兴趣的:(第三阶段)