MyBatis基于Maven入门实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oscar999/article/details/80197018
本地环境:
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

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试