Mybatis的简单使用步骤(java项目)

1、在eclipse中创建mavenjava项目(jar),并初始化依赖,放入log4j的配置文件

pom文件如下(一个是Mybatis必须的,一个是log4j的日志)


  4.0.0
  com.iflytek
  Mybatis1
  0.0.1-SNAPSHOT
  
  
		
		
			org.mybatis
			mybatis
			3.4.5
		
		
	
		
		
			log4j
			log4j
			1.2.17
		
	
  

放入log4j的配置文件(log4j.properties)

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.iflytek=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

2、创建mybatis-config.xml文件,指定最基本的environments和mappers,这里我用外部加载数据库配置文件jdbc.properties

jdbc.properties如下

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@172.16.130.35:1521:orcl
jdbc.username=uuid
jdbc.password=uuid

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true

mybatis-config.xml如下




	

	
	
		
	

	
		
			
			
				
				
				
				

			
		
	
	
	
		
	
3、创建对应的pojo, mapper.xml文件和对应在dao中的接口,这里用Blog.java, BlogMapper.xml和BlogMapper.java三个文件

Blog.java

package com.iflytek.domain;

public class Blog {
	private int id;
	private String title;
	private String auth;

	public int getId() {
		return id;
	}

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

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuth() {
		return auth;
	}

	public void setAuth(String auth) {
		this.auth = auth;
	}

	@Override
	public String toString() {
		return "Blog [id=" + id + ", title=" + title + ", auth=" + auth + "]";
	}

}

BlogMapper.xml





	
	
		
		
		
	

	
	

	
		insert into tt_blog (id,title) values(SEQ_BLOG.nextval,#{title})
	

	
		update tt_blog set title = #{title}
		
		
			,auth = #{auth}
		
		where id = #{id}
	

	
		delete from tt_blog where id = #{id}
	
BlogMapper.java

package com.iflytek.dao;

import com.iflytek.domain.Blog;

public interface BlogMapper {
	public Blog selectBlog(Blog blog);
	public void saveBlog(Blog blog);
	public void updateBlog(Blog blog);
	public void deleteBlog(Blog blog);
	
}

4、代建对应的数据库和表,本例创建如下

Mybatis的简单使用步骤(java项目)_第1张图片

初始化部分数据


5、创建测试类进行测试,这里对SQLsessionfactory还没有进行封装,开发中一般使用单例模式进行封装,只有一个工厂sessionfactory,可有多个session对象(Test.java)

package com.iflytek.test;

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 com.iflytek.dao.BlogMapper;
import com.iflytek.domain.Blog;

public class Test {
	public static void main(String[] args) throws IOException {
		
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession session = sqlSessionFactory.openSession();
		try {
			
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			
			Blog blog = new Blog();
			blog.setId(1);
			
			Blog res = mapper.selectBlog(blog);
			
			System.out.println(res.getTitle());
		} finally {
		  session.close();
		}
	}
}

6、查看结果(id为1的对应title为1111),正确

Mybatis的简单使用步骤(java项目)_第2张图片









你可能感兴趣的:(javaWEB)