mybatis简介
1 mybatis是什么
-
MyBatis 是一款优秀的持久层框架
-
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程
-
MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 实体类 【Plain Old Java Objects,普通的 Java对象】映射成数据库中的记录。
2 持久化和持久层
持久化
-
将原本存储在内存的临时数据保存到可永久化的存储设备中。
-
JDBC就是一种持久化机制。文件IO也是一种持久化机制。
-
持久化的需要是因为内存的缺陷引起的
持久层
- 完成持久化工作的代码块 . ----> dao层 【DAO (Data Access Object) 数据访问对象】
3 Mybatis的优点
- 使sql与程序代码解耦合
- 灵活,不会对应用程序产生影响
mybatis快速构建
- 为了方便使用可以安装lombox插件和Free Mybatis plugn插件
流程:pom环境–>mybatis-configxml配置文件–>工具类–>实体类–>接口–>接口实现类–>测试类
-
pom.xml
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
-
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://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
-
SqlSessionUtil工具类(用于获取session)
public class SqlSessionUtil { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //获取SqlSession连接 public static SqlSession getSession(){ return sqlSessionFactory.openSession(); } }
-
实体类UserInf
@Data @AllArgsConstructor @NoArgsConstructor public class UserInf { private int id; //id private String name; //姓名 private String pwd; }
-
接口UserMapper
-
List<UserInf> selectUser();
接口实现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=""> ... </mapper>
别忘了在Mybatis-config.xml中注册
<configuration> <mappers> <mapper resource="com\lyj\mapper\UserMapper.xml"/> </mappers> </configuration>
-
测试
@Test public void Test1(){ SqlSession session = SqlSessionUtil.getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); List<UserInf> users = userMapper.selectUser(); for (UserInf u : users) { System.out.println(u); } session.close(); }