一·MyBatis简介
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
二.MyBatis的配置
1.先在SQLyog中建立数据库,建立mybatis数据库,建一个表,用于测试。
CREATE DATABASE MyBatis;
USE MyBatis;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id INT NOT NULL PRIMARY KEY,
`username` VARCHAR(30),
pwd VARCHAR(30)
)ENGINE=INNODB CHARSET=utf8;
INSERT INTO `user` VALUES(1,'张三','123456'),(2,'李四','123456'),(3,'王五','123456');
表中数据如下:
2.在idea中建maven项目,采用父子项目格式,在父项目的pom.xml文件中导入依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
3.在resource中新建mybatis-config.xml
改驱动driver,url,username,password,跟JDBC一样。
<?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"/>
//不同Mysql版本,有的不能建立安全链接,只能显示定义useSSL=false.
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/he/dao/userDao/userMapper.xml"></mapper>
</mappers>
</configuration>
4.编写实体类。
5.封装工具类。
package com.he.dao.utils;
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.IOException;
import java.io.InputStream;
public class MyBatisUtils {
static SqlSessionFactory sqlSessionFactory = null;
static{
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
6.编写dao层Mapper接口。
package com.he.dao.userDao;
import com.he.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
7.编写Mapper.xml(类似于Mapper接口的实现类,存在映射关系)
<?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.he.dao.userDao.UserMapper">
//id为方法名,resultType为返回的类型,填写List<>中的实体类
<select id="getUserList" resultType="com.he.pojo.User">
select * from `user`
</select>
</mapper>
8.注册Mapper。
在mybatis-config.xml中注册Mapper。
路径用 / 隔开。
<mappers>
<mapper resource="com/he/dao/userDao/userMapper.xml"></mapper>
</mappers>
9.在test文件中测试用Junit。
public class UserMapperTest {
@Test
public void test(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//获取sqlsession
UserMapper mapper = sqlSession.getMapper(UserMapper.class);//获取接口
List<User> userList = mapper.getUserList();//调用接口方法
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
注意:
在java文件夹中的xml等配置文件可能不会被项目打包出去,得在pom.xml中配置。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
测试结果:
总结:
简化代码,第一次配置较为繁琐,以后多次利用,相比JDBC大大减少代码的量。