Mybatis3简单使用(PostgreSQL)

Mybatis中文文档,参见其官网: http://mybatis.github.io/mybatis-3/zh/index.html

首先,建立PostgreSQL数据库(desktop)的数据表(admin),其结构如下图所示:
Mybatis3简单使用(PostgreSQL)_第1张图片
然后,用Eclipse建立Java Project,将mybatis和postgresql的jar库导入进来。
再按照下图所示,建立配置文件(configuration.xml),映射文件(admin.xml),以及java文件。

Mybatis3简单使用(PostgreSQL)_第2张图片

configuration.xml

     


	
		
	
	
		
		
	
	
	
		
			
			
				
				
				
				
			
		
	
	
		
		
	
   

admin.xml

  



	
	
		
		
		
		
	

	
		insert into
		admin(username,password,create_time)
		values(#{username},#{password},now())
	
	
		update admin set
		username=#{username:VARCHAR},password=#{password:VARCHAR} where
		id=#{id:INTEGER}
	
	
	
		delete
		from admin where
		id=#{id:INTEGER}
	
	
	
  
AdminDao.java
package DAO;

import java.util.List;

import PO.Admin;

public interface AdminDao {
	public Integer addAdmin(Admin user);

	public boolean updateAdmin(Admin user);

	public boolean deleteAdmin(Integer Id);

	public Admin findAdmin(Integer Id);

	public int countAdmin();

	public List listAdmin();
}
Admin.java
package PO;

import java.util.Date;

public class Admin {
	private Integer id;
	private String username;
	private String password;
	private Date createTime;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Override
	public String toString() {
		return "Admin [id=" + id + ", username=" + username + ", password="
				+ password + ", createTime=" + createTime + "]";
	}
}
test.java
package test;

import java.io.Reader;
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 DAO.AdminDao;
import PO.Admin;

/**
 * myBatis数据库连接测试
 */
public class test {
	/**
	 * 获得MyBatis SqlSessionFactory, SqlSessionFactory负责创建SqlSession,
	 * 一旦创建成功,就可以用SqlSession实例来执行映射语句,commit,rollback,close等方法
	 */
	public static void main(String[] args) throws Exception {
		Reader reader = Resources.getResourceAsReader("configuration.xml");
		SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
		SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		AdminDao adminDao = sqlSession.getMapper(AdminDao.class);

		Admin admin = new Admin();
		admin.setUsername("gaojs");
		admin.setPassword("123");
		Integer aRet = adminDao.addAdmin(admin);
		System.out.println("addAdmin, aRet:" + aRet);
		System.out.println("addAdmin:" + admin);

		Admin found = adminDao.findAdmin(admin.getId());
		System.out.println("findAdmin:" + found);

		found.setPassword("1234");
		boolean uRet = adminDao.updateAdmin(found);
		System.out.println("updateAdmin, uRet:" + uRet);
		Admin found2 = adminDao.findAdmin(admin.getId());
		System.out.println("findAdmin:" + found2);

		int count = adminDao.countAdmin();
		System.out.println("countAdmin, count:" + count);

		List list = adminDao.listAdmin();
		System.out.println("listAdmin, list:" + list);

		boolean dRet = adminDao.deleteAdmin(admin.getId());
		System.out.println("deleteAdmin:" + dRet);

		sqlSession.commit();
		sqlSession.close();
	}
}

运行测试,发现确实能插入数据!其他接口,也都能正常实现其功能。

addAdmin, aRet:1
addAdmin:Admin [id=57, username=gaojs, password=123, createTime=null]
findAdmin:Admin [id=57, username=gaojs, password=123, createTime=Fri Oct 31 17:47:39 CST 2014]
updateAdmin, uRet:true
findAdmin:Admin [id=57, username=gaojs, password=1234, createTime=Fri Oct 31 17:47:39 CST 2014]
countAdmin, count:1
listAdmin, list:[Admin [id=57, username=gaojs, password=1234, createTime=Fri Oct 31 17:47:39 CST 2014]]
deleteAdmin:true



你可能感兴趣的:(Mybatis3简单使用(PostgreSQL))