1 MyBatis简介
MyBatis 是一个优秀的基于 Java 的持久层框架,它内部封装了 Jdbc,使开发者只需
要关注 SQL 语句本身,而不需要花费精力去处理加载驱动、创建连接、创建 statement 等
繁杂的过程。
MyBatis 通过 Xml 或注解的方式将要执行的各种 statement 配置起来,并通过 Java 对
象和 statement 中 SQL 的动态参数进行映射生成最终执行的 SQL 语句,最后由 MyBatis 框
架执行 SQL 并将结果映射为 Java 对象并返回。
MyBatis 具体操作
创建一个需要操作的实体类 如(Student)类,必须给属性添加set和get方法,因为MyBatis内部通过set注入的方式来操作类
(先导包)
一 创建需要操作的对象 如(Student)
package com.ljs.pojo;
import java.io.Serializable;
public class Student implements Serializable{
private Integer id;
private String name;
public Student() {
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
二 创建 MyBatis 的核心配置 (mybatis-config.xml)
<?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>
<引入数据库配置文件 db.properties>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<映射到指定的xml文件中,让其有操作数据库的能力>
<mapper resource="com/ljs/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
相对应的 db.properties 文件内容
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/day01
jdbc.username=root
jdbc.password=root
三 创建相对应的StudentMapper 接口 里面为操作数据库的方法
package com.ljs.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.ljs.pojo.Student;
public interface StudentMapper {
Student queryStu(int id);
Student queryName(String name);
//通过类对象
Student queryLike1(Student stu);
//通过参数索引
Student queryLike2(String name,int id);
//通过注解
Student queryLike3(@Param("name")String name,@Param("id")int id);
//多参处理方法map
List<Student> queryLike4(Map params);
// 增
void savaStu(Student stu);
//删
void delStu(int id);
//改
void updateStu(String name,int id);
//Mybatis动态SQL
Student queryLikeMove(String name,int id);
//批量查找操作(根据id查询)
List<Student> queryStus(List<Integer> list);
//批量添加
void saveStus(List<Student> list);
}
四 创建相对应的 StudentMapper.xml 文件 (具体的操作数据库的SQL语句)
<?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="com.ljs.mapper.StudentMapper">
<!-- 查询中*可以统一用此方法代替 -->
<sql id="allParams">
id,name
</sql>
<select id="queryStu" parameterType="int" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where id = #{id}
</select>
<select id="queryName" parameterType="String" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where name = #{name}
</select>
<!-- 类对象查询 -->
<select id="queryLike1" parameterType="com.ljs.pojo.Student" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where name like #{name} and id = #{id}
</select>
<!-- 参数索引查询 -->
<select id="queryLike2" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where name like #{arg0} and id = #{arg1}
</select>
<!-- 注解查询 -->
<select id="queryLike3" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where name like #{name} and id = #{id}
</select>
<!-- Map查询 -->
<select id="queryLike4" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include> from student where name like #{name} and id = #{id}
</select>
<!-- 增 -->
<insert id="savaStu" parameterType="com.ljs.pojo.Student">
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID();
</selectKey>
insert into student values(#{id},#{name})
</insert>
<!-- 删 -->
<delete id="delStu">
delete from student where id = #{id}
</delete>
<!-- 改 -->
<update id="updateStu">
update student set name = #{arg0} where id = #{arg1}
</update>
<!-- 动态SQL查询 -->
<select id="queryLikeMove" resultType="com.ljs.pojo.Student">
select <include refid="allParams"></include>
from student k
<where>
<if test="name != null">name = #{name}</if>
<if test="id != null">id = #{id}</if>
</where>
</select>
<!-- 批量查找操作(根据id查询) -->
<select id="queryStus" parameterType="java.util.List" resultType="com.ljs.pojo.Student">
insert into student values in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
<!-- 批量添加 -->
<insert id="saveStus" parameterType="java.util.List">
insert into student values
<foreach collection="list" separator="," item="stu">
(#{stu.id},#{stu.name})
</foreach>
</insert>
</mapper>
总结
一 传参方式
MyBatis 传参方式
一 根据类对象来传递参数
二 通过参数索引来传递对象
SQL 语句里面可以用 arg0,arg1 来占当做位
也 可用 pram1 和pram2 来充当占位符
三 多参时 可用List 和 map
二 多表联合查询
三种 方式 (一对多)
1 直接引用
2 从外部引用 resultmap = “引用地址”
<resultMap type="person" id="pmap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="address" column="address"/>
<association property="cart" javaType="cart" column="id" resultMap="com.ujiuye.mapper.CartMapper.cmap">
</association>
</resultMap>