--命名空间一般为该mapper映射文件所对应maper接口所在的路径java
<mapper namespace="com.harbsoft.com.mybatis.mapper.UserMapper">
--开启二级缓存 (实体类必须序列化)mysql
<cache type="org.mybatis.caches.ehcache.EhcacheCache" />
--抽取通用的SQLsql
<sql id="user_query_where"> 通用sql </sql>
--if数据库
<if test="id!=null and id!=''"> 一般是where条件语句 </if>
--foreach缓存
<foreach collection="ids" item="id" open="and (" close=")" separator="or"> user.id=#{id} </foreach>
--$mybatis
AND user.username LIKE '${username}%'
--#app
AND user.sex = #{sex}
--resultMap对应的是表与实体类的映射 -- type 数据库表对应的实体类,别名或完整类名均可以ui
<resultMap type="person" id="resultMapPerson"> <!-- 结果集的主键 --> --主键 <id/> <id property="userid" column="id"/> <!-- 普通的列 --> --column 是数据库中字段, property是实体类中字段 <result property="name" column="username" /> <result property="addr" column="address" /> </resultMap>
-- 一对一的关系处理(一个订单对应一个用户, 此处至关于一个类中的一个字段,该字段为一个对象)spa
<association property="user" javaType="com.harbosoft.mybatis.po.User"> <id property="id" column="user_id"/> <result property="username" column="username"/> <result property="sex" column="sex"/> <result property="address" column="address"/> </association>
--一对多的关系处理(一个用户有多个订单,此处至关于一个类中的一个字段,该字段为一个集合)code
<!-- 订单信息 --> <collection property="orders" ofType="com.harbosoft.mybatis.po.Orders"> <!-- 订单号 --> <result property="order_number" column="order_number" /> <result property="id" column="id" /> </collection>
三者能够嵌套使用
一个用户--------多个订单-------多个订单明细
一个用户--------多个订单-------多个订单明细--------多个商品
--select
public User findUserById(int id)
<select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM USER WHERE id=#{id} <!-- id-------mapper接口的方法名; parameterType -------mapper接口的方法参数的类型 resultType ---------mapper接口的方法的返回值类型 user ----------是别名(全名是com.harbosoft.mybatis.Items) id 和形参保持一致 (#) --> </select>
--返回值是list user
public List<User> findUserByName(String username)
<select id="findUserByName" parameterType="string" resultType="user"> SELECT * FROM USER WHERE username like '${value}%' <!-- 该方法返回值类型为List,可是集合中装的是user,因此resultType 的值只要和集合中存储的同样便可 value 能够随意些,什么均可以 ($) --> </select>
--返回值是list 参数是user resultType
public List<User> findUserList(User user)throws Exception;
<!-- 综合查询用户信息 --> <select id="findUserList" parameterType="user" resultType="user"> SELECT * FROM USER <where> <!-- 用户的查询条件 --> <include refid="user_query_where"/> <!--该条SQL可能会重用,因此抽取出来,引用时用include--> </where> </select>
<sql id="user_query_where"> <if test="id!=null and id!=''"> AND user.id=#{id} </if> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> user.id=#{id} and (userid = </foreach> <if test="username!=null and username!=''"> AND user.username LIKE '${username}%' </if> <if test="sex!=null and sex!=''"> AND user.sex = #{sex} </if> </sql>
--返回值是List resultMap
public List<Person> findUserListResultMap(User user)throws Exception;
<!-- 综合查询用户信息 使用resultMap--> <select id="findUserListResultMap" parameterType="user" resultMap="resultMapPerson"> SELECT * FROM USER WHERE username like '${username}%' and sex=#{sex} </select>
--参数是map hashmap resultType
public List<User> findUserListByHashmap(Map map)throws Exception;
<!-- 经过hashmap查询用户信息 --> <select id="findUserListByHashmap" parameterType="hashmap" resultType="user"> SELECT * FROM USER WHERE username like '${name}%' and sex=#{sex} </select>
--返回值是map resultType
public Map findUserByIdReturnMap(int id) throws Exception;
<!-- 获取单个用户信息返回hashmap --> <select id="findUserByIdReturnMap" parameterType="int" resultType="hashmap"> SELECT * FROM USER WHERE id=#{id} </select>
--insert
public void insertUser(User user) throws Exception;
<insert id="insertUser" parameterType="user"> <!-- keyProperty:指定主键映射的pojo对象的属性 order:selectKey的执行顺序,mysql这里设置为after 企业中实际使用时,主键一般使用uuid()即 SELECT UUID() --> <selectKey keyProperty="id" order="AFTER" resultType="int"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO USER(username,birthday,sex,address,detail,score) VALUES(#{username},#{birthday},#{sex},#{address},#{detail},#{score}) </insert>
--update
public void updateUserById(User user) throws Exception;
<update id="updateUserById" parameterType="com.harbsoft.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address},detail=#{detail},score=#{score} where id=#{id} </update>
--delete
public void deleteUserById(int id) throws Exception;
<delete id="deleteUserById" parameterType="java.lang.Integer"> delete from user where id=#{value} </delete>