MyBatis基於Maven入門例項

oscar999發表於2020-04-06
本地環境:
1. JDK: 1.8.0_161
2. Eclipse: Oxygen.3 Release (4.7.3)
3.   Maven:  apache-maven-3.5.3
4.   Database: Oracle 10g


準備階段:
第1步: 使用Maven建立一個 Java Project.  Project 名稱: JavaStudy
將此Project匯入到 Eclipse

如何建立和匯入, 可參考

maven建立Java 和 Web Project, 並匯入Eclipse


第2步: 在Oracle建立表 T_USER, 輸入一筆資料,用於後面測試
ID  USERID  USERNAME
1   W00001  bei.liu



程式碼階段
第3步:在pom.xml中加入依賴包, mybatis, ojdbc
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.oscar999</groupId>
	<artifactId>JavaStudy</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>JavaStudy</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.3</version>
		</dependency>
	</dependencies>
</project>


注:
由於需要oracle官方授權,所以maven上(https://repo.maven.apache.org/maven2/com/oracle/)無法下載ojdbc,需要自己下載,然後通過命令載入到本地maven庫中,詳細步驟如下
1) 到Oracle官方下載 ojdbc, 
這裡下載的是 ojdbc6, 版本號:11.2.0.3
2) 安裝到本地庫
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=d:\\temp\\ojdbc6.jar




在命令列進入專案目錄, 執行 mvn eclipse:eclipse, 目的是將相關的jar放入eclipse的build path


第4步:新增User的實體類
/**
 * @Title: User.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 3, 2018 4:05:49 PM
 * @version V1.0
 */


package com.oscar999;


/**
 * @ClassName: User
 * @Description: TODO
 * @author oscar999
 */


public class User {


	private String id;
	private String userId;
	private String userName;


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	@Override
	public String toString() {
		return "User[id=" + id + ",userId=" + userId + ",userName=" + userName + "]";
	}


}




第6步: 新增介面類
/**
 * @Title: UserMapper.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 3, 2018 4:05:00 PM
 * @version V1.0
 */


package com.oscar999;


/**
 * @ClassName: UserMapper
 * @Description: TODO
 * @author oscar999
 */


public interface UserMapper {
	public User findUserById(int id);
}




第7步: 新增介面對映檔案


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper 
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oscar999.UserMapper">
	<select id="findUserById" resultType="com.oscar999.User">
		select id, userId,
		userName from t_user where id = #{id}
	</select>
</mapper>

第8步: 新增MyBatis核心配置檔案

<?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="oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="jdbc:oracle:thin:@172.26.141.90:1521:orcl" />
				<property name="username" value="esdmbrn" />
				<property name="password" value="oracle" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="UserMapper.xml" />
	</mappers>
</configuration>

第9步: 新增測試類並測試

/**
 * @Title: MyBatisTest.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 3, 2018 4:13:48 PM
 * @version V1.0
 */


package com.oscar999;


import java.io.IOException;
import java.io.InputStream;


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;


/**
 * @ClassName: MyBatisTest
 * @Description: TODO
 * @author oscar999
 */


public class MyBatisTest {
	@Test
	public void test() throws IOException {
		SqlSessionFactory sqlSessionFactory = getSQLSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			User user = openSession.selectOne("com.oscar999.UserMapper.findUserById", "1");
			System.out.println(user);
		} finally {
			openSession.close();
		}


	}


	public SqlSessionFactory getSQLSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

}

專案結構:


maven建立Java 和 Web Project, 並匯入Eclipse

相關文章