Mybatis基于oracle数据库的简单案例

1.所需jar包如下:

commons-logging-1.1.1.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
ojdbc7.jar

2.log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout 

3.构造一个POJO对象

public class Role {
	private int id;
	private String name;
	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;
	}
}

4.采用XML方式构建映射器,包含一个接口和一个XML.
定义RoleMapper 接口:

public interface RoleMapper {
	public Role getRole(int id);
}

定义RoleMapper.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="RoleMapper">	
	<select id="getRole" parameterType="int" resultType="role">
		select id,name from stu where id=#{id}
	</select>
</mapper>

5.创建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>
	<typeAliases>
		<typeAlias alias="role" type="Role" />
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="" /><!-- 根据情况填写 -->
				<property name="username" value="" /><!-- 根据情况填写 -->
				<property name="password" value="" /><!-- 根据情况填写 -->
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="cn/itcast/demo5/RoleMapper.xml"></mapper>
	</mappers>
</configuration>

6.构建SqlSessionFactory

public class SqlSessionFactoryUtils {
	private final static Class<SqlSessionFactoryUtils> Lock = SqlSessionFactoryUtils.class;
	private static  SqlSessionFactory SqlSessionFactory = null;
	private SqlSessionFactoryUtils(){}
	public static SqlSessionFactory getSqlSessionFactory(){
		synchronized (Lock) {
			if(SqlSessionFactory != null){
				return SqlSessionFactory;
			}
			String resource = "mybatis-config.xml";
			InputStream inputStream;
			try {
				inputStream = Resources.getResourceAsStream(resource);
				SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
			return SqlSessionFactory;
		}
	}
	public static SqlSession openSqlSession(){
		if(SqlSessionFactory == null){
			getSqlSessionFactory();
		}
		return SqlSessionFactory.openSession();
	}
}

7.测试代码

public class ChapterMain {
	public static void main(String[] args) {
		Logger log = Logger.getLogger(ChapterMain.class);
		SqlSession sqlSession = null;
		sqlSession = SqlSessionFactoryUtils.openSqlSession();
		RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
		Role role = roleMapper.getRole(1);
		log.info("id=" + role.getId() +  " name=" + role.getName());
	}
}

8.测试结果

14:14:32,816  INFO ChapterMain:13 - id=1 name=aa

你可能感兴趣的:(mybatis)