配置文件
db.properties
jdbc.driver=com.oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:wiicare
jdbc.username=wiicare
jdbc.password=Mdsd123
log4j.properties
### 设置###
log4j.rootLogger = ERROR,stdout### è¾åºä¿¡æ¯å°æ§å¶æ¬ ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.category.org.springframework.beans.factory=ERROR
mybatis-config.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"><configutation><!-- 引入数据库配置 --><properties resource="db.properties"></properties><!-- 定义别名 --><typeAliases><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"></typeAlias></typeAliases><!-- 定义数据源 --><environments default=""><environment id="development"><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></dataSource></environment></environments><mappers><package name="com.geyao.mabatis.mapper"></package></mappers></configutation>
BlogMapper.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.geyao.mybatis.mapper.BlogMapper"><!-- statement --><select id="selectBlog" parameterType="int" resultType="Blog">select * from Blog where id=#{id}</select></mapper>
BlogMapper类
package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);}
Blog类
package com.geyao.mybatis.pojo;public class Blog {private Integer id;private String title;private int authorId;private String state;private Boolean featured;private String style;public Blog(Integer id, String title, int authorId, String state, Boolean featured, String style) {super();this.id = id;this.title = title;this.authorId = authorId;this.state = state;this.featured = featured;this.style = style;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getAuthorId() {return authorId;}public void setAuthorId(int authorId) {this.authorId = authorId;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}}
MyBatisUtil类
package com.geyao.mybatis.util;import java.io.IOException;
import java.io.InputStream;import javax.annotation.Resource;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.omg.CORBA.PUBLIC_MEMBER;import jdk.internal.dynalink.beans.StaticClass;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory=null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}