<MyBatis>入門一 HelloWorld
阿新 • • 發佈:2018-12-13
1.HelloWorld
匯入依賴
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
建立一個員工資料庫
CREATETABLE `tbl_employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(255) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `email` varchar(255) DEFAULT NULL ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
編寫實體類
public class Employee { private Integer id; privateString name; private Character gender; private String email;
... }
建立一個全域性配置檔案 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"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 將寫好的mapper註冊到全域性配置檔案中 --> <mappers> <mapper resource="EmployeeMapper.xml" /> </mappers> </configuration>
編寫對映檔案Mapper
<?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 名稱空間 作用:用於繫結介面,即使用namespace將對映檔案和介面繫結,就不需要寫實現類了
指定為介面的全類名 --> <mapper namespace="org.maple.dao.EmployeeMapper"> <!--id 唯一標識 :對應介面中的哪個方法 resultType 返回型別,對應實體類的Employee --> <select id="selectEmp" resultType="org.maple.pojo.Employee"> select id,last_name name,email,gender from tbl_employee where id = #{id} </select> </mapper>
編寫測試類
public class MybatisTest { @Test public void test01() throws IOException { //讀取配置檔案 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //建立sqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //獲取sqlSession例項,能直接執行已經對映的sql語句 SqlSession sqlSession = sqlSessionFactory.openSession(); //執行查詢方法 try { Employee employee = sqlSession.selectOne("org.maple.dao.EmployeeMapper.selectEmp", 1); System.out.println(employee); }finally { //關閉sqlSession sqlSession.close(); } } }
2.通過介面的方式
員工Crud介面
public interface EmployeeMapper { Employee getEmpById(Integer id); }
員工Mapper檔案
<?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 名稱空間 作用:用於繫結介面,即使用namespace將對映檔案和介面繫結,就不需要寫實現類了 --> <mapper namespace="org.maple.mapper.EmployeeMapper"> <!--id 唯一標識 :對應介面中的哪個方法 resultType 返回型別,對應實體類的Employee --> <select id="getEmpById" resultType="org.maple.pojo.Employee"> select id,last_name name,email,gender from tbl_employee where id = #{id} </select> </mapper>
測試方法
public class MybatisTest { @Test public void test01() throws IOException { //讀取配置檔案 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //建立sqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //獲取sqlSession例項,能直接執行已經對映的sql語句 SqlSession sqlSession = sqlSessionFactory.openSession(); //執行查詢方法 try {
//獲取介面的代理物件 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); Employee emp = mapper.getEmpById(1); System.out.println(emp); }finally { //關閉sqlSession sqlSession.close(); } } }
1、介面式程式設計
原生: Dao ====> DaoImpl
mybatis: Mapper ====> xxMapper.xml
2、SqlSession代表和資料庫的一次會話;用完必須關閉; 3、SqlSession和connection一樣都是非執行緒安全。每次使用都應該去獲取新的物件。 4、mapper介面沒有實現類,但是mybatis會為這個介面生成一個代理物件。 (將介面和xml進行繫結) EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class); 5、兩個重要的配置檔案: mybatis的全域性配置檔案:包含資料庫連線池資訊,事務管理器資訊等...系統執行環境資訊 sql對映檔案:儲存了每一個sql語句的對映資訊: 將sql抽取出來。
1、根據xml配置檔案(全域性配置檔案)建立一個SqlSessionFactory物件 有資料來源一些執行環境資訊 2、sql對映檔案;配置了每一個sql,以及sql的封裝規則等。 3、將sql對映檔案註冊在全域性配置檔案中 4、寫程式碼: 1)、根據全域性配置檔案得到SqlSessionFactory; 2)、使用sqlSession工廠,獲取到sqlSession物件使用他來執行增刪改查 一個sqlSession就是代表和資料庫的一次會話,用完關閉 3)、使用sql的唯一標誌來告訴MyBatis執行哪個sql。sql都是儲存在sql對映檔案中的。