MyBatis

MyBatis入门案例

MyBatis入门案例

自定义Mybatis

在这里插入图片描述

  1. src/main/java/com/dao
package com.dao;

import com.domain.User;
import com.mybatis.annotation.Select;

import java.util.List;

public interface IUserDao {
    /**
     * 查询所有操作
     * @return
     */

    @Select("select * from user")
    List<User> findAll();
}
  1. src/main/java/com/domain
package com.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

  1. src/main/java/com/mybatis/annotation
package com.mybatis.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//查询的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Select {
    //配置的SQL语句
    String value();
}

  1. src/main/java/com/mybatis/cfg
package com.mybatis.cfg;
import java.util.HashMap;
import java.util.Map;

//自定义mybatis的配置类
public class Configuration {
    private String driver;
    private String url;
    private String username;
    private String password;
    private Map<String,Mapper> mappers = new HashMap<String, Mapper>();

    public Map<String, Mapper> getMappers() {
        return mappers;
    }

    public void setMappers(Map<String, Mapper> mappers) {
        this.mappers.putAll(mappers); //此处需要使用追加的方式
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

package com.mybatis.cfg;


//用于封装执行的SQL语句和结果类型的权限定类名
public class Mapper {
    private String queryString; // SQl
    private String resultType; //全限定类名

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    public String getResultType() {
        return resultType;
    }

    public void setResultType(String resultType) {
        this.resultType = resultType;
    }
}

  1. src/main/java/com/mybatis/io
package com.mybatis.io;
import java.io.InputStream;
//使用类加载器读取配置文件的类
public class Resources {

    //根据传入的参数,获取一个字节输入流
    public static InputStream getResourceAsStream(String filePath){
        return Resources.class.getClassLoader().getResourceAsStream(filePath);
    }
}

  1. src/main/java/com/mybatis/sqlsession/defaults
package com.mybatis.sqlsession.defaults;

import com.mybatis.cfg.Configuration;
import com.mybatis.sqlsession.SqlSession;
import com.mybatis.sqlsession.proxy.MapperProxy;
import com.mybatis.utils.DataSourceUtils;

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

//SqlSession接口的实现
public class DefaultSqlSession implements SqlSession{

    private Configuration cfg;
    private Connection conn;

    public DefaultSqlSession(Configuration cfg) {
        this.cfg = cfg;
        conn = DataSourceUtils.getConnection(cfg);
    }

    //用于创建代理对象
    public <T> T getMapper(Class<T> daoInterfaceClass) {
        return (T) Proxy.newProxyInstance(daoInterfaceClass.getClassLoader(),
                    new Class[]{daoInterfaceClass},new MapperProxy(cfg.getMappers(), conn)
                );

    }

    //用于关闭资源
    public void close() {
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

package com.mybatis.sqlsession.defaults;

import com.mybatis.cfg.Configuration;
import com.mybatis.sqlsession.SqlSession;
import com.mybatis.sqlsession.SqlSessionFactory;

//SQLSessionFactory接口的实现类
public class DefaultSqlSessionFactory implements SqlSessionFactory{

    private Configuration cfg;

    public DefaultSqlSessionFactory(Configuration cfg) {
        this.cfg = cfg;
    }

    //用于创建一个新的操作数据库的对象
    public SqlSession openSession() {
        return new DefaultSqlSession(cfg);
    }
}

  1. src/main/java/com/mybatis/sqlsession/proxy
package com.mybatis.sqlsession;

/*
    自定义Mybatis中和数据库交互的核心类
    它里面可以创建dao接口的代理类
 */
public interface SqlSession {
    /*
        根据参数创建一个代理对象
            参数就是dao接口字节码
     */
    <T> T getMapper(Class<T> daoInterfaceClass);

    //释放资源
    void close();
}

package com.mybatis.sqlsession;

public interface SqlSessionFactory {

    //用于打开一个新的session对象
    public SqlSession openSession();
}

package com.mybatis.sqlsession;

import com.mybatis.cfg.Configuration;
import com.mybatis.sqlsession.defaults.DefaultSqlSessionFactory;
import com.mybatis.utils.XMLConfigBuilder;

import java.io.InputStream;

//用于创建SqlSessionFactory对象
public class SqlSessionFactoryBuilder {

    /*
        根据参数的输入流来构建一个SqlSessionFactory对象
     */
    public SqlSessionFactory build(InputStream config){
        Configuration cfg = XMLConfigBuilder.loadConfiguration(config);

        return new DefaultSqlSessionFactory(cfg);
    }
}

  1. src/main/java/com/mybatis/utils
package com.mybatis.utils;

import com.mybatis.cfg.Configuration;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

//用于创建数据源的工具类
public class DataSourceUtils {
    //用于获取一个连接
    public static Connection getConnection(Configuration cfg){
        try {
            Class.forName(cfg.getDriver());
            return DriverManager.getConnection(cfg.getUrl(),cfg.getUsername(),cfg.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}

package com.mybatis.utils;

import com.mybatis.cfg.Mapper;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * 负责执行SQL语句,并且封装结果集
 */
public class Executor {

    public <E> List<E> selectList(Mapper mapper, Connection conn) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            //1.取出mapper中的数据
            String queryString = mapper.getQueryString();//select * from user
            String resultType = mapper.getResultType();//com.itheima.domain.User
            Class domainClass = Class.forName(resultType);
            //2.获取PreparedStatement对象
            pstm = conn.prepareStatement(queryString);
            //3.执行SQL语句,获取结果集
            rs = pstm.executeQuery();
            //4.封装结果集
            List<E> list = new ArrayList<E>();//定义返回值
            while(rs.next()) {
                //实例化要封装的实体类对象
                E obj = (E)domainClass.newInstance();

                //取出结果集的元信息:ResultSetMetaData
                ResultSetMetaData rsmd = rs.getMetaData();
                //取出总列数
                int columnCount = rsmd.getColumnCount();
                //遍历总列数
                for (int i = 1; i <= columnCount; i++) {
                    //获取每列的名称,列名的序号是从1开始的
                    String columnName = rsmd.getColumnName(i);
                    //根据得到列名,获取每列的值
                    Object columnValue = rs.getObject(columnName);
                    //给obj赋值:使用Java内省机制(借助PropertyDescriptor实现属性的封装)
                    PropertyDescriptor pd = new PropertyDescriptor(columnName,domainClass);//要求:实体类的属性和数据库表的列名保持一种
                    //获取它的写入方法
                    Method writeMethod = pd.getWriteMethod();
                    //把获取的列的值,给对象赋值
                    writeMethod.invoke(obj,columnValue);
                }
                //把赋好值的对象加入到集合中
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            release(pstm,rs);
        }
    }


    private void release(PreparedStatement pstm,ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        if(pstm != null){
            try {
                pstm.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

package com.mybatis.utils;


import com.mybatis.annotation.Select;
import com.mybatis.cfg.Configuration;
import com.mybatis.cfg.Mapper;
import com.mybatis.io.Resources;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 *  用于解析配置文件
 */
public class XMLConfigBuilder {



    /**
     * 解析主配置文件,把里面的内容填充到DefaultSqlSession所需要的地方
     * 使用的技术:
     *      dom4j+xpath
     */
    public static Configuration loadConfiguration(InputStream config){
        try{
            //定义封装连接信息的配置对象(mybatis的配置对象)
            Configuration cfg = new Configuration();

            //1.获取SAXReader对象
            SAXReader reader = new SAXReader();
            //2.根据字节输入流获取Document对象
            Document document = reader.read(config);
            //3.获取根节点
            Element root = document.getRootElement();
            //4.使用xpath中选择指定节点的方式,获取所有property节点
            List<Element> propertyElements = root.selectNodes("//property");
            //5.遍历节点
            for(Element propertyElement : propertyElements){
                //判断节点是连接数据库的哪部分信息
                //取出name属性的值
                String name = propertyElement.attributeValue("name");
                if("driver".equals(name)){
                    //表示驱动
                    //获取property标签value属性的值
                    String driver = propertyElement.attributeValue("value");
                    cfg.setDriver(driver);
                }
                if("url".equals(name)){
                    //表示连接字符串
                    //获取property标签value属性的值
                    String url = propertyElement.attributeValue("value");
                    cfg.setUrl(url);
                }
                if("username".equals(name)){
                    //表示用户名
                    //获取property标签value属性的值
                    String username = propertyElement.attributeValue("value");
                    cfg.setUsername(username);
                }
                if("password".equals(name)){
                    //表示密码
                    //获取property标签value属性的值
                    String password = propertyElement.attributeValue("value");
                    cfg.setPassword(password);
                }
            }
            //取出mappers中的所有mapper标签,判断他们使用了resource还是class属性
            List<Element> mapperElements = root.selectNodes("//mappers/mapper");
            //遍历集合
            for(Element mapperElement : mapperElements){
                //判断mapperElement使用的是哪个属性
                Attribute attribute = mapperElement.attribute("resource");
                if(attribute != null){
                    System.out.println("使用的是XML");
                    //表示有resource属性,用的是XML
                    //取出属性的值
                    String mapperPath = attribute.getValue();//获取属性的值"com/dao/IUserDao.xml"
                    //把映射配置文件的内容获取出来,封装成一个map
                    Map<String,Mapper> mappers = loadMapperConfiguration(mapperPath);
                    //给configuration中的mappers赋值
                    cfg.setMappers(mappers);

                }else{
                    System.out.println("使用的是注解");
                    //表示没有resource属性,用的是注解
                    //获取class属性的值
                    String daoClassPath = mapperElement.attributeValue("class");
                    //根据daoClassPath获取封装的必要信息
                    Map<String,Mapper> mappers = loadMapperAnnotation(daoClassPath);
                    //给configuration中的mappers赋值
                    cfg.setMappers(mappers);
                }
            }
            //返回Configuration
            return cfg;
        }catch(Exception e){
            throw new RuntimeException(e);
        }finally{
            try {
                config.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 根据传入的参数,解析XML,并且封装到Map中
     * @param mapperPath    映射配置文件的位置
     * @return  map中包含了获取的唯一标识(key是由dao的全限定类名和方法名组成)
     *          以及执行所需的必要信息(value是一个Mapper对象,里面存放的是执行的SQL语句和要封装的实体类全限定类名)
     */
    private static Map<String,Mapper> loadMapperConfiguration(String mapperPath)throws IOException {
        InputStream in = null;
        try{
            //定义返回值对象
            Map<String,Mapper> mappers = new HashMap<String,Mapper>();
            //1.根据路径获取字节输入流
            in = Resources.getResourceAsStream(mapperPath);
            //2.根据字节输入流获取Document对象
            SAXReader reader = new SAXReader();
            Document document = reader.read(in);
            //3.获取根节点
            Element root = document.getRootElement();
            //4.获取根节点的namespace属性取值
            String namespace = root.attributeValue("namespace");//是组成map中key的部分
            //5.获取所有的select节点
            List<Element> selectElements = root.selectNodes("//select");
            //6.遍历select节点集合
            for(Element selectElement : selectElements){
                //取出id属性的值      组成map中key的部分
                String id = selectElement.attributeValue("id");
                //取出resultType属性的值  组成map中value的部分
                String resultType = selectElement.attributeValue("resultType");
                //取出文本内容            组成map中value的部分
                String queryString = selectElement.getText();
                //创建Key
                String key = namespace+"."+id;
                //创建Value
                Mapper mapper = new Mapper();
                mapper.setQueryString(queryString);
                mapper.setResultType(resultType);
                //把key和value存入mappers中
                mappers.put(key,mapper);
            }
            return mappers;
        }catch(Exception e){
            throw new RuntimeException(e);
        }finally{
            in.close();
        }
    }

    /**
     * 根据传入的参数,得到dao中所有被select注解标注的方法。
     * 根据方法名称和类名,以及方法上注解value属性的值,组成Mapper的必要信息
     * @param daoClassPath
     * @return
     */
    private static Map<String,Mapper> loadMapperAnnotation(String daoClassPath)throws Exception{
        //定义返回值对象
        Map<String,Mapper> mappers = new HashMap<String, Mapper>();

        //1.得到dao接口的字节码对象
        Class daoClass = Class.forName(daoClassPath);
        //2.得到dao接口中的方法数组
        Method[] methods = daoClass.getMethods();
        //3.遍历Method数组
        for(Method method : methods){
            //取出每一个方法,判断是否有select注解
            boolean isAnnotated = method.isAnnotationPresent(Select.class);
            if(isAnnotated){
                //创建Mapper对象
                Mapper mapper = new Mapper();
                //取出注解的value属性值
                Select selectAnno = method.getAnnotation(Select.class);
                String queryString = selectAnno.value();
                mapper.setQueryString(queryString);
                //获取当前方法的返回值,还要求必须带有泛型信息
                Type type = method.getGenericReturnType();//List<User>
                //判断type是不是参数化的类型
                if(type instanceof ParameterizedType){
                    //强转
                    ParameterizedType ptype = (ParameterizedType)type;
                    //得到参数化类型中的实际类型参数
                    Type[] types = ptype.getActualTypeArguments();
                    //取出第一个
                    Class domainClass = (Class)types[0];
                    //获取domainClass的类名
                    String resultType = domainClass.getName();
                    //给Mapper赋值
                    mapper.setResultType(resultType);
                }
                //组装key的信息
                //获取方法的名称
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String key = className+"."+methodName;
                //给map赋值
                mappers.put(key,mapper);
            }
        }
        return mappers;
    }
}

注解的入门了解

基于代理Dao实现CRUD操作
  • 根据ID查询用户
    • 在UserDao.xml中的配置
<!--根据id查询用户-->
    <select id="findById" parameterType="java.lang.Integer" resultType="com.domain.User">
        select * from user where id=#{id};
    </select>
  • 注意点:
    1. resultType属性:用于指定结果集的类型
    2. parameterType属性:用于指定传入参数的类型
    3. sql语句中使用#{}字符:它代表占位符,相当于原来jdbc部分所学的?占位符,都是用与执行语句时替换实际的数据。具体的数据是由#{}里面的内容决定
    4. #{}中内容的写法:由于在此案例中数据类型是基本数据类型,所以此处可以随意写
  • 保存操作
<!--保存用户-->
    <insert id="saveUser" parameterType="com.domain.User">
        insert into user(username,address,sex,birthday)values(#{userName},#{userAddress},#{userSex},#{userBirthday});
    </insert>
  • 注意点:
    1. parameterType属性:代表参数的类型,由于我们要传入的是一个类的对象,所以类型就要写类的全名称。sql语句中使用#{}字符
    2. sql语句中使用#{}字符:同上
    3. #{}中内容的写法:由于我们保存方法的参数是一个User对象,此处要写User对象中的属性名称。用的是ognl表达式
    4. (Object Graphic Navigation Language)表达式:语法格式:#{对象.对象}
    • 例:#{user.username}会先去找user对象,然后在user对象中找到username属性,并调用getUsername()方法把值取出来。由于我们在paratmterType属性上制定了实体类名称,所以可以省略user,直接写username,增删改的测试类中,要使用session.commit();来实现事务提交
  • 实现新增用户id的返回值:配置 selectKey标签
<!--保存用户-->
    <insert id="saveUser" parameterType="com.domain.User">
        <!--配置插入操作后,获取插入数据的id-->
        <selectKey keyProperty="userId" keyColumn="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(username,address,sex,birthday)values(#{userName},#{userAddress},#{userSex},#{userBirthday});
    </insert>
  • 用户更新
<!--更新用户-->
    <update id="updateUser" parameterType="com.domain.User">
        UPDATE user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id};
    </update>
  • 用户删除
<!--删除用户-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        DELETE from user WHERE id=#{id};
    </delete>
  • 模糊查询
  1. 使用#{}占位符
 <!--根据名字模糊查询用户-->
    <select id="findByName" parameterType="java.lang.String" resultType="com.domain.User">
        select * from user where username like #{name};
    </select>
  1. 使用${value}
 <!--根据名字模糊查询用户-->
    <select id="findByName" parameterType="java.lang.String" resultType="com.domain.User">
        select * from user where username like '%${value}%'
    </select>
  • 注意:方式二中的${value}的写法是固定的,不能写成其他名字,这种写法在代码中就不需要加入模糊查询的匹配符%了
    #{} 和 ${}的区别
    • #{}表示一个占位符号:通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入。#{}可以接受简单类型值或pojo属性值。如果parameterType传输单个简单类型值,#{}括号中介意是value或其他名称
    • **${}表示拼接sql串:**通过 ${}可以将parameterType传入的内容拼接在sql中且不进行jdbc类型转换, ${}可以接受简单类型值或pojo属性值,如果parameterType传输单个简单类型值, ${}括号中只能是value
  • 使用聚合函数查询
 <!--查询总用户数-->
    <select id="findTotal" resultType="java.lang.Integer">
        select count(*) from user;
    </select>
Mybatis编程与JDBC编程的比较
  • 数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库连接池可以解决
    • 解决:在SqlMapConfig.xml中配置数据连接池,使用连接池管理数据库连接
  • Sql语句写在代码中造成代码不易维护,实际应用sql变化的可能较大,sql变动需要改变java代码
    • 解决:将Sql语句配置在XXXmapper.xml文件中与java代码分离
  • 向sql中传参数麻烦,因为sql语句的where条件不一定,可能对也可能少,占位符需要和参数对应
    • 解决:Mybatis自动将java对象映射至sql语句,通过statement中的parameterType定义输入参数的类型
  • 对结果集合解析麻烦,sql变化导致解析代码变化,且解析前需要遍历,如果能将数据库级联封装成pojo对象解析比较仿版
    • 解决:Mybatis自动将sql执行结果映射至java对象,通过statement中的resultType定义输出结果的类型
Mybatis的参数深入
  • 传递pojo包装对象:开发中通过pojo传递查询条件,查询条件时综合的查询条件,不仅包括用户查询条件还包括其他的查询条件,这时可以使用包装对象传递参数
<!--配置queryVo的条件查询用户-->
    <select id="findUserByVo" parameterType="com.domain.QueryVo" resultType="com.domain.User">
        select * from user where username like #{user.username};
    </select>
Mybatis的输出结果封装
  • resultType配置结果类型:resultType属性可以指定结果集的类型,它支持基本类型和实体类类型,,它和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须 使用全限定类名
<!--查询所有-->
    <select id="findAll" resultMap="com.domain.User">
        <!--select id userId,username userName,address userAddress,sex userSex,birthday userBirthday from user;-->
        select * from user;
    </select>
  • resultMap结果类型:resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装
<!--配置查询结果的列名和实体类的属性名的对应关系
	type属性:指定实体类的全限定类名
	id属性:给定一个唯一标识,是给查询select标签引用用的
	
	id标签:用于指定主键字段
	result标签:用于指定非主键字段
	column属性:用于指定数据库列名
	property属性:用于指定实体类属性名称
-->
    <resultMap id="userMap" type="com.domain.User">
        <!--主键字段的对应-->
        <id property="userId" column="id"></id>
        <!--非主键字段的对应-->
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>
<!--查询所有-->
    <select id="findAll" resultMap="userMap">
        <!--select id userId,username userName,address userAddress,sex userSex,birthday userBirthday from user;-->
        select * from user;
    </select>
Mybatis传统Dao层开发
  • mybatis实现Dao的传统开发方式
package com.dao.impl;

import com.dao.IUserDao;
import com.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements IUserDao{

    private SqlSessionFactory factory;

    public UserDaoImpl(SqlSessionFactory factory){
        this.factory = factory;
    }
    public List<User> findAll() {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用SqlSession中的方法,实现查询列表
        List<User> users = session.selectList("com.dao.IUserDao.findAll");
        //释放资源
        session.close();
        return users;
    }

    public void saveUser(User user) {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现保存
        session.insert("com.dao.IUserDao.saveUser",user);
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
    }

    public void updateUser(User user) {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现更新
        session.update("com.dao.IUserDao.updateUser",user);
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
    }

    public void deleteUser(Integer id) {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现删除
        session.delete("com.dao.IUserDao.deleteUser",id);
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
    }

    public User findById(Integer id) {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现根据id查找用户
        User user = (User) session.selectOne("com.dao.IUserDao.findById", id);
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
        return user;
    }

    public List<User> findByName(String name) {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现根据名字查找用户
        List<User> users = session.selectList("com.dao.IUserDao.findByName", name);
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
        return users;
    }

    public int findTotal() {
        //1.根据factory获取sqlSession对象
        SqlSession session = factory.openSession();
        //2.调用方法实现求用户的总数
        Integer total = session.selectOne("com.dao.IUserDao.findTotal");
        //3.提交事务
        session.commit();
        //4.释放资源
        session.close();
        return total;
    }
}

  • 配置内容
    • SqlMapConfig.xml中的配置内容和顺序

-properties(属性)
    —property
-settings(全局配置参数)
    —setting
-typeAliases(类型别名)
    —typeAliase
    —package
-typeHandlers(类型处理器)
-objectFactory(对象工厂)
-plugins(插件)
-environments(环境集合属性对象)
     —environment(环境子属性对象)
      —transactionManager(事务管理)
      —dataSource(数据源)
-mappers(映射器)
    —mapper
    —package

  • properties属性
    • 第一种:
	<properties >
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
	</properties>
<!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
  • 第二种:
properties文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
<!--配置properties
        可以在标签内部配置连接数据库的信息,也可以通过属性引用外部配置文件信息
        resource属性:
            用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
        url属性:
            要求按照url的写法来写地址
            URL:Uniform Resource Locator 统一资源定位符y.它是可以唯一标识一个资源的位置
            写法:
                http://localhost:8080/mybatisserver/demo1Servlet
                协议      主机    端口      URI
            URI:Uniform Resource Identifier 统一资源标识符。它是在应用中可以唯一定位一个资源的
    -->
    <properties url="file:///E:/Study/ssm/MybatisCRUD/src/main/resources/jdbcConfig.properties">
    </properties>
<!--配置连接池-->
            <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>
  • typeAliases(类型别名):自定义别名的方式
在 SqlMapConfig.xml 中配置: 
<typeAliases >  
	<!-- 单个别名定义 -->  
	<typeAlias alias="user" type="com.itheima.domain.User"/>  
	<!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->  
	<package name="com.itheima.domain"/> 
	 <package name=" 其它包 "/> 
 </typeAliases> 
  • mappers(映射器)
  1. resource属性
使用相对于类路径的资源 
如:<mapper resource="com/itheima/dao/IUserDao.xml" /> 
  1. class属性
使用 mapper 接口类路径 
如:<mapper class="com.itheima.dao.UserDao"/> 注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同,且放在同一个目录中。
  1. package属性
注册指定包下的所有 mapper 接口 
如:<package name="cn.itcast.mybatis.mapper"/> 注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同,且放在同一个目录中

Mybatis实现 CRUD 操作源码

Mybatis连接池与事务深入

mybatis连接池的分类

  1. UNPOOLED 不适用连接池的数据源
  2. POOLED 使用连接池的数据源
  3. JNDI 使用JNDI实现的数据源
 <!--配置事务-->
 <transactionManager type="JDBC"></transactionManager>
  <!--配置连接池-->
  <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>

mybatis的事务控制

  1. mybatis中事务提交方式
 @After //用于在测试方法执行之后执行
    public void destory() throws Exception{
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
        in.close();
    }
  1. mybatis自动提交事务的设置
@Before //用于在测试方法执行之前执行
    public void init() throws Exception{
        //1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SQLSessionFactory对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3.获取SqlSession对象
        sqlSession = factory.openSession(true);
        //4.获取dao的代理对象
        userDao = sqlSession.getMapper(IUserDao.class);
    }
Mybatis的动态SQL语句

<if>标签

<select id="findUserByCondition" resultMap="userMap" parameterType="com.domain.User">
     select * from user where 1=1
     <if test="userName!=null">
         and username=#{userName}
     </if>
     <if test="userSex != null">
         and sex=#{userSex}
     </if>
 </select>

<where>标签

 <select id="findUserByCondition" resultMap="userMap" parameterType="com.domain.User">
        select * from user
        <where>
            <if test="userName!=null">
                and username=#{userName}
            </if>
            <if test="userSex != null">
                and sex=#{userSex}
            </if>
        </where>
    </select>

<foreach>标签

<!--
	sql语句:select 字段 from user where id in(?,?..);
	<foreach>标签用于遍历集合,其中的属性:
		① collection:代表要遍历的集合元素,注意编写时不要写#{}
		② open:代表语句的开始部分
		③ close:代表结束部分
		④ item:代表遍历集合的每个元素,生成的变量名
		sperator:代表分隔符
-->
<!--根据queryVo中的id集合实现查询用户列表-->
    <select id="findUserInIds" resultMap="userMap" parameterType="com.domain.QueryVo">
        <include refid="defaultUser"></include>
        -- select * from user
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

简化编写的sql片段

<!--抽取重复的Sql语句-->
    <sql id="defaultUser">
        select * from user
    </sql>
    <!--查询所有-->
    <select id="findAll" resultMap="userMap">
        <!--select * from user;-->
        <include refid="defaultUser"></include>
    </select>
Mybatis多表查询

1.一对一查询(多对一)

  1. 方式一:定义一个AccountUser类并继承Account来作为最后结果的实现类
package com.domain;

public class AccountUser extends Account{

    private String username;
    private String address;

}

<!--查询所有账户,同时包含用户名和地址信息-->
    <select id="findAllAccount" resultType="com.domain.AccountUser">
        select a.*,u.username,u.address from account a,user u where u.id = a.uid;
    </select>
  1. 通过配置信息来实现一对多的操作,不用再重新创建一个类来作为结果分装
<!--定义封装account和user的resultMap-->
    <resultMap id="accountUserMap" type="com.domain.Account">
        <id property="id" column="aid"/>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>

        <!--一对一的关系映射,配置封装的user的内容-->
        <association property="user" column="uid" javaType="com.domain.User">
            <id property="id" column="id"/>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
        </association>
    </resultMap>
 <!--查询所有-->
    <select id="findAll" resultMap="accountUserMap">
        select a.id as aid,a.uid,a.money,u.username,u.address from account a,user u where u.id = a.uid;
    </select>
package com.domain;

import java.io.Serializable;

public class Account implements Serializable{
    private Integer id;
    private Integer uid;
    private Double money;

    //从表实体应该包含一个主表实体的对象引用
    private User user;
}

package com.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class User implements Serializable{
    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;

    //一对多关系映射,主表实体应该包含从表实体的集合应用
    private List<Account> accounts;
}

多对多查询

<?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.dao.IRoleDao">
    <!--定义role表的东西-->
    <resultMap id="roleUserMap" type="com.domain.Role">
        <id property="roleId" column="id"></id>
        <result property="roleName" column="role_name"></result>
        <result property="roleDesc" column="role_desc"></result>

        <collection property="users" ofType="com.domain.User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
        </collection>
    </resultMap>

    <!--查询所有-->
    <select id="findAll" resultMap="roleUserMap">
      SELECT u.*,r.* from role r
	  LEFT OUTER JOIN user_role ur on r.id = ur.RID
	  LEFT OUTER JOIN user u on u.id=ur.UID
    </select>
</mapper>
<?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.dao.IUserDao">

    <!--定义User的resultMap-->
    <resultMap id="userRoleMap" type="com.domain.User">
        <id column="id" property="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>

        <collection property="roles" ofType="com.domain.Role">
            <id property="roleId" column="id"></id>
            <result property="roleName" column="role_name"></result>
            <result property="roleDesc" column="role_desc"></result>
        </collection>
    </resultMap>
    
    <!--查询所有-->
    <select id="findAll" resultMap="userRoleMap">
        SELECT u.*,r.* from user u
	  LEFT OUTER JOIN user_role ur on u.id = ur.uID
	  LEFT OUTER JOIN role r on r.id=ur.rID
    </select>

    <!--根据id查询用户-->
    <select id="findById" parameterType="java.lang.Integer" resultType="com.domain.User">
        select * from user where id=#{uerId};
    </select>



</mapper>
package com.domain;

import java.io.Serializable;
import java.util.List;

public class Role implements Serializable{
    private Integer roleId;
    private String roleName;
    private String roleDesc;

    //多对多的关系映射。一个角色可以赋予多个用户
    private List<User> users;

    

package com.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class User implements Serializable{
    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;

    private List<Role> roles;

    
}

Mybatis延迟加载策略

何为延迟加载

  • 就是在需要用到数据时才进行加载,不需要拥戴数据时就不加载数据。延迟加载也称为懒加载。
  • 好处:
    • 先从单表查询,需要的时候再从关联查询,大大提高数据库性能,因为查询单表比关联查询多张表速度快
  • 坏处:
    • 因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降

使用assocation实现延迟加载(一对一/多对一)

<mapper namespace="com.dao.IAccountDao">

    <!--定义封装account和user的resultMap-->
    <resultMap id="accountUserMap" type="com.domain.Account">
        <id property="id" column="id"/>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>

        <!--一对一的关系映射,配置封装的user的内容
            select属性指定查询用户的唯一标致(我们调用的select映射的id)
            column属性指定用户根据id查询时,所需要的参数的值(填写我们要传递给select映射的参数)
        -->
        <association property="user" column="uid" javaType="com.domain.User" select="com.dao.IUserDao.findById">
        </association>
    </resultMap>

    <!--查询所有-->
    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>
</mapper>

开启mybatis的延迟加载

  • 我们需要在Mybatis的配置文件SqlMapConfig.xml文件中添加延迟加载的配置
 <!--配置参数-->
    <settings>
        <!--开启mybatis支持延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

使用Collection实现延迟加载(一对多/多对多)

<!--定义User的resultMap-->
    <resultMap id="userAccountMap" type="com.domain.User">
        <id column="id" property="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
        <!--配置user对象中account集合的映射
			ofType:用于指定集合元素的数据类型
			select:用于指定查询账户的唯一标识(账户的dao全限定类名加上方法名称)
			column:用于指定使用哪个字段的值作为条件查询
		-->
        <collection property="accounts" ofType="com.domain.Account" select="com.dao.IAccountDao.findAccountByUid" column="id">

        </collection>
    </resultMap>
在这里插入代码片
Mybatis缓存

mybatis一级缓存

  • 一级缓存时SqlSession级别的缓存,只要SqlSession没有flush或没有close,它就存在

一级缓存的分析

  • 一级缓存时SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存
    在这里插入图片描述
    1. 第一次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,如果没有,从数据库查 询用户信息。 得到用户信息,将用户信息存储到一级缓存中。 如果 sqlSession 去执行 commit 操作(执行插入、更新、删除),清空 SqlSession 中的一级缓存,这样 做的目的为了让缓存中存储的是最新的信息,避免脏读。
    2. 第二次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,缓存中有,直接从缓存 中获取用户信息。

Mybatis二级缓存

  • 二级缓存是mapper映射级别的缓存,多个SqlSession去操作同一个Mapper映射的sql语句,对各SqlSession可以公用二级缓存,二级缓存是跨SqlSession的
  • 二级缓存结构图
    在这里插入图片描述

    1. 首先开启 mybatis 的二级缓存。
    2. 去查询用户信息,查询到用户信息会将查询数据存储到二级缓存中。
    3. 如果 SqlSession3 去执行相同 mapper 映射下 sql,执行 commit 提交,将会清空该 mapper 映射下的二 级缓存区域的数据。
    4. sqlSession2 去查询与 sqlSession1 相同的用户信息,首先会去缓存中找是否存在数据,如果存在直接从 缓存中取出数据。

二级缓存的开启和关闭

  1. 在SqlMapConfig.xml文件开启二级缓存
 <settings>
        <setting name="cacheEnable" value="true"/>
 </settings>
  1. 配置相关mapper映射文件
<?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.dao.IUserDao">
    <!--开启user支持二级缓存-->
    <cache></cache>
</mapper>
  1. 配置statement上面的userCache属性
<!--根据id查询用户-->
    <select id="findById" parameterType="java.lang.Integer" resultType="com.domain.User" useCache="true">
        select * from user where id=#{uerId};
    </select>
	将设置userCache="true"代表当前这个statement要使用二级缓存,如果不使用可以设置为false
	注意:针对每次查询都需要最新的数据的sql,要设置userCache=false,禁止二级缓存

二级缓存的注意事项

  • 当我们在使用二级缓存时,所缓存的类一定要实现Serializable接口,这种就可以使用序列化方式来保存对象
MyBatis注解开发
  • mybatis的常用注解说明
@Insert:实现新增 
@Update:实现更新 
@Delete:实现删除 
@Select:实现查询
@Result:实现结果集封装 
@Results:可以与@Result 一起使用,封装多个结果集 @ResultMap:实现引用@Results 定义的封装 
@One:实现一对一结果集封装 
@Many:实现一对多结果集封装 
@SelectProvider: 实现动态 SQL 映射 
@CacheNamespace:实现注解二级缓存的使用
  • mybatis注解实现基本CRUD
//查询所有用户
    @Select("select* from user")
    List<User> findAll();

    //保存用户
    @Insert("insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})")
    void saveUser(User user);

    //更新用户
    @Update("update user set username=#{username} where id=#{id}")
    void updateUser(User user);

    //删除用户
    @Delete("delete from user where id=#{id}")
    void deleteUser(Integer userId);

    //根据id查询用户
    @Select("select * from user where id=#{id}")
    User findById(Integer userId);
  • 使用注解实现复杂关系映射开发
  1. 复杂关系映射的注解说明
@Results 注解 
	代替的是标签<resultMap>  
	该注解中可以使用单个@Result 注解,也可以使用@Result 集合 
	@Results{@Result(),@Result()})或 @Results@Result()) 
 
@Resutl 注解 
	代替了 <id>标签和<result>标签  
@Result 中 属性介绍:   
	id 是否是主键字段  
	column 数据库的列名  
	property 需要装配的属性名  
	one  需要使用的@One 注解(@Result(one=@One)()))  
	many  需要使用的@Many 注解(@Result(many=@many)())) 
 
@One 注解(一对一)  
	代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:  
	select  指定用来多表查询的 sqlmapper  
	fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。。 
使用格式:  
	@Result(column=" ",property="",one=@One(select="")) 
 
@Many 注解(多对一)       
	代替了<Collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。 
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,
	 属性的 javaType (一般为 ArrayList)但是注解中可以不定义;  
使用格式:   
	@Result(property="",column="",many=@Many(select="")) 

  1. 使用注解实现一对一复杂关系映射及延迟加载
//查询账户,并且获得每个账户所属的用户信息
    @Select("select * from account")
    @Results(id="accountMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "uid",property = "uid"),
            @Result(column = "money",property = "money"),
            @Result(property = "user",column = "uid",one = @One(select="com.dao.IUserDao.findById",fetchType= FetchType.EAGER))
    })
    List<Account> findAll();

    //根据id查询账户
    @ResultMap(value = {"accountMap"})
    @Select("select * from account where uid=#{id}")
    Account findById(Integer id);
  1. 使用注解实现一对多复杂关系映射
//查询所有用户
    @Select("select* from user")
    @Results(id = "userMap",value ={
            @Result(id = true,column = "id",property = "userId"),
            @Result(column = "username",property = "userName"),
            @Result(column = "address",property = "userAddress"),
            @Result(column = "sex",property = "userSex"),
            @Result(column = "birthday",property = "userBirthday"),
            @Result(column = "id",property = "accounts",many = @Many(select = "com.dao.IAccountDao.findById",fetchType = FetchType.LAZY))
    })
    List<User> findAll();

    //根据id查询用户
    @Select("select * from user where id=#{id}")
    @ResultMap(value = {"userMap"})
    User findById(Integer userId);
  • mybatis基于注解的二级缓存
  1. 在SqlMapConfig中开启二级缓存支持
<!--配置开启二级缓存-->
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
  1. 在持久层接口中使用注解配置二级缓存
/**
 * 在mybatis中针对CRUD一共有四个注解
 * @Select @Insert @Update @Delete
 */
@CacheNamespace(blocking = true)
public interface IUserDao {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值