Open hsipeng opened 7 years ago
最简单的javabean ,用来传递数据
mybatis中的标签没有实际的约束条件,但最好标签对应相应语句的内容。
在默认条件下,在使用mybatis时,列名保持一致,否则无法赋值
使用列名时使用${map中的key}
SqlSession sqlSession = sqlSessionFactory.openSession();
Map<String, String> uMap = new HashMap<String,String>();
uMap.put("age", "age");
List<User> uList= sqlSession.selectList("bean.UserMapper.findUserByAge",uMap);
for (User user : uList) {
System.out.println(user);
}
mybatis默认传一个参数,多个参数时,封装为单值。
DATA区
select * from user where age >#{minAge} and <![CDATA[age <#{maxAge}]]>;
<update id="updateUser" parameterType="bean.User">
update user
<set>
<if test="name!=null">name=#{name},</if>
<if test="age!=0">age=#{age},</if>
<if test="sex!=null">sex=#{sex}</if>
</set> where id=#{id};
</update>
<where>
<if test="id!=0">id=#{id}</if>
<if test="name!=null">name=#{name}</if>
<if test="age!=0">age=#{age}</if>
<if test="sex!=null">sex=#{sex}</if>
</where>
<sql id="selectUser">
select * from user
</sql>
<!-- 复用 -->
<include refid="selectUser"/>
<typeAliases>
<typeAlias type="bean.User" alias="User"/>
</typeAliases>
<!--
批量删除
集合操作
array 数组
list
map 中的key遍历的是value值
open "("
item 表示当前的遍历变量名
separator 以“,”分隔
close ")"
-->
<delete id="delUsers">
delete from user where id in
<foreach collection="array" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</delete>
sql语句不要写分号 转义字符<![CDATA[age<#{maxAge}]]>
首先找到当前接口的路径,然后与namespace做匹配,匹配成功,根据接口方法的名称,匹配sql的ID,然后执行sql语句。
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<mapper namespace="mapper.UserMapper">
<cache/>
<select id="findAll" resultType="bean.User">
select * from user
</select>
</mapper>
public class User implements Serializable{
private static final long serialVersionUID = 5048168053828815152L;
sqlMapGeneractor.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<properties resource="generatorConfig.properties"/>
<classPathEntry location="D:\project\Mybatis-day02-2\lib\mysql-connector-java-5.1.10-bin.jar" />
<context id="tarena">
<commentGenerator>
<property name="suppressAllComments" value="${suppressAllComments}"/>
</commentGenerator>
<jdbcConnection driverClass="${driverClass}" connectionURL="${url}" userId="${username}" password="${password}" />
<javaModelGenerator targetPackage="${modeltargetPackage}" targetProject="${targetProject}" />
<sqlMapGenerator targetPackage="${sqltargetPackage}" targetProject="${targetProject}" />
<javaClientGenerator targetPackage="${clienttargetPackage}" targetProject="${targetProject}" type="XMLMAPPER" />
<table schema="" tableName="user" />
</context>
</generatorConfiguration>
generatorConfig.properties
suppressAllComments=true
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
username=root
password=123456
targetProject=Mybatis-day02-2
modeltargetPackage=cn.buttonz.pojo
sqltargetPackage=cn.buttonz.pojo
clienttargetPackage=cn.buttonz.mapper
<select id="findAll_U" resultMap="userRM">
select id as user_id,name as user_name,age as user_age,sex as user_sex from user
</select>
<!-- 自定义结果集封装 -->
<resultMap type="bean.User" id="userRM">
<!-- 主键id,不能省略-->
<id column="user_id" property="id"/>
<result column="user_name" property="name"/>
<result column="user_age" property="age"/>
<result column="user_sex" property="sex"/>
</resultMap>
一对一
<resultMap type="bean.User" id="userRMOneToOne" autoMapping="true">
<!-- 主键id,不能省略-->
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="dept" javaType="bean.Dept">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<select id="oneToOne" resultType="bean.User" resultMap="userRMOneToOne">
SELECT u.id,u.name,u.age,u.sex,d.dept_id,d.dept_name FROM
(SELECT * FROM user) u
LEFT JOIN (SELECT * FROM dept) d
on u.dept_id=d.dept_id
</select>
一对多
<select id="oneToMore" resultMap="deptRM">
select * from
(select dept_id as d_id,dept_name from dept) d
LEFT JOIN
(select * from user) u
on d.d_id=u.dept_id
</select>
<!-- 自定义结果集封装 -->
<resultMap type="bean.Dept" id="deptRM" autoMapping="true">
<!-- 主键id,不能省略-->
<id column="d_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<collection property="uList" ofType="bean.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
多对多
<!-- 查询用户即购买的商品信息的ResultMap -->
<resultMap type="com.mybatis.entity.User" id="userAndItemsResultMap">
<!-- 用户信息 -->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<!-- 订单信息
一个用户对应多个订单,使用collection映射 -->
<collection property="ordersList" ofType="com.mybatis.entity.Orders">
<id column="id" property="id"/>
<result column="user_id" property="userid"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<!-- 订单明细
一个订单包括 多个明细
-->
<collection property="orderdetails" ofType="com.mybatis.entity.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
<!-- 商品信息
一个订单明细对应一个商品
-->
<association property="items" javaType="com.mybatis.entity.Items">
<id column="items_id" property="id"/>
<result column="items_name" property="itemsName"/>
<result column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<!-- 查询用户及用户购买的商品信息,使用resulaMap-->
<select id="findUserAndItemsResultMap" resultMap="userAndItemsResultMap">
SELECT
t1.*,
t2.username,
t2.sex,
t2.address,
t3.id orderdetail_id,
t3.items_id,
t3.items_num,
t3.orders_id,
t4.itemsname items_name,
t4.detail items_detail,
t4.price items_price
FROM
orders t1,
t_user t2,
orderdetail t3,
items t4
WHERE t1.user_id = t2.id AND t3.orders_id=t1.id AND t3.items_id = t4.id
</select>
jdbc的缺点
mybatise是一个持久层框架
自动对象关系映射,使用SQL直接操作数据库 mybatis是半自动的ORM(对象关系映射)
Mybatis组件