ORMapping: Object Relationship Mapping 对象关系映射
对象指面向对象
关系指关系型数据库
Java 到 MySQL 的映射,开发者可以以⾯面向对象的思想来管理理数据库。
如何使用
通过 Mapper 代理理实现自定义接口,定义相关业务方法。
编写与方法相对应的 Mapper.xml。
1、自定义接口
package com.southwind.repository;
import com.southwind.entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int deleteById(long id);
public List<Account> findAll();
public Account findById(long id);
}
2、创建接口对应的 Mapper.xml,定义接口方法对应的 SQL 语句句。
statement 标签可根据 SQL 执⾏行行的业务选择 insert、 delete、 update、 select。
MyBatis 框架会根据规则⾃自动创建接⼝口实现类的代理理对象。
规则:
Mapper.xml 中 namespace 为接口的全类名。
Mapper.xml 中 statement 的 id 为接口中对应的方法名。
Mapper.xml 中 statement 的 parameterType 和接口中对应方法的参数类型一致。
Mapper.xml 中 statement 的 resultType 和接口中对应方法的返回值类型一致。
<?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.southwind.repository.AccountRepository">
<insert id="save" parameterType="com.southwind.entity.Account">
insert into t_account(username,password,age) values(#{username},#
{password},#{age})
</insert>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password = #{password},age
= #{age} where id = #{id}
</update>
<delete id="deleteById" parameterType="long">
delete from t_account where id = #{id}
</delete>
<select id="findAll" resultType="com.southwind.entity.Account">
select * from t_account
</select>
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
</mapper>
Mapper.xml参数传递
statement 标签: select、 update、 delete、 insert 分别对应查询、修改、删除、添加操作。
- parameterType:参数数据类型
1、基本数据类型,通过 id 查询 Account
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
2、 String 类型,通过 name 查询 Account
<select id="findByName" parameterType="java.lang.String"
resultType="com.southwind.entity.Account">
select * from t_account where username = #{username}
</select>
3、包装类,通过 id 查询 Account
<select id="findById2" parameterType="java.lang.Long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
4、多个参数,通过 name 和 age 查询 Account
<select id="findByNameAndAge" resultType="com.southwind.entity.Account">
select * from t_account where username = #{arg0} and age = #{arg1}
</select>
5、 Java Bean
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password = #{password},age =
#{age} where id = #{id}
</update>
- resultType:结果类型
1、基本数据类型,统计 Account 总数
<select id="count" resultType="int">
select count(id) from t_account
</select>
2、包装类,统计 Account 总数
<select id="count2" resultType="java.lang.Integer">
select count(id) from t_account
</select>
3、 String 类型,通过 id 查询 Account 的 name
<select id="findNameById" resultType="java.lang.String">
select username from t_account where id = #{id}
</select>
4、 Java Bean
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
级联查询
- 一对多
Student
package com.southwind.entity;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private Classes classes;
}
Classes
package com.southwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Classes {
private long id;
private String name;
private List<Student> students;
}
public interface StudentRepository {
public Student findById(long id);
}
StudentRepository.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.southwind.repository.StudentRepository">
<resultMap id="studentMap" type="com.southwind.entity.Student">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<association property="classes" javaType="com.southwind.entity.Classes">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</association>
</resultMap>
<select id="findById" parameterType="long" resultMap="studentMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where s.id = #{id} and s.cid = c.id
</select>
</mapper>
public interface ClassesRepository {
public Classes findById(long id);
}
ClassesRepository.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.southwind.repository.ClassesRepository">
<resultMap id="classesMap" type="com.southwind.entity.Classes">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="students" ofType="com.southwind.entity.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<select id="findById" parameterType="long" resultMap="classesMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where c.id = #{id} and s.cid = c.id
</select>
</mapper>