Mybatis-mybatis使用&xml文件配置

一,概述:

Mybatis官网

Mybatis介绍:

Mybatis是一款优秀的持久层框架,它支持定制化SQL,存储过程以及高级映射。mybaits 避免了几乎所有的JDBC代码和手动设置参数以及获取结果集
Mybatis可以使用简单的xml或注解来配置和映射原生信息,将接口和java的POJO(Plain Old java Objects,普通java对象)映射成数据库中的记录。

Mybatis是基于JDBC做的简单的映射包装。

Mybatis架构:

Mybatis-mybatis使用&xml文件配置_第1张图片
Mybatis-mybatis使用&xml文件配置_第2张图片

二,Mybatis使用

1, pom 文件引入依赖

    
  <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.30version>
  dependency>
   <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.5version>
    dependency>

2,mybatis-config.xml :全局配置文件

配置数据源,事务,加载映射文件。 后文详解

3,创建pojo类(提供基本的get/set方法)

/**
 * 创建Pojo类,和数据库表Student对应
 * 属性需要与数据库一一对应
 */
public class Student {
    private int SID;
    private String Sname;
    private String Ssex;
    private int Sage;
    省略get和set方法
 }  

4,创建Mapper.java接口

该接口中的方法名需要唯一,每一个接口方法均与Mapper.xml文件sql语句一一映射

public interface StudentMapper {
    
//   需求:通过ID查询Student表中用户信息
    //select * from Student where SID = XXX
    public Student getStudentById(int sid);
}

5,Mapper.xml 映射配置文件





<mapper namespace="com.dao.StudentMapper">
    
    <select id="getStudentById" parameterType="java.lang.Integer" resultType="com.bean7.Student">
        select * from Student where SID = #{sid}
    select>
mapper>

将Mapper.xml 配置到全局配置文件中 mybatis-config.xml

     <mappers>
          <mapper resource="mapper/StudentMapper.xml"/>
      mappers>

编程使用 :一般放在启动类中

  • SqlSessionFactory :会话工厂,
    通过读取配置文件创建,配置文件只需读取一次即可,可以将SqlSessionFactory设置单例
  • SqlSession: 会话
    通过SqlSessionFactory 得到,对数据库的CRUD操作都是通过SqlSession来操作的
    SqlSession是线程不安全的,将其设置为方法的局部变量
    一级缓存是基于sqlsession来创建的,mybatis默认开启一级缓存
/**
 * @Created with IntelliJ IDEA
 * @Description:
 * @Package: PACKAGE_NAME
 * @author: FLy-Fly-Zhang
 * @Date: 2019/7/26
 * @Time: 13:56
 */
public class App {
    public static void main(String[] args) throws IOException {
        String resource="mybatis-config.xml";
        //读取配置文件
        InputStream inputStream=Resources.getResourceAsStream(resource);
        //创建sqlSessionFactory 
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //设置是主动提交事务还是手动,默认手动
        sqlSessionFactory.openSession(true);
        //得到sqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //通过动态代理产生StrudentMapper对象
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
        //调用对应接口方法,进行sql语句操作
        Student student = mapper.getStudentById(1);
        //若未设置主动提交事务,进行手动提交
        sqlSession.commit();
        System.out.println(student);

    }
}

三,配置文件详解:

全局配置文件

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis
db.username=root
db.password=12345678
 
    <properties>-->
       <property name="db.properties" value="db.properties"/>
    properties>

    
    <settings>
        
        
        
        
        
        
    settings>
    
    
    <typeAliases>
        
        <typeAlias type="com.bean7.Student" alias="student"/>
        
        
    typeAliases>

    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            
		<dataSource type="POOLED">
			<property name="driver" value="${db.driver}"/>
			<property name="url" value="${db.url}"/>
			<property name="username" value="${db.username}"/>
			<property name="password" value="${db.password}"/>
		dataSource>
		   
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="xxxx"/>
                <property name="password" value="xxxxx"/>
                
                
            dataSource>
        environment>
    environments>

    
    <mappers>
        
        <mapper resource="mapper7/StudentMapper.xml"/>
        
        
    mappers>
    
setting标签

Mybatis-mybatis使用&xml文件配置_第3张图片
Mybatis-mybatis使用&xml文件配置_第4张图片

mapper配置文件

#{} 和${}区别:

mybatis中有两个常见的符号#和KaTeX parse error: Expected 'EOF', got '#' at position 25: …的时候是很常见的,我的理解就是#̲是需要进行预编译的,是字符串进…是直接进行字符串的替换工作,两者各有有缺点。

  • #{} :表示一个占位符 ?
  1. 通过#{ }可以实现preparedStatement向占位符中设置值,

  2. 自动进行java类型和jdbc类型转换,

  3. #{ }可以有效防止sql注入。

  4. #{ }可以接收简单类型值或pojo属性值。

  5. 如果parameterType传输单个简单类型值,#{ }括号中可以是value或其它名称。

