mapper.xml配置文件详解
2015-11-09 00:00
239 查看
--命名空间通常为该mapper映射文件所对应maper接口所在的路径
<mapper namespace="com.harbsoft.com.mybatis.mapper.UserMapper">--开启二级缓存 (实体类必须序列化)
<cache type="org.mybatis.caches.ehcache.EhcacheCache" />--抽取通用的SQL
<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>--$
AND user.username LIKE '${username}%'--#
AND user.sex = #{sex}--resultMap对应的是表与实体类的映射 -- type 数据库表对应的实体类,别名或完整类名都可以
<resultMap type="person" id="resultMapPerson"> <!-- 结果集的主键 --> --主键 <id/> <id property="userid" column="id"/> <!-- 普通的列 --> --column 是数据库中字段, property是实体类中字段 <result property="name" column="username" /> <result property="addr" column="address" /> </resultMap>-- 一对一的关系处理(一个订单对应一个用户, 此处相当于一个类中的一个字段,该字段为一个对象)
<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>--一对多的关系处理(一个用户有多个订单,此处相当于一个类中的一个字段,该字段为一个集合)
<!-- 订单信息 --> <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>
相关文章推荐
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Java简单实现SpringMVC+MyBatis分页插件
- 浅析mybatis和spring整合的实现过程
- mybatis的动态sql详解(精)
- MyBatis入门学习教程(一)-MyBatis快速入门
- Spring与Mybatis的整合方法有哪些
- 解决springmvc+mybatis+mysql中文乱码问题
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- SpringMVC+MyBatis项目总结(一)
- SpringMVC+MyBatis项目总结(二)
- MyBatis返回结果不稳定
- MyBatis通过反射建立一个对象的过程。
- springMVC+spring+Mybatis
- SpringMVC+mybatis+Spring框架整合+简单实现的demo
- 笔记:学习JavaWeb开发第三课
- Spring+mybatis+shiro+freemarker+ehcache+ldap+mongo
- 继续钟爱JdbcTemplate