dongxurr 2020-02-03
Mybatis是一种面向sql的orm持久层框架,类似于hibernate。
iBatis前身,Mybatis底层依赖jdbc驱动程序,它把传统的jdbc连接数据库过程进行了封装,大大渐少了开发人员的工作量,是开发人员只需要专注于sql本身,而且方便代码维护,和持久利用,不用像传统mybatis那样对sql语句做频繁的修改。
那么先回顾一下传统的jdbc访问数据库过程:
JDBC缺点:
在idea开发环境中,新建一个mybatis工程,实现表的增删查改
在数据库中建立一张user表
create table user ( ID INT(11) PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(18) DEFAULT NULL, AGE INT(11) DEFAULT NULL )
pom文件配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ming</groupId> <artifactId>hhyMybatis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> </project>
建立sqlMapConfig来配置数据库信息:
<?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> <!--环境配置,连接的数据库,这里使用的是MySQL--> <environments default="development"> <environment id="development"> <!--指定事务管理的类型,这里简单使用Java的JDBC的提交和回滚设置--> <transactionManager type="JDBC"/> <!--dataSource 指连接源配置,POOLED是JDBC连接对象的数据源连接池的实现--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?charset=utf8&useSSL=false&allowPublicKeyRetrieval=true"/> <property name="username" value="root"/> <property name="password" value="hhyuan"/> </dataSource> </environment> </environments> <mappers> <!--这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名, 如果在某包下,则要写明路径,如:com/mybatistest/config/User.xml--> <mapper resource="User1.xml"/> </mappers> </configuration>
建立user.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"> <select id="GetUserByID" parameterType="String" resultType="com.ming.User"> select * from user where id = #{id} </select> <insert id="saveUser" parameterType="com.ming.User" useGeneratedKeys="true"> insert into user(NAME) values (#{name}) </insert> </mapper> <!--zi zeng zhang yao you zhu jian -->
建立测试类,测试查询:
package com.ming; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.*; import org.junit.Test; import java.io.InputStream; /** * @Information: * @Author: HeHaoYuan * @Date: Created at 13:18 on 2020/2/3 * @Package_Name: com.ming */ public class mybatisTest { private SqlSessionFactory sqlSessionFactory = null; @Before public void testInit() { InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatisconfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e){ e.printStackTrace(); } } @Test public void testGetUserByID(){ SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("test.GetUserByID","1"); System.out.println(user); }catch (Exception ex){ ex.printStackTrace(); } finally { sqlSession.close(); } } }
执行结果: