实验八 MyBatis基础应用
一、基础实验——MyBatis框架搭建
(一)实验目的
- 掌握MyBatis 开发环境搭建的基本步骤,了解MyBatis 的基本原理;
- 观察持久化类与数据库表的映射关系,观察相应的Mybatis 映射文件配置,并能够做简单应用;
- 观察MyBatis 配置文件中的主要元素及属性配置,并能够做简单应用。
(二)基本知识与原理
- MyBatis是一个基于Java的持久层ORM(Object-Relational Mapping)框架,其提供的持久层框架包括SQL Maps和Data Access Objects(DAO),几乎消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索;
- MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs映射成数据库中的记录;
(三)实验过程及其记录
-
在阿里云云数据库RDS实例中创建名为mybatisdb的数据库,并在其中创建一个名为user的数据表,表结构如下:
字段名称 类型 中文含义 uid INTEGER(11),Primary key,Not Null 用户编号 uname VARCHAR(20) 用户名 usex VARCHAR(20) 用户性别 建表DDL如下:
create table user( uid integer(11) primary key not null , uname varchar(20), usex varchar(20) );
-
在表user中添加若干条记录;
-
新建工程mybatis-proj1,将MySQL驱动文件加入到工程中;
-
下载MyBatis发布版,将核心jar包、依赖jar包添加到工程中,maven配置如下:
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>7.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.2.20</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.27.0-GA</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.30</version> <scope>test</scope> </dependency> </dependencies>
-
创建一个名为com.mybatis.po包,在该包中创建持久化类MyUser,类中声明的属性与数据表user的字段一致:
package com.mybatis.po; public class User { private int uid; private String uname; private String sex; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
-
创建名为com.mybais.mapper包,在该包中创建映射文件UserMapper.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.mybatis.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="com.mybatis.po.User"> Select * from user where uid = #{uid} </select> <select id="selectAllUser" resultType="com.mybatis.po.User"> Select * from user </select> <insert id="addUser" parameterType="com.mybatis.po.User"> Insert into user (uname,usex) values(#{uname},#{usex}) </insert> <update id="updateUser" parameterType="com.mybatis.po.User"> update user set uname=#{uname}, user=#{usex} where uid=#{uid} </update> <delete id="deleteUser" parameterType="Integer"> delete from user where uid = #{uid} </delete> </mapper>
-
创建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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://rm-bp10ju74719fp6g4emo.mysql.rds.aliyuncs.com:3306/mybatisdb"