mybatis(6)---mapper动态代理方式

mapper动态代理实现原理:

mapper接口开发方式只需要程序员编写mapper接口(相当于dao层接口)

mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法同dao接口实现类方法。


mapper接口开发需要遵循以下规范:

1)、mapper.xml文件中的namespace和mapper接口的类路径相同,即namespace设置为接口的类路径

2)、mapper接口中的方法名称应该与mapper.xml中的id名称相同,即接口中的方法名和映射文件中的id名称相同

3)、mapper接口中的方法中的参数类型应该与mapper.xml中的每个sql的paramterType的类型相同

4)、mapper接口中的方法中的结果类型和mapper.xml中的每个sql的resultType的类型相同


操作步骤如下:

1、新建项目mybatisDemo,并导入所需要的jar包




2、在src下新建mybatis的核心配置文件mybatis.cfg.xml

2.1、新建数据库配置db.properites

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
user=root
password=root
2.2、mybatis.cfg.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、引入db.properties配置文件 -->
   	 <properties resource="db.properties" />
	 
	 <!-- 2、设置别名 -->
	 <typeAliases>
	 	<package name="com.cn.vo" />
	 </typeAliases>
	 
	 <!-- 
	 	development为开发模式
	 	work为工作模式
	  -->	
     <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="${user}" />
                 <property name="password" value="${password}" />
             </dataSource>
         </environment>     
     </environments>
     
</configuration>

3、创建数据库




4、在src下新建数据库表t_student所对应的实体类StudentVO

package com.cn.vo;
/**
 * 学生实体类
 * */
public class StudentVO {
	private int stuId;           //学生id
	private String stuName;      //学生姓名
	private String sex;          //学生性别
	
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "StudentVO [stuId=" + stuId + ", stuName=" + stuName + ", sex="
				+ sex + "]";
	}
	
	
}
注:上面的实体类字段取名会存在一个隐患问题,不知发现没有,数据库字段stu_id对应实体类字段stuId,stu_name对应stuName,sex对应sex

数据库表字段名和实体类字段名不一致解决问题:


5、在src下新建包名com.cn.mapper,然后在包下新建实体类映射文件 StudentMapper.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.cn.dao.StudentDao">
 		<!-- 1.根据id查询学生信息 -->
 		<select id="selectStudentById" parameterType="int" resultType="StudentVO">
 			select t.stu_id as stuId,
 				   t.stu_name as stuName,
 				   t.sex as sex	
 			 from t_student t where t.stu_id=#{stuId} 
 		</select>
 		
 		<!-- 2、通过学生姓名查询用户 -->
 		<select id="selectStudentByName" parameterType="java.lang.String" resultType="StudentVO">
 			select t.* from t_student t 
 			where t.stu_name like '%${stuName}%' 
 		</select>
 		
 		<!-- 3、新增用户 -->
 		<insert id="insertUser" parameterType="StudentVO">
 			<selectKey keyProperty="stuId" order="AFTER" resultType="java.lang.Integer">
				<!-- mysql数据库需要设置主键自增 -->
 				select LAST_INSERT_ID()
 			</selectKey>
 			insert into t_student(
 				stu_name,
 				sex
 			)values(
 				#{stuName},
 				#{sex}
 			)
 		</insert>
 		
 </mapper>

6、在src新建包com.cn.dao,然后新建接口类StudentDao(注意:和StudentMapper.xml文件中的<mapper>标签对的namespace属性值相同)

package com.cn.dao;

import com.cn.vo.StudentVO;

public interface StudentMapper {
	//根据学生id查询学生信息
	public StudentVO selectStudentById(int stuId);
	//根据学生名称查询学生信息
	public StudentVO selectStudentByName(String stuName);
	//新增用户
	public void insertUser(StudentVO studentVO);
}

7、在mybatis.cfg.xml中注册映射文件StudentMapper.xml

 <!-- 注册UserMapper.xml文件 -->
     <mappers>
   		<mapper resource="com/cn/mapper/StudentMapper.xml" />
     </mappers>


8、测试

1、新增Student测试代码如下

package com.cn.test1;

import org.apache.ibatis.session.SqlSession;

import com.cn.dao.StudentDao;
import com.cn.utils.MybatisUtil;
import com.cn.vo.StudentVO;

/**
 * 新增学生
 * */
public class InsertStudent {
	public static void main(String[] args) {
		//1.获得sqlSession
		SqlSession session=MybatisUtil.getSqlSession(true);
		//2.获取mapper接口的代理对象
		StudentDao studentDao=session.getMapper(StudentDao.class);
		//3.实例化对象
		StudentVO studentVO =new StudentVO();
		studentVO.setStuName("哈哈");
		studentVO.setSex("男");
		//4.通过mapper接口添加学生
		studentDao.insertUser(studentVO);
	}
}

2、通过id查询Student测试代码如下:

package com.cn.test1;

import org.apache.ibatis.session.SqlSession;

import com.cn.dao.StudentDao;
import com.cn.utils.MybatisUtil;
import com.cn.vo.StudentVO;

public class FindUserById {
	public static void main(String[] args) {
		//1.获取sqlSession 
		SqlSession session=MybatisUtil.getSqlSession();
		//2.获取mapper接口的代理对象
		StudentDao stuDao=session.getMapper(StudentDao.class);
		//3.sessio操作执行查询
		StudentVO stuVO=stuDao.selectStudentById(2);
		//4.打印结果
		System.out.println(stuVO.toString());
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值