MyBatis入门案例
MyBatis是服务器与数据库交互的轻量级框架,需要导入相关jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
跟着官方文档一点一点来实现与数据库的交互
第一步:通过xml配置文件创建SqlSessionFactory对象
配置数据库连接的信息
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
<?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?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="Hkx123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="employeeMapper.xml"/>
</mappers>
</configuration>
第二步:从sqlSessionFactory对象中获取SqlSession对象
@Test
public void test01() throws IOException {
SqlSessionFactory sqlSessionFactory = MyBatisTest.getSqlSessionFactory();
//获取sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//调用sqlSession.seleteOne()查询方法,第一个参数为映射的xml配置文件的名称空间+id,第二个参数为sql语句的占位符
Employee employee = sqlSession.selectOne("com.yellowstar.mybatis.EmployeeMapper.selectEmployee", 1);
System.out.println(employee);
//需使用try-catch-finally关闭资源
sqlSession.close();
}
创建employeeMapper.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.yellowstar.mybatis.EmployeeMapper">
<!--
namespace:名称空间
id:唯一表示
resultType:返回值类型
#{id}:从传递过来的参数中取id值
-->
<select id="selectEmployee" resultType="com.yellowstar.mybatis.bean.Employee">
select * from employee where id = #{id}
</select>
</mapper>
接口式编程
我们通过接口,自动匹配xml文件中的名称空间与id值
public interface EmployeeDao {
Employee getEmployeeById(Integer id);
}
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.yellowstar.mybatis.dao.EmployeeDao">
<select id="getEmployeeById" resultType="com.yellowstar.mybatis.bean.Employee">
select * from employee where id = #{id}
</select>
</mapper>
如何调用?
@Test
public void test02() throws IOException {
SqlSessionFactory sqlSessionFactory = MyBatisTest.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取接口对象,参数为接口的类型
EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
Employee employee = mapper.getEmployeeById(2);
System.out.println(employee);
sqlSession.close();
}