第一步,创建mybatis-config.xml
sql文件:
DROP TABLE IF EXISTS `hms_user`;
CREATE TABLE `hms_user` (
`id` varchar(50) NOT NULL COMMENT 'uuid',
`username` varchar(20) NOT NULL,
`userpwd` varchar(20) NOT NULL,
`birthdy` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of hms_user
-- ----------------------------
INSERT INTO `hms_user` VALUES ('1', '张山', '123456', '2013-11-07 14:30:44');
INSERT INTO `hms_user` VALUES ('2', '李四', '123456', '2013-10-29 14:30:48');
INSERT INTO `hms_user` VALUES ('3', '王五', '123456', '2013-11-07 02:30:44');
示例如下:
<?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>
<typeAliases>
<typeAlias alias="HmsUser" type="com.mybatis.model.HmsUser" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/model/HmsUser.xml"/>
</mappers>
</configuration>
第二步,调用mybatis-config.xml,代码如下:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
第三步,创建mapper.xml和model类
package com.mybatis.model;
import java.sql.Timestamp;
/**
*@文件名 xxx.java
*@E-Mail xy511500623@sina.cn
*@创建日期 2013-12-19 下午6:44:20
*/
public class HmsUser implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String username;
private String userpwd;
private Timestamp birthdy;
// Constructors
/** default constructor */
public HmsUser() {
}
/** minimal constructor */
public HmsUser(String id, String username, String userpwd) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
}
/** full constructor */
public HmsUser(String id, String username, String userpwd, Timestamp birthdy) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
this.birthdy = birthdy;
}
// Property accessors
public HmsUser(String username, String userpwd) {
this.username = username;
this.userpwd = userpwd;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return this.userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public Timestamp getBirthdy() {
return this.birthdy;
}
public void setBirthdy(Timestamp birthdy) {
this.birthdy = birthdy;
}
}
对应的xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.model.HmsUserMapper">
<!-- 一定要和mybatis-config.xml中的typeAlias 对应 -->
<!--这里resultType也可以写为com.mybatis.model.HmsUser-->
<select id="selectUserByID" parameterType="string" resultType="HmsUser">
select * from `hms_user` where id = #{id}
</select>
</mapper>
第四步,测试
package com.mybatis.example;
import java.io.IOException;
import java.io.InputStream;
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 com.mybatis.model.HmsUser;
/**
*@文件名 Test.java
*@E-Mail xy511500623@sina.cn
*@创建日期 2013-12-19 下午5:42:56
*/
public class Test {
public static void main(String[] args) throws Exception {
String resource = "mybatis-config.xml";
System.out.println(Test.class.getResource("/"));
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
HmsUser user = (HmsUser) session.selectOne("com.mybatis.model.HmsUserMapper.selectUserByID", 1);
System.out.println(user.getUsername());
} finally {
session.close();
}
}
}
第五步,查看运行结果
项目结构如下:
项目源码:http://download.csdn.net/detail/yx511500623/6741711