mybatis使用mapper代理开发和注意点
mybatis使用mapper代理开发和注意点
小小小施爷 发表于4个月前
mybatis使用mapper代理开发和注意点
  • 发表于 4个月前
  • 阅读 2
  • 收藏 1
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

使用mapper代理开发的规范

输入图片说明

总结:

输入图片说明

给出一个实际开发的案例 :

maven文件

<dependencies>
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.2.7</version>
  	</dependency>
  	<dependency>
  		<groupId>commons-logging</groupId>
  		<artifactId>commons-logging</artifactId>
  		<version>1.2</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.43</version>
  	</dependency>
  	<dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>  
      </dependency> 
      <!-- 引入dbcp连接池 -->
      <dependency>
	    <groupId>commons-dbcp</groupId>
	    <artifactId>commons-dbcp</artifactId>
		    
	    <version>1.2</version>
	  </dependency>
      <dependency>
      	<groupId>cglib</groupId>
      	<artifactId>cglib</artifactId>
      	<version>3.1</version>
      </dependency>
      <dependency>
      	<groupId>org.apache.logging.log4j</groupId>
      	<artifactId>log4j-api</artifactId>
      	<version>2.8</version>
      </dependency>
      <dependency>
      	<groupId>org.apache.logging.log4j</groupId>
      	<artifactId>log4j-core</artifactId>
      	<version>2.8</version>
      </dependency>
      <dependency>
      	<groupId>org.slf4j</groupId>
      	<artifactId>slf4j-log4j12</artifactId>
      	<version>1.7.7</version>
      </dependency>
      <dependency>
      	<groupId>asm</groupId>
      	<artifactId>asm</artifactId>
      	<version>3.3</version>
      </dependency>
      <dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-ehcache</artifactId>
		    <version>1.0.0</version>
		</dependency>
  </dependencies>

mybatis全局配置文件 sqlMapConfig.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> 
	<!-- 1  加载配置文件 -->
	<properties  resource="db.properties"></properties>
	
	<!-- 2 对连接池 和 事物的配置 (这里必须粘贴之前的 自己写的一模一样都报错) -->
	<environments default="development">  
        <environment id="development">  
        <!-- 使用jdbc事物管理,事物控制由mybatis管理 -->
            <transactionManager type="JDBC" />  
            <!-- 数据库连接池 -->
            <dataSource type="POOLED">  
                <property name="driver" value="${jdbc.driver}" />  
                <property name="url" value="${jdbc.url}" />  
                <property name="username" value="${jdbc.uername}" />  
                <property name="password" value="${jdbc.password}" />  
            </dataSource>  
        </environment>  
    </environments> 
	
	<!-- 3  在sqlMapconfig中加载我们的配置文件 -->
	<mappers>
		<mapper resource="com/shi/mapper/UserMapper.xml"/>
	</mappers>
	
</configuration>

UserMapper.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">

<!--namespace:必须是和mapper.java的相对路径  -->
<mapper namespace="com.shi.mapper.UserMapper">
	<select id="selectUserById" parameterType="Integer" resultType="com.shi.pojo.User">
		select * from User WHERE id=#{id}
	</select>

</mapper>

UserMapper.java文件

package com.shi.mapper;

import com.shi.pojo.User;

public interface UserMapper {
	
	/**
	 * 操作user的mapper的接口
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public User selectUserById(Integer id)throws Exception;

}

测试文件 test.java

package com.shi.mapper;

import static org.junit.Assert.*;

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.Before;
import org.junit.Test;

import com.shi.pojo.User;

public class UserMapperTest {
	
	private SqlSessionFactory sqlSessionFactory;
	
	/**
	 * 在测试之前 我们要得到 会话工厂
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception{
		//1  先得到配置文件
		InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
		
		//2  根据配置文件呢 创建我们的会话工厂
		sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
	}
	

	@Test
	public void testSelectUserById() throws Exception {
		//3  打开一个会话
		SqlSession sqlSession=sqlSessionFactory.openSession();
		//4  创建UserMapper接口的实现类,mybatis自动生成代理对象,自动生成其实现类
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		//5  调用方法 执行
		User user=userMapper.selectUserById(2);
		//6 关闭会话
		sqlSession.close();
		System.out.println(user);
		
	}

}

pojo对象 User.java

package com.shi.pojo;

public class User {
	private Integer id;
	private String username;
	private String birthday;
	private String sex;
	private String address;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
				+ address + "]";
	}
	
}

标签: mapper代理开发
共有 人打赏支持
粉丝 1
博文 76
码字总数 56071
×
小小小施爷
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: