时间:2022-11-06 09:20:46 | 栏目:JAVA代码 | 点击:次
Mybatis是一款超级无敌的持久层框架,它支持自定义SQL、存储过程以及高级映射。Mybatis可以通过简单的XML或者注解来配置和映射原始类型、接口和Java POJO为数据库中的记录。
Mybatis相比于Hibernate在处理复杂业务的时候灵活度更高、复杂的SQL编写更容易。
很早之前我们引入一个外部项目需要到处去下载jar包,然后放入项目的lib目录下,但是自从用了maven以后我们只需要一个坐标,maven帮我们搞定一切!
<?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>org.example</groupId> <artifactId>Mybatis-Test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- 引入Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> </project>
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/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="TestMapper.xml"/> </mappers> </configuration>
说明:
1.注意自己的driver 看是否需要根据版本改为:com.mysql.cj.jdbc.Driver
2.&
这个是&在xml中的转义表示
TestMapper.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="TestMapper"> <select id="list" resultType="map"> select * from test </select> </mapper>
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 java.io.InputStream; import java.util.List; import java.util.Map; /** * 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作 * @create 2021-08-25 21:26 */ public class TestMain { public static void main(String[] args) throws Exception { // 1.配置文件编程输入流 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); // 2. 创建SqlSession工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 3. 创建SqlSession // try里边创建数据流 try语句执行完后流会被自动关闭 但是资源必须实现java.lang.AutoCloseable接口 // 这叫做 ARM 块(Automatic Resource Management) // SqlSession 实现了Closeable Closeable继承了AutoCloseable接口 try (SqlSession session = sqlSessionFactory.openSession()) { // 4. 执行查询 查列表出来 List<Map<String,Object>> res = session.selectList("TestMapper.list"); // 5. 遍历结果输出 if (res != null) { for (int i = 0; i < res.size(); i++) { Map<String,Object> tmp = res.get(i); System.out.println("id="+tmp.get("id")+",name="+ tmp.get("name")+",salary="+tmp.get("salary")); } } } } }
输出结果
项目结构:
下篇预告: