1. MyBatis简介
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
2. MyBatis框架
2.1 MyBatis下载
MyBatis下载地址:
MyBatis参考文档:
2.2 MyBatis基本构成
MyBatis核心组件:
◊ SqlSessionFactoryBuilder:根据配置信息或代码来生成SqlSessionFactory。
◊ SqlSessionFactory:生成SqlSession。
◊ SqlSession:发送SQL去执行并返回结果,获取Mapper接口。
◊ SQL Mapper:由一个Java接口和XML文件(或注解)构成,提供对应的SQL和映射规则。负责发送SQL去执行,并返回结果。
3. MyBatis快速入门
3.1 基于XML实现
项目结构:
4.0.0 libing com-helloworld-api war 0.0.1-SNAPSHOT com-helloworld-api Maven Webapp http://maven.apache.org UTF-8 UTF-8 1.8 mysql mysql-connector-java 5.1.44 org.mybatis mybatis 3.4.5 log4j log4j 1.2.17 junit junit 4.12 test com-helloworld-api org.apache.maven.plugins maven-compiler-plugin
database.driver=com.mysql.jdbc.Driverdatabase.url=jdbc:mysql://localhost:3306/helloworld?characterEncoding=utf8database.username=rootdatabase.password=root
log4j.rootLogger=DEBUG,stdoutlog4j.logger.org.mybatis=DEBUGlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
package com.libing.helloworld.model;public class Role { private Integer id; private String roleName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }
package com.libing.helloworld.dao;import java.util.List;import com.libing.helloworld.model.Role;public interface IRoleDao { ListfindAll(); }
package com.libing.helloworld.test;import java.io.InputStream;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.apache.log4j.PropertyConfigurator;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import com.libing.helloworld.dao.IRoleDao;import com.libing.helloworld.model.Role;public class RoleTest { SqlSession sqlSession = null; @Before public void init() { PropertyConfigurator.configure(RoleTest.class.getClassLoader().getResourceAsStream("log4j.properties")); String resource = "mybatis-config.xml"; try { InputStream inputStream = RoleTest.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); } catch (Exception e) { e.printStackTrace(); } } @Test public void findAll() { try { IRoleDao roleDao = sqlSession.getMapper(IRoleDao.class); Listroles = roleDao.findAll(); Assert.assertNotNull(roles); Assert.assertTrue(roles.size() > 0); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } }}
选中RoleTest.java,右击 | Run As | JUnit Test,查看运行结果。
DEBUG [main] - ==> Preparing: SELECT id, role_name FROM role ORDER BY id ASC DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 6
3.2 基于注解实现
package com.libing.helloworld.dao;import java.util.List;import org.apache.ibatis.annotations.Select;import com.libing.helloworld.model.Week;public interface IRoleDao { @Select("SELECT id,role_name FROM role ORDER BY id ASC") ListfindAll();}
3.3 mysql-connector-java 6.0.6配置
pom.xml:
mysql mysql-connector-java 6.0.6
mybatis-config.xml:
jdbc.properties:
database.driver=com.mysql.cj.jdbc.Driverdatabase.url=jdbc:mysql://localhost:3306/helloworld?serverTimezone=UTCcharacterEncoding=utf8&useSSL=falsedatabase.username=rootdatabase.password=root