1. Mybatis 介绍
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以
及对结果集的检索封装。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,
普通的 Java 对象)映射成数据库中的记录.JDBC-->dbutils-->MyBatis-->Hibernate。
2. mybatis 快速入门
编写第一个基于 mybaits 的测试例子:
2.1. 添加 jar 包
【mybatis】
mybatis-3.1.1.jar
【MYSQL 驱动包】
mysql-connector-java-5.1.7-bin.jar
2.2. 建库+表
create database mybatis; use mybatis; CREATE TABLE users( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT ); INSERT INTO users(NAME, age) VALUES('Tom', 12); INSERT INTO users(NAME, age) VALUES('Jack', 11);
2.3. 添加 Mybatis 的配置文件 conf.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://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration>
2.4. 定义表所对应的实体类
public class Users { private int id; private String name; private int age; //get,set 方法 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public Users(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public Users() { } public Users(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
2.5. 定义操作 users 表的 sql 映射文件 userMapper.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="com.zhiyou100.hhz.mapper.UsersMapper"> <select id="getUser" parameterType="int" resultType="com.zhiyou100.hhz.bean.Users"> select * from users where id=#{id} </select> <insert id="addUser" parameterType="com.zhiyou100.hhz.bean.Users"> insert into users(name,age) value(#{name},#{age}) </insert> <update id="updateUser" parameterType="com.zhiyou100.hhz.bean.Users"> update Users set name=#{name},age=#{age} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from Users where id=#{id} </delete> <select id="getUserAll" resultType="com.zhiyou100.hhz.bean.Users"> select * from users </select> <select id="selectUserByAge" parameterType="java.util.Map" resultType="com.zhiyou100.hhz.bean.Users"> <![CDATA[select * from users where age>=10 and age<=30]]> </select> </mapper>
2.6. 在 conf.xml 文件中注册 userMapper.xml 文件
在</configuration>标签上写
<mappers> <mapper resource="com/zhiyou100/hhz/mapper/UsersMapper.xml" /> </mappers>
2.7. 编写测试代码:执行定义的 select 语句等
class UsersTest { final String str="com.zhiyou100.hhz.mapper.UsersMapper."; static SqlSession session = null; @BeforeAll static void setUpBeforeClass() throws Exception { Reader reader = Resources.getResourceAsReader("conf.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); session = sessionFactory.openSession(); } @Test void test1() { Users users = session.selectOne(str+"getUser",1); System.out.println(users); } @Test void test2() { session.insert(str+"addUser",new Users("朱华龙",1)); } @Test void test3() { session.update(str+"updateUser",new Users(1,"王玉芳",25)); } @Test void test4() { session.delete(str+"deleteUser",6); } @Test void testselectAll() { List<Users> list = session.selectList(str+"getUserAll"); for(Users users:list) { System.out.println(users); } } @Test void testselectByAge() { Map<String, Integer> map=new HashMap<String,Integer>(); map.put("min", 10); map.put("max", 30); List<Users> list = session.selectList(str+"selectUserByAge",map); for(Users users:list) { System.out.println(users); } } @AfterAll static void tearDownAfterClass() throws Exception { session.commit(); }