mybatis官方文档连接:https://mybatis.org/mybatis-3/zh/getting-started.html
dome结构:
数据库结构:
项目代码:
UserDao.java
package dao; import pojo.User; import java.util.List; public interface UserDao { List<User> getUserList(); }
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"> <!--namespace=绑定一个对应的Dao/Mapper接口--> <mapper namespace="dao.UserDao"> <select id="getUserList" resultType="pojo.User"> select * from mybatis.user </select> </mapper>
User.java
package pojo; //实体类 public class User { int id; String name; String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
MybatisUtils.java
package 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; //sqlSessionFactory sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; //使用Mybatis第一步,获取sqlSessionFactory对象 static { String resource = "mybatis-config.xml"; { try { InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } } //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。 //SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
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?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC "/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--每一个mapper都要注意一次--> <mappers> <mapper resource="dao/UserMapper.xml"></mapper> </mappers> </configuration>
UserDaoTest
package dao; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import pojo.User; import utils.MybatisUtils; import java.util.List; public class UserDaoTest { @Test public void test() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //执行Sql UserDao userDao=sqlSession.getMapper(UserDao.class); List<User> userList=userDao.getUserList(); for(User user:userList) { System.out.println(user.getId()); } //关闭SqlSession sqlSession.close(); } }
数据库内容:
测试结果图:
体会:
测试代码调用MybatisUtils.java文件,MybatisUtils.java文件调用mybatis-config.xml配置文件,测试文件再利用所返回的SqlSession调用UserDao接口文件,运行UserMapper.xml文件中的sql语句。经学习,了解到mybatis可以很好的将文件解耦,对程序的维护有利。
学习一项新的技术,他的官方文档很重要。