MyBatis:配置文件Mapper.xml

一、Mapper.xml 的作用?

MyBatis 框架是一个“半自动”的 ORM 框架,SQL 语句需要开发者自定义,MyBatis 框架的关注点在于 Java Bean 于 SQL 之间的映射关系,自定义的 SQL 语句定义在 Mapper.xml 中。

二、Mapper.xml 常用属性

1、parameterType 参数数据类型

  • (1) 基本数据类型

通过 id 查询 User。

UserRepository

//通过 id 查询 User
public User findById(int id);

UserRepository.xml

<select id="findById" parameterType="int" resultType="com.spring.entity.User">
	select * from t_user where id = #{id}
</select>
  • (2) String 类型

通过 username 查询 User。

UserRepository

//通过 username 查询 User
public User findByUsername(String username);

UserRepository.xml

<select id="findByUsername" parameterType="java.lang.String" resultType="com.spring.entity.User">
    select * from t_user where username = #{username}
</select>
  • (3) 包装类

通过 age 查询 User。

UserRepository

//通过 age 查询 User
public User findByAge(Integer age);

UserRepository.xml

<select id="findByAge" parameterType="java.lang.Integer" resultType="com.spring.entity.User">
    select * from t_user where age = #{age}
</select>
  • (4) 多个参数

通过 username 和 age 查询 User。两个参数分别是 String 类型和 int 类型,类型不一致,所以此时 parameterType 可以省略,通过参数下标取出参数值。

UserRepository

//通过username和age查询User
public User findByUsernameAndAge(String username,int age);

UserRepository.xml

<select id="findByUsernameAndAge" resultType="com.spring.entity.User">
    select * from t_user where username = #{param1} and age = #{param2}
</select>
  • (5) JavaBean

当同时存在多个参数时,通过下标一个一个写很麻烦,这时候可以将参数列表进行封装,将封装的对象作为 paramterType 的值。

UserRepository

//通过User封装对象查询User
public User findByUser(User user);

UserRepository.xml

<select id="findByUser" parameterType="com.spring.entity.User" resultType="com.spring.entity.User">
    select * from t_user where username = #{username} and age = #{age}
</select>

2、resultType:结果类型

  • (1) 基本数据类型

统计 User 总数。

UserRepository

//统计User总数
public int count();

UserRepository.xml

<select id="count" resultType="int">
	select count(*) from user
</select>
  • (2) 包装类

统计 User 总数。

UserRepository

//统计User总数
public Integer count();

UserRepository.xml

<select id="count" resultType="java.lang.Integer">
    select count(*) from t_user
</select>
  • (3) String类型

根据 id 查询 User 的 username。

UserRepository

//根据 id 查询 User 的 username
public String findNameById(int id);

UserRepository.xml

<select id="count" resultType="int">
	select count(*) from user
</select>
  • (4) JavaBean

通过 id 查询 User

UserRepository

//根据 id 查询 User
pulbic User findById(int id);

UserRepository.xml

<select id="findById" parameterType="int" resultType="com.spring.entity.User">
	select * from t_user where id = #{id}
</select>
MyBatis 是一款使用 XML 或注解配置的持久层框架,它可以自动化地将数据库中的数据映射到 Java 对象中。在 MyBatis 中,mappermapper.xml 是配对使用的,其中 mapper 是接口,而 mapper.xml 是映射配置文件mapper 接口中定义了数据库操作的方法,而 mapper.xml 中则定义了这些方法的 SQL 语句以及参数映射规则、结果集映射规则等。 下面是一个简单的例子: 1. 定义 mapper 接口 ```java public interface UserMapper { User selectUserById(Integer id); } ``` 2. 定义 mapper.xml 映射配置文件 ```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.example.UserMapper"> <resultMap id="userResultMap" type="com.example.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 上述代码中,namespace 属性指定了 mapper 接口的全限定名,resultMap 标签定义了一个结果集映射规则,select 标签定义了一个查询操作,其中 id 属性指定了 mapper 接口中的方法名,resultMap 属性指定了结果集映射规则的 id。 3. 在 MyBatis 配置文件中引入 mapper.xml ```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> <mappers> <mapper resource="com/example/UserMapper.xml"/> </mappers> </configuration> ``` 上述代码中,mapper 标签指定了映射配置文件的位置。 这样就完成了 mappermapper.xml 的配置。在代码中调用 selectUserById 方法时,MyBatis 会根据 mapper.xml 中的配置自动生成 SQL 语句,并将查询结果映射到 User 对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值