mybatis sql 语句使用
时间: 2023-09-25 17:16:24 浏览: 104
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。下面是 MyBatis 中常用的 SQL 语句使用示例:
1. 查询语句
单表查询:
```xml
<select id="selectUserById" resultType="com.example.User">
select * from user where id = #{id}
</select>
```
多表查询:
```xml
<select id="selectUserAndOrderById" resultMap="userResultMap">
select u.*, o.order_no, o.total_price
from user u
left join order o on u.id = o.user_id
where u.id = #{id}
</select>
<resultMap id="userResultMap" type="com.example.User">
<id property="id" column="id" />
<result property="username" column="username" />
<collection property="orders" ofType="com.example.Order">
<result property="orderNo" column="order_no" />
<result property="totalPrice" column="total_price" />
</collection>
</resultMap>
```
2. 插入语句
```xml
<insert id="insertUser" parameterType="com.example.User">
insert into user (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
```
3. 更新语句
```xml
<update id="updateUser" parameterType="com.example.User">
update user set username = #{username}, password = #{password}
where id = #{id}
</update>
```
4. 删除语句
```xml
<delete id="deleteUserById" parameterType="int">
delete from user where id = #{id}
</delete>
```
以上只是 MyBatis 中 SQL 语句的示例,实际应用中可能需要更复杂的 SQL 语句,需要根据具体情况进行编写。
阅读全文