思路:搭建環境-->導入Mybatis-->編寫代碼-->測試java
新建maven工程,配置xml文件mysql
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <!--導入依賴--> <dependencies> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
<?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核心配置文件--> <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?useSSL=true&useUnicode=true&characterEncode=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--每個Mapper.xml都須要在mybatis核心配置文件中註冊!--> <mappers> <mapper resource="org/mybatis/example/UserDaoMapper.xml"/> </mappers> </configuration>
實體類sql
package com.spong.pojo; public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = 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; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
Mapper接口數據庫
public interface UserMapper { List<User> getUserList(); }
接口實現類由原來的UserDaoImpl轉變爲一個Mapper配置文件 UserMapper.xmlapache
<?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="com.spong.dao.UserDao"> <select id="getUserList" resultType="com.spong.pojo.User"> select * from mybatis.user </select> </mapper>
MybatisUtils工具類session
將sqlSessionFactory的建立以及獲取SqlSession的方法封裝爲工具類,實際請求中使用MybatisUtils工具類的靜態方法直接得到SQLSession實例。mybatis
package com.spong.util; 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; static { try { //獲取SqlSessionFactory對象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //獲取SQLSession實例,SQLSession中徹底包含了面向數據庫執行SQL命令所需的全部方法 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
junit測試app
@Test public void test(){ //第一步,獲取SqlSession對象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //第二步,執行sql UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } //關閉SqlSession sqlSession.close(); }
異常:org.apache.ibatis.binding.BindingException: Type interface com.spong.dao.UserDao is not known to the MapperRegistry.maven
須要在覈心配置文件中註冊mapperside
<!--每個Mapper.xml文件都要在覈心配置文件中註冊--> <mappers> <mapper resource="com/spong/dao/UserMapper.xml"/> </mappers>
maven資源導出問題(測試時發現mybatis.xml文件未找到):
須要在pom.xml中配置
<!--在build中配置resources,防止資源導出失敗的問題--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build>