首先,建立PostgreSQL数据库(desktop)的数据表(admin),其结构如下图所示:
然后,用Eclipse建立Java Project,将mybatis和postgresql的jar库导入进来。
再按照下图所示,建立配置文件(configuration.xml),映射文件(admin.xml),以及java文件。
configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" />
</settings>
<typeAliases>
<!--给实体类起一个别名 user -->
<typeAlias alias="Admin" type="PO.Admin" />
</typeAliases>
<!--数据源配置 这块用 BD2数据库 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver" />
<property name="url"
value="jdbc:postgresql://127.0.0.1:5432/desktop?charSet=utf-8" />
<property name="username" value="postgres" />
<property name="password" value="gjs@y1" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
<mapper resource="config/admin.xml" />
</mappers>
</configuration>
admin.xml
<?xml version="1.0" encoding="UTF-8" ?>AdminDao.java
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--这块等于dao接口的实现 namespace必须和接口的类路径一样 -->
<mapper namespace="DAO.AdminDao">
<!--type是在configuration.xml里定义过的typeAlias -->
<resultMap id="AdminResult" type="Admin">
<result column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<insert id="addAdmin" parameterType="Admin" useGeneratedKeys="true" keyProperty="id">
insert into
admin(username,password,create_time)
values(#{username},#{password},now())
</insert>
<update id="updateAdmin" parameterType="Admin">
update admin set
username=#{username:VARCHAR},password=#{password:VARCHAR} where
id=#{id:INTEGER}
</update>
<select id="findAdmin" parameterType="int" resultMap="AdminResult">
select *
from admin where id = #{id:INTEGER}
</select>
<delete id="deleteAdmin" parameterType="int">
delete
from admin where
id=#{id:INTEGER}
</delete>
<select id="countAdmin" resultType="int">
select count(*) from admin
</select>
<select id="listAdmin" resultMap="AdminResult">
select * from admin order by id
</select>
</mapper>
package DAO;Admin.java
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<Admin> listAdmin();
}
package PO;test.java
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 + "]";
}
}
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<Admin> 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