我们一般在刚刚接触到mybatis的时候都是使用的是#{变量名字},来进行sql语句的赋值工作,比如 select * from table_name where name = #{name},来进行查询,在接口里面传入Map或者是String都是可以的,这样的是mybatis进行了语句的编译工作,#{name},必须要放到name=后面,这样才会编译通过,进行执行sql语句。

  • $ {}
  1. ${ }可以将parameterType 传入的内容拼接在sql中

  2. 不进行jdbc类型转换,

  3. $ { }可以接收简单类型值或pojo属性值,

  4. 如果parameterType传输单个简单类型值,${ }括号中只能是value。

我们需要进行变化传输sql语句,这个时候一般都会用到的是等mybatis里面的动态标签来动态拼接sql语句,但是有的时候根据一些业务场景是需要传输表名或者是特定的变量名的,这个时候就需要根据业务来动态的改变查询的表名等问题,这个时候 的 好 处 就 体 现 了 , 的好处就体现了, 我的感觉就是单纯的字符串替换,比如,select * from table_name where name = ${name}这个时候mybatis不需要编译而是直接进行字符串的替换工作,比如我要传入一个表名,select * from KaTeX parse error: Expected 'EOF', got '#' at position 26: …} where name = #̲{name},这个时候在前台就…{tableName}就是单纯的字符串替换,而#{name}则需要进行编译才可以赋值

  • 注意:
    一般还是使用#,因为#是比较安全的一种传输方式,在一些渗透和安全性校验的时候,$ 不是特别安全,容易引起sql的注入问题,导致毁灭性灾难,但是也不是说一点不能用,只能说不可以从前台传输有关于sql的语句来使用 $ 否则就会出现删除数据的问题。

<mapper namespace="com.dao7.StudentMapper">

    
    <resultMap id="resultStudent" type="com.bean7.Student">
        
        <id column="SID" property="SID" javaType="java.lang.Integer" jdbcType="INTEGER">id>
        <result column="Sname" property="name">result>
        <result column="Ssex" property="Ssex">result>
        <result column="Sage" property="Sage">result>

    resultMap>
    
    <select id="getStudentById" parameterType="java.lang.Integer" resultMap="resultStudent">
        select * from Student where SID = #{sid}
    select>

    
    
        insert into Student (SID,Sname) values (#{SID},#{Sname})
    insert>

    
    <delete id=""/>
    
    
    <update id=""/>  
  

mybatis接口绑定形式:配置和注解

配置:
  • mapper.xml和mapper.java 绑定
  • mapper.xml文件中的namespace属性和mapper.java接口的类全路径保持一致
  • mapper.xml文件sql的CRUD的id属性和mapper.java接口中对应的方法名保持一致,且一个xml文件中,id不能重复
  • mapper.xml文件中的操作的parameterType属性和mapper.java接口中对应的方法入参类型保持一致。
  • mapper.xml文件中的操作的resultType属性和mapper.java接口中对应方法返回类型保持一致。
注解形式:
  • 当方法参数只有一个时,不需要使用@param注解
  • 当方法参数有多个时,需要使用@param注解将形参和@param参数进行一一绑定。

public interface StudentMapper {

    /**
     * 通过对象新增
     * @param student
     * @return
     */
    @Insert("insert into student(student_number, student_name, age) values (#{studentNumber, jdbcType=VARCHAR}, #{studentName, jdbcType=VARCHAR}, #{age, jdbcType=INTEGER})")
    int insertByObject(Student student);

    /**
     * 通过map新增
     * @param map
     * @return
     */
    @Insert("insert into student(student_number, student_name, age) values (#{studentNumber, jdbcType=VARCHAR}, #{studentName, jdbcType=VARCHAR}, #{age, jdbcType=INTEGER})")
    int insertByMap(Map<String, Object> map);

    /**
     * 通过编号查询
     * @param studentNumber
     * @return
     */
    @Select("select * from student where student_number = #{studentNumber}")
    @Results({
            @Result(column = "student_number", property = "studentNumber"),
            @Result(column = "student_name", property = "studentName"),
            @Result(column = "age", property = "age")
    })
    Student findByStudentNumber(String studentNumber);

    /**
     * 通过姓名查询
     * @param studentName
     * @return
     */
    @Select("select * from student where student_name = #{studentName}")
    @Results({
            @Result(column = "student_number", property = "studentNumber"),
            @Result(column = "student_name", property = "studentName"),
            @Result(column = "age", property = "age")
    })
    List<Student> findByStudentName(String studentName);

    /**
     * 通过编码删除
     * @param studentNumber
     * @return
     */
    @Delete("delete from student where student_number = #{studentNumber}")
    int deleteByStudentNumber(String studentNumber);

    /**
     * 通过对象修改
     * @param student
     * @return
     */
    @Update("update student set student_name = #{studentName}, age = #{age} where student_number = #{studentNumber}")
    int updateByObject(Student student);

    /**
     * 通过编码修改名字
     * 传多个参数时必须使用@Param("")
     * @param studentName
     * @param StudentNumber
     * @return
     */
    @Update("update student set student_name = #{studentName} where student_number = #{studentNumber}")
    int updateStudentNameByStudentNumber(@Param("studentName") String studentName, @Param("studentNumber") String StudentNumber);

}

参考博主:

Mybatis全面详解
Mybatis教程

你可能感兴趣的:(Mybatis)