MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。具体概念,想要跟多了解可以去问度娘。此处不多言。
最近在开发屋祺的项目,使用的持久层框架是MyBatis,使用XML文件直接映射数据库中的记录,特别方便。
既然是使用xml文件直接映射数据库中的记录,那么我们要先将数据库创建好。下面先简单的创建一个数据库,里边有一张usrs表.(我使用的是mysql数据库。)
1、创建数据库,这个大家都会,就不多说了。
表如下图:
2、写xml文件。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<classPathEntry location="D:/mysql-connector-java-5.1.7.jar" />
<context id="MySqlTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"
userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="mybatis.model"
targetProject="test\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mybatis.mapping"
targetProject="test\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="mybatis.dao" targetProject="test\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="users">
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
4、生成相应的dao.mapping和model
Dao层:
package mybatis.dao;
import java.util.List;
import mybatis.model.Users;
import mybatis.model.UsersExample;
import org.apache.ibatis.annotations.Param;
public interface UsersMapper {
int countByExample(UsersExample example);
int deleteByExample(UsersExample example);
int deleteByPrimaryKey(Integer id);
int insert(Users record);
int insertSelective(Users record);
List<Users> selectByExample(UsersExample example);
Users selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Users record, @Param("example") UsersExample example);
int updateByExample(@Param("record") Users record, @Param("example") UsersExample example);
int updateByPrimaryKeySelective(Users record);
int updateByPrimaryKey(Users record);
}
Maping相应的sql语句,都在这里,可以在他的基础上修改,修改成自己要的sql语句。
Model层:
package mybatis.model;
public class Users {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
}
使用mybatis的优点:
1、易于上手和掌握
2、SQL写字XML文件里,便于统一管理和优化。
3、接触SQL与程序代码的耦合
4、提供映射标签,支持对象与数据库的ORM字段关系映射。
Hibernate和Mybatis都是比较优秀的O/Rmapping框架,Hibernate对于数据库结构提供了比较完整的封装,Hibernate的O/R Mapping实现了POJO和数据库表之间的映射,以及SQL的自动生成和执行。
MyBatis则是通过映射配置文件就是xml文件,将SQL所需的参数,以及返回的结构字段映射到指定的POJO。