<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""../mybatis-3-mapper.dtd"><!-- 指定当前mapper文件需要实现的接口 --><mappernamespace="org.cosolas.cn.dao.BankDao"><!-- 1,多个参数传入的时候,在接口的参数类型前边加@Param("name")在配置文件中直接用@{name}直接可以取出 --><!-- 2,进行模糊查询,在sql语句中写: '%'||#{name}||'%' ,进行字符串的拼接--><!-- 3,配置文件中的大于小于号,用 > < 在执行sql语句的时候. 或者: <![CDATA[>]]>表示大于号,表示普通文本,脚本语言处理特殊符号的通用解决方案 --><!-- 4,对象中的字段和实体的字段不一致,sql语句起别名或者用mybatis起别名 --><!-- 接口里的查询 :方法,参数类型,返回类型--><selectid="queryBankById"parameterType="Integer"resultType="org.cosolas.cn.entity.Bank">
select * from bank where id = #{id}
</select><!-- 查询的集合 --><selectid="queryAll"resultType="org.cosolas.cn.entity.Bank">
select * from bank
</select><!-- 添加 --><insertid="insertBank"parameterType="org.cosolas.cn.entity.Bank"><!-- 给参数属性赋值, --><selectKeyresultType="java.lang.Integer"keyProperty="id"order="BEFORE">
select s_bank.nextval from dual
</selectKey>
insert into bank values(#{id}, #{name}, #{balance}, #{day})
</insert><updateid="updataBank"parameterType="org.cosolas.cn.entity.Bank">
update bank set name = #{name} where id = #{id}
</update><deleteid="dateBank"parameterType="Integer">
delete * from bank where id = #{id}
</delete></mapper>
StuDao.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="org.cosolas.cn.testdao.StuDao"><!-- 插入过程中的null值, #{属性名,jdbcType = DATE | VARCHAR NUMERIC } --><!-- 1,多个参数进行处理,多个参数会被封装到一个map中,
通过key进行取值,#{0},#{param1}...
2,在参数之前用@Param("id"),给map指定键,取的时候就用键
//尽量使用数据模型中的数据
3,通过直接传输对象进行传输, #{对象的属性}
//参数多,不是业务模型的数据,不经常使用,用map
4,通过map集合进行传输 值是对象#{对象的属性}
//经常使用的自定义实体,用来传输数据
5,定义一个数据传输对象,
6,传入的参数是list有特殊处理取值 #{list[0]}
--><!-- mybatis自增主键,利用statement.getGenreateKeys();
添加的时候不需要关注主键,添加的时候返回主键,添加给对应的属性就可以
<insert id="insertStu" parameterType="org.cosolas.cn.entity.T_Stu"" useGeneratedKeys="true"
keyProperty = "id">
--><!-- oracle的主键自增策略 --><insertid="insertStu"parameterType="org.cosolas.cn.entity.T_Stu"><selectKeyresultType="Integer"keyProperty="id"order="BEFORE">
select seq_t_stu.nextval from dual
</selectKey>
insert into t_stu values(#{id},#{name},#{age},#{address.id})
</insert><!-- 复杂的结果查询 ,定义一个手册说明复杂类型的封装过程--><resultMaptype="org.cosolas.cn.entity.T_Stu"id="T_Stu01"><!-- 主属性 --><idcolumn="id"property="id"/><!-- 业务属性 --><resultcolumn="name"property="name"/><resultcolumn="age"property="age"/><!-- 关系属性 --><associationproperty="address"javaType="org.cosolas.cn.entity.T_Address"><resultcolumn="aid"property="id"/><resultcolumn="shen"property="shen"/><resultcolumn="shi"property="shi"/><resultcolumn="xian"property="xian"/><resultcolumn="cunzheng"property="cunzheng"/></association></resultMap><!-- 连表查询,引入查询的手册 --><selectid="queryAll"resultMap="T_Stu01">
select s.*,a.id aid,a.shen,a.shi,a.xian,a.cunzheng from t_stu s left join t_address a on s.t_address = a.id
</select></mapper>
BankDaos[使用注解的类]
packageorg.cosolas.cn.dao.impl;importjava.util.List;importorg.apache.ibatis.annotations.Select;importorg.cosolas.cn.entity.Bank;publicinterfaceBankDaos{@Select("select * from bank where id = #{id} ")publicBankqueryBankById(Integer id);}