温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SSM框架Mybatis的示例分析

发布时间:2021-10-21 10:53:58 来源:亿速云 阅读:113 作者:柒染 栏目:大数据

这篇文章将为大家详细讲解有关SSM框架Mybatis的示例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Mybatis

Mybatis有两个配置文件:映射文件(mapper.xml)、主配置文件(mybatis.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="test">
   <insert id="insertStudent" parameterType="beans.Student">     <!-- statement -->  <!-- parameterType:sqlSession传过来的参数类型 -->
      insert into student(name,age,score) values(#{name}, #{age}, #{score})        <!-- values中放的属性名(不是成员变量) -->
                                                                                   <!-- name反射为getname() -->
   </insert>
</mapper>

主配置文件

  • 配置事务管理:<transactionManager type="JDBC"/>

  • 数据源(连接数据库):<dataSource type="POOLED"><property name="driver/url/username/password  value="""

  • 注册映射文件:<mapper resource="mapper.xml" />

注:数据库连接池(POOLED)是内存中与数据库连接的进程

        SqlSessionFactory对象的构建需要dataSource对象,所以后续dataSource和SqlSession的创建都交给Spring即可

<?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="mysql_test_EM">             <!-- 指定跑的环境 -->
      <environment id="mysql_test_EM">
         <transactionManager type="JDBC"/>          <!-- 事务管理器:JDBC默认事务管理 -->
         <dataSource type="POOLED">                 <!-- 连接数据库:数据库连接池技术 -->
            <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"/>   <!-- test数据库 -->
            <property name="username" value="root"/>
            <property name="password" value="123123"/>
         </dataSource>
      </environment>
   </environments>
   
   <!-- 注册映射文件 -->
   <mappers>
      <mapper resource="dao/mapper.xml"/>
   </mappers>
</configuration>

配置文件需要mybatis约束文件(dtd)的约束,约束文件定义根标签

配置文件与程序:

  • 程序通过配置文件完成数据库连接和操作

  1. InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");     //通过mybatis.xml新建sql会话


  2. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


  3. sqlSession = sqlSessionFactory.openSession();


  4. sqlSession.insert("insertStudent", student);               //通过mapper.xml完成具体操作(mapper.xml中指定student类才知道对象中的属性)


  5. sqlSession.commit();                                       //提交后才能存入数据库


注:sqlSession提供增删改查操作函数,mapper.xml中通过"#{}"接收函数传过来的值,resultType指定数据封装成什么样子

sqlSession操作方法:

方法含义
  
  

进阶:

(1)mapper动态代理

  • 省去实现类(实现类的作用就是联系方法与mapper.xml中相应的sql语句)

  • 前提1:将mapper.xml中的namespace命名为接口全路径名“dao.IStudentDao”

  • 前提2:接口方法须和mapper语句id一样

  • mapper中语句id=“insertStudent”,接口中方法名为insertStudent(Student stu)

*****IStudentDao dao = sqlSession.getMapper(dao.IStudentDao.class);         //获取IStudentDao接口的代理对象 ,该代理对象就相当于实现类

                那么dao.insertStudent(student)直接对应mapper中相应的语句执行

  • 非动态代理情况:通过实现类找到相应的mapper语句

注:IStudentDao是一个接口 它并没有实现类,为什么接口可以直接使用呢? 因为MyBbatis使用了JDK动态代理机制动态生成了代理类。

mapper动态代理原理

(2)动态SQL

  • 用于解决用户查询条件不确定的情况

  • 参数是对象(包含所有的可能条件即属性),在接口中改该方法的参数

  • if拼接

<select id="selectStudentsByIf" resultType="Student">
      select id,name,age,score 
      from student 
      where 1=1                                            <!-- 为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误,会降低系统效率 -->
      <if test="name != null and name != ''">
         and name like "%" #{name} "%"
      </if>
      <if test="age > 0">
         and age > #{age}
      </if>
</select>
  • where标签拼接取代if

<select id="selectStudentsByWhere" resultType="Student">
      select id,name,age,score 
      from student 
      <where>                                                       <!-- 为了避免where 1=1 影响效率-->
         <if test="name != null and name != ''">
            and name like "%" #{name} "%"
         </if>
         <if test="age > 0">
            and age > #{age}
         </if>
      </where>
</select>

(3)关联关系查询

  • 查询涉及多个表

  • 延迟加载

(4)查询缓存

  • 提高查询效率,不用每次都从数据库中查询结果

(5)注解式开发

  • 替换映射文件mapper.xml

@Insert(value = { "insert into student(name,age,score) values(#{name}, #{age}, #{score})" })
void insertStudent(Student student);

总结:

  • 通过Mapper接口注册映射文件(批量加载)

  1. Mapper 接口必须要和 Mapper.xml 文件同名且在同一个包下,也就是说 Mapper.xml 文件中的namespace是Mapper接口的全类名

  2. Mapper接口中的方法名和 Mapper.xml 文件中定义的 id 一致

  3. Mapper接口输入参数类型要和 Mapper.xml 中定义的 parameterType 一致

  4. Mapper接口返回数据类型要和 Mapper.xml 中定义的 resultType 一致

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="com.ys.mapper.Mapper">

</mapper>

MyBatis.xml

<mappers>
 <!--批量加载mapper
 指定 mapper 接口的包名,mybatis自动扫描包下的mapper接口进行加载
 -->
 <package name="com.ys.mapper"/>
</mappers>

关于SSM框架Mybatis的示例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI