- 使用动态代理实现dao接口的实现类
MyBatis允许只声明一个dao接口,而无需写dao实现类的方式实现数据库操作。前提是必须保证Mapper文件中的<mapper>标签的namespace属性值必须要和dao接口的类路径一致,MyBatis容器会自动通过动态代理生成接口的实现类。
Mapper.java
1 package cn.mybatis.dao; 2 3 import cn.mybatis.domain.Student; 4 5 public interface StudentMapper { 6 public void insertStudent(Student s); 7 public void updateStudent(Student s); 8 public void deleteStudent(String stuid); 9 public Student selectStudentById(String stuid); 10 }
Mapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="cn.mybatis.dao.StudentMapper"> 5 <resultMap type="student" id="BaseResultMap"> 6 <id column="stu_id" property="stuId" jdbcType="VARCHAR" javaType="java.lang.String" /> 7 <result column="stu_name" property="stuName" jdbcType="VARCHAR" javaType="java.lang.String" /> 8 <result column="stu_birthdate" property="stuBirthdate" jdbcType="DATE" javaType="java.util.Date" /> 9 <result column="stu_phone" property="stuPhone" jdbcType="VARCHAR" javaType="java.lang.String" /> 10 </resultMap> 11 12 <!-- 插入数据 --> 13 <insert id="insertStudent" parameterType="student"> 14 insert into student (stu_id,stu_name,stu_birthdate,stu_phone) 15 values(#{stuId},#{stuName},#{stuBirthdate},#{stuPhone}) 16 </insert> 17 18 <!-- 更新数据 --> 19 <update id="updateStudent" parameterType="student"> 20 update student set stu_name=#{stuName}, stu_birthdate=#{stuBirthdate}, 21 stu_phone=#{stuPhone} where stu_id=#{stuId} 22 </update> 23 <!-- 删除数据 --> 24 <delete id="deleteStudent" parameterType="string"> 25 delete from student where stu_id=#{stuId} 26 </delete> 27 28 <!-- 查询数据,返回的数据会根据resultMap设置封装到实体类对象中 --> 29 <select id="selectStudentById" resultType="cn.mybatis.domain.Student" parameterType="string" > 30 select stu_name as stuName from student where stu_id=#{stuId} 31 </select> 32 </mapper>
测试
1 package cn.mybatis.demo; 2 3 import java.io.InputStream; 4 import java.text.SimpleDateFormat; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.apache.log4j.Logger; 11 12 import cn.mybatis.dao.StudentMapper; 13 import cn.mybatis.domain.Student; 14 15 public class Demo_01 { 16 private static SqlSessionFactory fac; 17 static{ 18 InputStream is = null; 19 try{ 20 //处理并根据config配置文件实例化SqlSessionFactory 21 is = Resources.getResourceAsStream("SqlMapperConfig.xml"); 22 //获取session工厂类 23 fac = new SqlSessionFactoryBuilder().build(is); 24 }catch(Exception e){ 25 e.printStackTrace(); 26 Logger.getLogger(Demo_01.class).debug(e.getMessage()); 27 } 28 } 29 public static void main(String[] args) throws Exception { 30 //创建要保存的学生信息 31 Student s = new Student(); 32 s.setStuId("5"); 33 s.setStuName("zhou"); 34 s.setStuBirthdate(new SimpleDateFormat("yyyy-MM-dd").parse("1991-1-12")); 35 s.setStuPhone("341324123"); 36 37 SqlSession session = fac.openSession(); 38 StudentMapper mapper = session.getMapper(StudentMapper.class); 39 // mapper.insertStudent(s); 40 // mapper.updateStudent(s); 41 // mapper.deleteStudent("5"); 42 Student s1 = mapper.selectStudentById("1"); 43 System.out.println(s1.getStuName()); 44 session.commit(); 45 session.close(); 46 } 47 }