mybatis开发流程 | |||||||||||||
1 | 导入jar包 | mybatis-3.2.3.jar | |||||||||||
mysql-connector-java-5.1.28-bin.jar | |||||||||||||
2 | mapper.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 |
Mybatis
最新推荐文章于 2024-06-12 21:52:13 发布