cache:命名空间的二级缓存配置
cache-ref:其他命名空间缓存配置的引用
resultMap:自定义结果映射集
parameterMap:已废弃,老式风格的参数映射
sql:抽取可重用的语句块
insert:映射插入语句
update:映射更新语句
delete:映射删除语句
select:映射查询语句
- id:对应接口的方法名
- parameterType:传入的参数类型
- 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.dao.CustomerDao">
<!--public List<Customer> findAll();-->
<select id="findAll" parameterType="com.domain.Customer" resultType="com.domain.Customer">
select * from customer
</select>
<!--public List<Customer> findOne(int page,int num);-->
<select id="findOne" resultType="com.domain.Customer">
select * from customer
limit #{page},#{num}
</select>
<!--public Customer findCustomerByName(String name);-->
<select id="findCustomerByName" resultType="com.domain.Customer">
select * from customer where cust_name=#{name}
</select>
<!--public Customer findCustomerById(Integer id);-->
<select id="findCustomerById" resultType="com.domain.Customer">
select * from customer where cust_id=#{id}
</select>
<!--public Long insertCustomer(Customer customer);-->
<insert id="insertCustomer" parameterType="com.domain.Customer" useGeneratedKey="true" keyProperty="id" >
insert into customer(
cust_name,
cust_linkman,
cust_phone,
cust_mobile,
cust_zipcode,
cust_address
)
values(
#{cust_name},
#{cust_linkman},
#{cust_phone},
#{cust_mobile},
#{cust_zipcode},
#{cust_address}
)
</insert>
<!--public Boolean updateCustomerById(Customer customer);-->
<update id="updateCustomerById" parameterType="com.domain.Customer" >
update customer
set
cust_name=#{cust_name},
cust_linkman=#{cust_linkman},
cust_phone=#{cust_phone},
cust_mobile=#{cust_mobile},
cust_zipcode=#{cust_zipcode},
cust_address=#{cust_address}
where cust_id=#{cust_id}
</update>
<!--public void deleteCustomerById(Integer id);-->
<delete id="deleteCustomerById">
delete from customer
where cust_id=#{id}
</delete>
</mapper>
mybatis 允许增删改直接定义以下返回值类型
Integer、Long、Boolean
mysql 支持自增主键,自增主键值的获取,MyBatista也是利用 statement.getGenreatedKey()
useGeneratedKeys="true";使用自增主键获取主键值策略
keyProperty:指定对应的主键属性,也是mybatis获取到主键值以后,将这个值封装给 javaBean 的哪个属性
oracle不支持自增主键
keyProperty:查出的主键值封装给javaBean的哪个属性
order="BEFORE" :当前sql在插入之前运行,AFTER:当前sql在插入之后运行
BEFORE运行顺序:先运行selectKey查询 id 的 sql ,查出 id 值封装给 javaBean 的 id 属性,在运行插入的 sql ,就可以取出 id 属性对应的值
<insert id="insertUser" >
<selectKey keyProperty="id" order="BEFORE" resultType="Integer" >
select * from dual
</selectKey>
insert into user(id,name,email) values(#{id},#{name},#{email})
</insert>