1.创建maven工程
创建 mybatis01 的工程,工程信息如下:
Groupid:com.itheima
ArtifactId:mybatisfirst
Packing:jar
2.添加Mybatis3.4.5的坐标
在 pom.xml 文件中添加 Mybatis3.4.5 的坐标,如下
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies>
3、编写User实体类
public class Users implements Serializable { private String ID; private String Name; private String Account; private String Password; private Integer Status; private Integer Sort; private String Note; public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getAccount() { return Account; } public void setAccount(String account) { Account = account; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } public Integer getStatus() { return Status; } public void setStatus(Integer status) { Status = status; } public Integer getSort() { return Sort; } public void setSort(Integer sort) { Sort = sort; } public String getNote() { return Note; } public void setNote(String note) { Note = note; } }
4.编写持久层接口IUserDao
public interface IUserDao { List<Users> FindAll(); Users FindByName(String Name); }
5、编写持久层的映射文件IUserDao.xml
要求:
创建位置:必须和持久层接口在相同的包中。
名称:必须以持久层接口名称命名文件名,扩展名是.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.mingqi.dao.IUserDao"> <select id="FindAll" resultType="com.mingqi.domin.Users"> select * from Users </select> <select id="FindByName" resultType="com.mingqi.domin.Users"> select * from Users where name like #{name} </select> </mapper>
6、编写SqlMapConfig.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=""> <environment id="SqlServer"> <!---配置事务类型--> <transactionManager type="JDBC"></transactionManager> <!---配置数据源--> <dataSource type="POOLED"> <!---配置链接数据库的基本信息--> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/> <property name="username" value="sa"/> <property name="password" value="123sa"/> </dataSource> </environment> </environments> <!---映射--> <mappers> <mapper resource="com/mingqi/dao/IUserDao.xml"></mapper> </mappers> </configuration>
7、编写测试类
package com.mingqi; import com.mingqi.dao.IUserDao; import com.mingqi.domin.Users; 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 org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class mybatisTest { private InputStream in; private SqlSession session; private IUserDao userdao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ //1.读取配置文件,生成字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.获取SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in,"SqlServer"); //3.获取SqlSession对象 session = factory.openSession(); //4.获取dao的代理对象 userdao = session.getMapper(IUserDao.class); } @After//用于在测试方法执行之后执行 public void destroy()throws Exception{ //提交事务 session.commit(); //6.释放资源 session.close(); in.close(); } @Test public void TestFindAll() throws Exception { //1读取配置文件 //InputStream inputStream; /* InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //2创建SqlSessionFactory工厂 SqlSessionFactoryBuilder sfb =new SqlSessionFactoryBuilder(); SqlSessionFactory factory=sfb.build(inputStream,"SqlServer"); //3使用工厂生产SqlSession 对象 SqlSession session=factory.openSession(); // 4使用Sql 创建Dao 接口的代理对象 IUserDao userdao=session.getMapper(IUserDao.class);*/ //5使用代理对象执行放放 List<Users> users=userdao.FindAll(); for (Users User :users) { System.out.println(User.getName()); } //6释放方法 /* session.close(); inputStream.close();*/ } @Test public void TestFindByName() throws IOException { //1读取配置文件 //InputStream inputStream; /* InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //2创建SqlSessionFactory工厂 SqlSessionFactoryBuilder sfb =new SqlSessionFactoryBuilder(); SqlSessionFactory factory=sfb.build(inputStream,"SqlServer"); //3使用工厂生产SqlSession 对象 SqlSession session=factory.openSession(); // 4使用Sql 创建Dao 接口的代理对象 IUserDao userdao=session.getMapper(IUserDao.class);*/ //5使用代理对象执行放放 Users user=userdao.FindByName("%李%"); // for (Users User :users) //{ System.out.println(user.getName()); // } //6释放方法 // session.close(); // inputStream.close(); } }
通过快速入门示例,我们发现使用 mybatis 是非常容易的一件事情,因为只需要编写 Dao 接口并且按照
mybatis 要求编写两个配置文件,就可以实现功能。远比我们之前的 jdbc 方便多了。
SqlMapConfig.xml 配置文件