Mybatis

mybatis开发流程
1导入jar包mybatis-3.2.3.jar
mysql-connector-java-5.1.28-bin.jar
2mapper.xml映射文件
<?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="namespace">//namespace的名字可以任意取
<resultMap type="li.shi.lin.Student" id="studentMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sal" property="sal"/>
</resultMap>
<insert id="add">
insert into students(id,name,sal) values(3,"哈哈",7000)
</insert>
<insert id="add" paramType="li.shi.lin.Student">
insert into students(id,name,sal) values(3,"哈哈",7000)
</insert>
<select id="select" parameterType="li.shi.lin.Student" resultType="li.shi.lin.Student">注意查询需要有返回值类型
select * from students where id=(#{id})
</select>
</mapper>
3数据库的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
http://mybatis.org/dtd/mybatis-3-config.dtd>
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///batis_demo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> 
<mappers>
<mapper resource="li/shi/lin/StudentMapper.xml"/>
</mappers>
</configuration>
动态SQL
查询<select id="select" parameterType="li.shi.lin.Student" resultType="li.shi.lin.Student">注意查询需要有返回值类型
select * from students where id=(#{id})
<where>
<if test="pid!=null">
and id=#{pid}
</if>
</where>
</select>
更新<update id="select" parameterType="li.shi.lin.Student">注意查询需要有返回值类型
update students
<set>
<if test="pid!=null">
 id=#{pid},
</if>
<if test="pname!=null">
 name=#{pname}
</if>
</set>
</update>
插入<delelte id="dynaDelete" parameterType="array">
delete from students where id in
<foreach collection="array" open="(" close=")" separator="," item="ids"
#(ids)
</foreach>
</delete>原语句:delete from students where id in (1,2,3)
删除
原语句:insert into students(key) values(value)
<sql id="key">
<if test="id!=null">
id,
</if>
<if test="name!=null">
name,
</if>
<if test="sal!=null">
sal
</if>
</sql>
<sql id="value">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal}
</if>
</sql>
<insert id="dynamicInsert" parameterType="li.shi.lin.Student">
insert into students(<include refid="key"/>) values(<include refid="value"/>)
</insert>
比较:
jdbc优点:学习快,灵活,效率高
缺点:代码繁琐,难以写出高质量的代码,SQL注入安全性,开发者既要业务逻辑,又要写对象的创建和销毁,管理底层具体数据库的语法
使用范围:适合于超大批量数据的操作,速度快
hibernate优点:不用写SQL,完全以面向对象的方式设计和访问,不用管底层数据库的语法
缺点:处理复杂业务时,灵活度差,复杂的HQL很难写理解
使用范围:适合中小批量的数据的操作,速度慢
mybatis优点:结合jdbc和hibernate的优点
既有sql语句的书写,又有映射
连接查询:select * from customers c,orders c where c.id=o.id
只能查询出连接字段相同的记录
select * from orders o right outer join customers c group by c.name
根据一方,将另外不符合相同记录强行查询出来
inner join将一张表通过别名的方式看做多张表,进行连接
outer join
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值