Mybatis介绍:
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
配置:
①导入jar包:mybatis-3.2.7.jar mysql-connector-java-5.1.30-bin.jar
②创建数据库和表(MySQL)
③在src创建全局配置文件(编写JDBC四个变量)
eg:mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- default 引用environment的id,当前所使用的环境 --> 7 <environments default="default"> 8 <environment id="default"> 9 <!-- 使用原生JDBC事务 --> 10 <transactionManager type="JDBC"></transactionManager> 11 <!-- 数据库连接池 --> 12 <dataSource type="POOLED"> 13 <property name="driver" value="com.mysql.jdbc.Driver"/> 14 <property name="url" value="jdbc:mysql://localhost:3306/ssm"/> 15 <property name="username" value="root"/> 16 <property name="password" value="310011"/> 17 </dataSource> 18 </environment> 19 </environments>
④在src下定义实体类,实体类代码如下:
package com.pojo; public class Flower { private int id; private String name; private double price; private String production; public Flower(int id, String name, double price, String production) { super(); this.id = id; this.name = name; this.price = price; this.production = production; } public Flower() { super(); } @Override public String toString() { return "Flower [id=" + id + ", name=" + name + ", price=" + price + ", production=" + production + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getProduction() { return production; } public void setProduction(String production) { this.production = production; } }
⑤定义操作数据库表的sql映射文件FlowerMapper.xml
创建一个com.mapper包,专门存放sql映射文件,在包中创建FlowerMapper.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"> <!-- namespase:理解成实现类的全路径(包名+类名) --> <mapper namespace="com.mapper.FlowerMapper">
<!-- id属性值必须是唯一的,不能重复 resultType属性指明查询返回的结果集类型,这里表示将查询结果封装成Flower类的对象返回
--> <select id="selAll" resultType="com.pojo.Flower"> select * from flower </select> </mapper>
⑥在mybatis.xml文件中注册FlowerMapper.xml文件
<mappers> <!-- 注册FlowerMapper.xml文件 --> <mapper resource="com/bjsxt/mapper/FlowerMapper.xml"/> </mappers>
⑦最后编写测试代码:
package com.test; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.List; 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 com.pojo.Flower; public class Test { public static void main(String[] args) throws IOException { //mybatis的配置文件 String resource = "mybatis.xml"; //使用类加载器加载mybatis的配置文件(也加载关联的映射文件) InputStream is = Test.class.getClassLoader().getResourceAsStream(resource); //使用工厂设计模式 //构建SQL Session的工厂 SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(is);//创建能执行映射文件中sql的sqlSession SqlSession session = sessionfactory.openSession(); /* * 映射sql的标识字符串 * com.bjsxt.mapper.FlowerMapper是FlowerMapper.xml文件中namespace属性的值, * selAll是select标签的id的属性值,通过select标签的id属性值就可以找到要执行的sql * */ String statement = "com.mapper.FlowerMapper.selAll";//映射sql的标识字符串 //执行查询返回一个唯一Flower对象的sql List<Flower> list = session.selectList(statement); for(Flower flower :list) { System.out.println(flower.toString()); } session.close(); } }
执行结果如下:
记录一下学习内容