mybatis-配置

XML配置

  1. 导包
    需要两个包,mybatis和jdbc驱动
  <dependencies>
  	<dependency>
	  <groupId>org.mybatis</groupId>
	  <artifactId>mybatis</artifactId>
	  <version>3.5.2</version>
	</dependency>
  	<dependency>
	  <groupId>com.oracle.jdbc</groupId>
	  <artifactId>ojdbc6</artifactId>
	  <version>6.0</version>
	</dependency>
</dependencies>
  1. 核心配置文件(具体可看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>
//加载配置文件
  <properties resource="config.properties"/>
  
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  
  <mappers>
  //配置映射文件
  	<mapper resource="org/lq/mapper/DeptMapper.xml"/>
  	<mapper resource="org/lq/mapper/EmpMapper.xml"/>
  </mappers>
</configuration>
  1. 创建实体类
  2. 创建dao
  3. 创建实体类对应的映射文件
<?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="org.lq.dao.DeptMapper">

//对应的resultMap,
  <resultMap id="BaseResultMap" type="org.lq.model.Dept">
    <id column="DEPTNO" jdbcType="DECIMAL" property="deptno" />
    <result column="DNAME" jdbcType="VARCHAR" property="dname" />
    <result column="LOC" jdbcType="VARCHAR" property="loc" />
  </resultMap>
  
  <sql id="Base_Column_List">
    DEPTNO, DNAME, LOC
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from DEPT
    where DEPTNO = #{deptno,jdbcType=DECIMAL}
  </select>
</mapper>
  1. 测试
		SqlSessionFactory build;
		try {
			build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
			DeptMapper dao = build.openSession().getMapper(DeptMapper.class);
			System.out.println(dao.selectByPrimaryKey(10L));
		} catch (IOException e) {
			e.printStackTrace();
		}

注解配置

。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值