MyBatis实践之实践之DAO与与Mapper
MyBatis前身是iBatis,是一个基于Java的数据持久层/对象关系映射(ORM)框架.通过本文给大家介绍MyBatis实践之DAO与Mapper的相关知识,需要的朋友参考下吧
MyBatis简介简介
MyBatis前身是iBatis,是一个基于Java的数据持久层/对象关系映射(ORM)框架.
MyBatis是对JDBC的封装,使开发人员只需关注SQL本身,而不需花费过多的精力去处理如注册驱动、设置参数、创建Connection/Statement、解析结果集等JDBC过程性代码.MyBatis基于XML/注解的方式
配置Statement,执行SQL,并将执行结果映射成Java对象, 大大降低了数据库开发的难度.
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of
parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
– MyBatis项目地址/在线文档.
初识初识MyBatis
使用MyBatis需要在pom.xml中添加如下依赖:
<code class="hljs xml"><dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.3.0</version>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.36</version>
</dependency></code>
Select
配置mybatis/mybatis-configuration.xml
作为MyBatis的全局配置文件,其配置了MyBatis的运行环境信息(如数据源/mapper文件等).
<code class="hljs xml"><code class="hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E-->
<configuration>
<environments default="development">
<environment id="development">
<!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEJDBC%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%2D%2D%3E-->
<transactionmanager type="JDBC">
<!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90%2D%2D%3E-->
<datasource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver">
<property name="url" value="jdbc:mysql://host:port/db?characterEncoding=utf-8">
<property name="username" value="username">
<property name="password" value="password">
</property></property></property></property></datasource>
</transactionmanager></environment>
</environments>
<!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDmapper%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%20%2D%2D%3E-->
<mappers>
<mapper resource="mybatis/mapper/UserDAO.xml">
</mapper></mappers>
</configuration></code></code>
书写UserDAO(mapper映射)
最为MyBatis最核心的部分,配置了操作数据库的SQL语句:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E-->
<mapper namespace="namespace">
<select id="selectUserById" parametertype="java.lang.Integer" resulttype="com.fq.domain.User">
SELECT * FROM user WHERE id = #{id};
</select>
<select id="selectUserByName" parametertype="java.lang.String" resulttype="com.fq.domain.User">
SELECT * FROM user WHERE name LIKE '%${value}%';
</select>
</mapper></code></code></code>
属性 描述
namespace 命名空间,用于隔离SQL语句
parameterType 定义SQL输入映射类型,MyBatis通过OGNL从输入对象中获取参数传入SQL语句.
resultType 定义SQL输出映射类型,MyBatis将SQL查询结果的一行记录映射为resultType指定的类型.
mapper映射文件名有UserDAO.xml/UserMapper.xml/User.xml等几种形式, 其一般存放在与mybatis-configuration.xml同级的mapper目录下,由于其主要作用为定义SQL语句与映射关系, 因此一般统称为
mapper映射文件.
定义定义PO类类
PO类主要作用为SQL(输入/输出)映射,通常与数据库表对应:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java">/**
* @author jifang
* @since 15/12/31 下午2:27.
*/
public class User {
private Integer id;
private String name;
private String password;
public User() {
}
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
评论0
最新资源