MyBatis
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
总的来说就是可以完成持久化的代码块;
MyBatis可以从Maven中导入
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency>
可以从github中获取:Releases · mybatis/mybatis-3 (github.com)
搭建环境
在resources文件目录下新建一个 mybatis-config.xml 文件
其中url是数据库地址
driver是驱动地址
username是数据库账号
password是数据库密码
mapper中的resource是Dao下面的UseMapper.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.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="zhang1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/zhang/Dao/UserMapper.xml"/> </mappers> </configuration>
UseMapper.xml
mapper 的namespace是UserDao
resultType是返回类型
<?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">
<!--命名空间绑定一个DAO/MAPPER接口-->
<mapper namespace="com.zhang.Dao.UserDao">
<!-- select查询语句-->
<select id="getUserList" resultType="com.zhang.pojo.User">
select * from mybatis.user
</select>
</mapper>
UserDao
package com.zhang.Dao; import com.zhang.pojo.User; import java.util.List; public interface UserDao { List<User> getUserList(); }
UserDaoImpl
package com.zhang.Dao; import com.zhang.pojo.User; import java.util.List; public class UserDaoImpl implements UserDao{ @Override public List<User> getUserList() { return null; } }
MybaitisUtils
package com.zhang.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 { private static SqlSessionFactory sqlSessionFactory = null; static { try { //使用mybatis第一步:获取 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession() { SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
实体类User
package com.zhang.pojo; //实体类 public class User { private long id; private String name; private String password; public User() { } public User(long id, String name, String password) { this.id = id; this.name = name; this.password = password; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }