很早之前就想學習mybatis,據說很多公司都使用這個架構。以前我都使用老掉牙的DBCP,Hibernate感覺太大,現
在來學學這個中性的架構。
首先是環境的配置,我使用maven建立項目,pom.xml檔案如下
<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>com.bird</groupId><artifactId>concursey</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>concursey</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version></dependency></dependencies></project>
還得建立一個資料庫和對應的一張表做示範,這裡就不寫了,直接上對應的JavaBean
package com.bird.mybatis.bean;public class Users {private int id;private String name;private int age;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 int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";}}
然後要給對應的JavaBean建立對應的mapper.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.bird.mybatis.bean.userMapper"><!--根據ID擷取對應的值 --><select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">select * from users where id = #{id}</select></mapper>
當然還有mybatis的通用檔案,conf.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><!-- development:開發模式 work:工作模式 --><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> <mappers><mapper resource="com/bird/mybatis/bean/userMapper.xml" /></mappers></configuration>
最後是測試代碼
package com.bird.mybatis.bean;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test {public static void main(String[] args) throws Exception {String resource = "conf.xml";Reader reader = Resources.getResourceAsReader(resource);SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);SqlSession session = sessionFactory.openSession();String statement = "com.bird.mybatis.bean.userMapper.getUser";Users user = session.selectOne(statement, 1);System.out.println(user);}}
總體來說還是非常簡單的。