简介
什么是 MyBatis ?
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
下载MyBatis的相关的Jar包:
mybatis-3.2.8.jar
mybatis-spring-1.2.2.jar
配置:Mybitis的Configure.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> <typeAliases> <typeAlias alias="User" type="com.yiibai.mybatis.models.User" /> 配置别名 别名是一个较短的Java 类型的名称这只是与
</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://192.168.0.13:3306/bookstore" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource></environment></environments><mappers> <!-- // power by http://www.yiibai.com --> <mapper resource="com/yiibai/mybatis/models/User.xml" /></mappers></configuration> 创建数据库:XML 配置文件相关联,减少输入多余的完整类
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`dept` varchar(254) NOT NULL DEFAULT '',
`website` varchar(254) DEFAULT '',
`phone` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'yiibai', 'Tech', 'http://www.yiibai.com', '13800009988');
创建映射的实体类
package com.yiibai.mybatis.models;
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String website;
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
同时创建UserMapper.Java
package com.yiibai.mybatis.models;
import java.util.List;
import java.util.Map;
public interface UserMapper {
public User GetUserByID(int id);//查询某一个
public List<User> getUserList();//查询所有
public void insertUser(User user);//插入
public void updateUser(User user);//更新
public void deleteUser(int userId);//删除
//public User getUser(int id);
}
创建User.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.yiibai.mybatis.models.UserMapper">
<resultMap id="user" type="User"> //返回的结果类型
<id column="id" property="id" />
<result column="name" property="name" />
<result column="dept" property="dept" />
<result column="website" property="website" />
<result column="phone" property="phone" />
</resultMap>
<resultMap id="map" type="java.util.HashMap"> 返回的结果类型
<id column="id" property="id" />
<result column="name" property="name" />
<result column="dept" property="dept" />
<result column="website" property="website" />
<result column="phone" property="phone" />
</resultMap>
<select id="GetUserByID" parameterType="int" resultType="User"> 根据Id查询
select * from `user` where id = #{id}
</select>
<insert id="insertUser" parameterType="User"> //插入User
insert into user values (#{id},#{name},#{dept},#{website},#{phone})
</insert>
<select id="getUserList" resultMap="user"> 查询所有的
select * from `user` order by id
</select>
<select id="getMtypeList" resultMap="map">
select * from `user` order by id
</select>
<delete id="deleteUser" parameterType="int"> 删除
DELETE FROM `user` WHERE id = #{id}
</delete>
<update id="updateUser" parameterType="User"> 更新
update user set name=#{name},dept=#{dept},website=#{website} where id=#{id}
</update>
</mapper>
创建测试类首先加载配置文件:
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
//加载配置文件:
static {
try {
reader = Resources.getResourceAsReader("Configure.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
创建测试方法:
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (int i = 0; i < userList.size(); i++) {
System.err.println(userList.get(i).toString());
}
//其他的与此一样,不在贴出代码