网络知识 娱乐 01.mybatis入门介绍

01.mybatis入门介绍

一、什么是mybatis

MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apache software foundation迁移到了google code,并且改名为MyBatis。2013年11月迁移到Github。

官网地址:https://mybatis.net.cn/getting-started.html

二、使用环境准备

1.创建数据库

创建student表和calzz表。student表有五个字段:学生的ID,学生的姓名,学生的年龄,学生的住址,学生的班级号;班级表有三个字段:班级的ID,班级的名称,班级的编号,学生和班级之间使用班级号进行关联;

CREATE TABLE `student` ( `id` VARCHAR(100) NOT NULL, `sname` VARCHAR(100) DEFAULT NULL, `sage` INT(11) DEFAULT NULL, `saddress` VARCHAR(100) DEFAULT NULL, `clazzno` VARCHAR(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO student(id,name,age,address,clazzno) VALUES('1','张三',18,'四川省成都市武侯区','001');INSERT INTO student(id,name,age,address,clazzno) VALUES('2','李四',18,'四川省成都市锦江区','001');INSERT INTO student(id,name,age,address,clazzno) VALUES('3','王五',18,'四川省成都市金牛区','002');INSERT INTO student(id,name,age,address,clazzno) VALUES('4','赵六',18,'四川省成都市成华区','002');CREATE TABLE `clazz` ( `id` VARCHAR(100) NOT NULL, `cname` VARCHAR(100) DEFAULT NULL, `cno` VARCHAR(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO clazz(id,cla_name,cla_no) VALUES('1','一班','001');INSERT INTO clazz(id,cla_name,cla_no) VALUES('2','二班','002');INSERT INTO clazz(id,cla_name,cla_no) VALUES('3','三班','003');

2、创建maven项目

创建maven项目

3、引入jar包依赖

<!-- 引入数据库链接的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <!-- 引入mybatis驱动--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!--单元测试的依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- 实体类简写额依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency>

4、创建学生实体类

@Data@AllArgsConstructor@NoArgsConstructorpublic class Student { // 学生的ID String id; //学生姓名 String name; //学生年龄 Integer age; //学生住址 String address; //学生所在班级编号 String calzzno;}

5.静态资源过滤

静态资源过滤,是为了在编译的时候把mapper.mxl文件也编译到和类相同的文件夹下;

<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource></resources>

三、入门案例

1.创建核心配置文件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"/> <!--数据库链接池--> <dataSource type="POOLED"> <!--数据库驱动--> <property name="driver" value="com.mysql.jdbc.Driver"/> <!-- 数据库链接的地址--> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"/> <!--数据库用户名--> <property name="username" value="root"/> <!-- 数据库密码--> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!-- mapper映射文件--> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers></configuration>

2、创建StudentMapper接口

public interface StudentMapper { /** * 根据ID查询学生信息 * @param id * @return */ public Student getStudentByid(String id);}

3.创建接口对应的映射文件

<?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"><!--对应的studenMapper映射地址--><mapper namespace="com.jiaguohui.mybatis.mapper.StudentMapper"> <!-- ID是方法的名称,resultType是映射的实体类--> <select id="getStudentByid" resultType="com.jiaguohui.mybatis.domain.Student"> SELECT * FROM student where id = #{id} </select></mapper>

4.核心配置文件mybatis-config.xml中注册mapper

<mappers> <!-- mapper映射文件--> <mapper resource="com/jiaguohui/mybatis/mapper/StudentMapper.xml"/> </mappers>

四、使用测试

@Test public void getStudentByid() { //读取配置we年 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //使用getMapper()方法获取对象 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); //调用查询的方法 Student student = studentMapper.getStudentByid("1"); //打印对象 System.out.println("student = " + student); }

五、注意事项

1.核心配置文件mybatis-config.xml中注册mapper

<mappers> <!-- mapper映射文件--> <mapper resource="com/jiaguohui/mybatis/mapper/StudentMapper.xml"/> </mappers>

2.静态资源过滤

<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>