SSM框架Mybatis的示例分析

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

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

Mybatis

Mybatis有两个配置文件:映射文件(mapper.xml)、主配置文件(mybatis.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="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>

主配置文件

注:数据库连接池(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动态代理

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

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

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

mapper动态代理原理

(2)动态SQL

<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>
<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)注解式开发

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

总结:

  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的示例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. mybatis属性的示例分析
  2. MyBatis参数的示例分析

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

mybatis ssm框架

上一篇:Go语言循环和判断以及选择语句有哪些

下一篇:Android如何实现点击持续录音,松开结束录音并实现随着分贝的大小改变图片

相关阅读

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

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