Mybatis环境搭建
(一)环境搭建
(1)第一步:创建maven工程并导入jar包
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.41version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.6version> dependency> dependencies>
(2)第二步:创建实体类和dao的接口
(3)第三步:创建Mybatis的主配置文件SqlMapperConfig.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC">transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver">property> <property name="url" value="jdbc:mysql://localhost:3306/zml?useUnicode=true&characterEncoding=utf-8">property> <property name="username" value="root">property> <property name="password" value="root">property> dataSource> environment> environments> <mappers> <mapper resource="dao/UserDao.xml">mapper> mappers> configuration>
(4)第四步:创建映射配置文件UserDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.UserDao"> <select id="findAll" resultType="entity.Users"> select * from users; select> mapper>
(二)环境搭建的注意事项
(1)创建UserDao.xml和UserDao.java时名称是为了和我们之前的知识保持-致。在Mybatis中它把持久层的操作接口名称和映射文件也叫做:Mapper所以:UserDao和UserMapper是一样的
(2)在idea中创建目录的时候,它和包是不一样的包在创建时:com.itheina.dao它是三级结构目录在创建时:com.itheima.dao是一级目录
(3)mybatis的映射配置文件位置必须和dao接口的包结构相同
(4)映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名
(5)映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名,当我们遵从了第三,四,五点之后,我们在开发中就无须再写dao的实现类
(三)第一个Mybatis实例
(1)第一步:
pom.xml
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.41version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.6version> dependency> dependencies>
(2)第二步:
Users.java
package entity; public class Users { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Users{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
UserDao.java
package dao; import entity.Users; import java.util.List; public interface UserDao { ListfindAll(); }
(3)第三步:
SqlMapperConfig.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC">transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver">property> <property name="url" value="jdbc:mysql://localhost:3306/zml?useUnicode=true&characterEncoding=utf-8">property> <property name="username" value="root">property> <property name="password" value="root">property> dataSource> environment> environments> <mappers> <mapper resource="dao/UserDao.xml">mapper> mappers> configuration>
(4)第四步
UserDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.UserDao"> <select id="findAll" resultType="entity.Users"> select * from users; select> mapper>
(5)第五步:创建测试类
UserTest.java
package test; import dao.UserDao; import entity.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.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class UserTest { @Test public void findall() throws IOException { //1.读取配置文件 InputStream in= Resources.getResourceAsStream("SqlMapperConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂生产SqlSession对象 SqlSession session=factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 UserDao UserDaoImpl=session.getMapper(UserDao.class); //5.使用代理对象执行方法 Listusers=UserDaoImpl.findAll(); for(Users u:users){ System.out.println(u); } //6.释放资源 session.close(); in.close(); } }