版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yuan52007298/article/details/81476550
测试环境一定要配置好(mybatis.xml,MyBatisUtil.java)
见https://blog.csdn.net/Yuan52007298/article/details/81413042
Mapper动态代理省略了接口方法的实现,减少代码书写量。使用更方便快捷。
动态代理的方法就是使映射文件的namspace指向接口方法。要求:方法名必须和映射sql语句id相同
(1)方法接口(不必写实现类)
package com.Demo.Dao;
import java.util.List;
import java.util.Map;
import com.Demo.Entity.Student;
public interface StudentDao {
void insertStudent(Student student);
void insertStudentReturnId(Student student);
void deleteStudent(Integer id);
void updateStudent(Student student);
List<Student> selectAllStudent();
Student selectStudentById(Integer id);
List<Student> selectStudentLimit(Map map);
}
(2)mapper.xml (记得在mybatis.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 namespace="com.Demo.Dao.StudentDao">
<!-- 解决数据库字段名与实体属性字段名不一致映射 -->
<resultMap type="Student" id="studentMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="score" property="score"/>
</resultMap>
<insert id="insertStudent" parameterType="com.Demo.Entity.Student">
insert into student(name,age,score) values(#{name},#{age},#{score})
</insert>
<!-- 使用别名 -->
<insert id="insertStudentReturnId" parameterType="Student">
insert into student(name,age,score) values(#{name},#{age},#{score})
<!-- mysql的自增主键在insert语句之后,所以AFTER。ORACLE数据库的逐渐自增在inert语句之前,所以BEFORE -->
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@identity
</selectKey>
</insert>
<delete id="deleteStudent">
<!-- #{xxx}是占位符,名称随意,参数自动填充 -->
delete from Student where id = #{xxx}
</delete>
<update id="updateStudent">
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<!-- select结果数据集需要封装到实体类中,需要设置属性resultType -->
<select id="selectAllStudent" resultType="Student">
select id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age,score from student where id=#{xxx}
</select>
<select id="selectStudentLimit" resultType="Student">
select id,name,age,score from student LIMIT #{start},#{size}
</select>
<!-- 模糊查询 -->
<select id="selectStudentByName" resultType="Student">
<!-- select id,name,age,score from student where name like concat('%',#{xxx},'%') -->
<!-- select id,name,age,score from student where name like '%' #{xxx} '%' -->
select id,name,age,score from student where name like '%${value}%'
</select>
</mapper>
(3)完成接口到SQL语句的映射后,就可以直接写测试了
package com.Demo.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.Demo.Dao.StudentDao;
import com.Demo.Entity.Student;
import com.Demo.Utils.MyBatisUtil;
public class MyTest2 {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testInsert() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
Student student = new Student("黄荣",23,89.9);
dao.insertStudent(student);
sqlSesion.commit();
sqlSesion.close();
}
@Test
public void testInsertReturnId() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
Student student = new Student("乐呵呵",23,89.9);
System.out.println(student.toString());
dao.insertStudentReturnId(student);
sqlSesion.commit();
sqlSesion.close();
System.out.println(student.toString());
}
@Test
public void deleteStudent() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
dao.deleteStudent(11);
sqlSesion.commit();
sqlSesion.close();
}
@Test
public void updateStduent() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
Student student = new Student("范冰冰",24,89.9);
student.setId(1);
dao.updateStudent(student);
sqlSesion.commit();
sqlSesion.close();
}
@Test
public void selectAllStudent() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
List<Student> stuList = dao.selectAllStudent();
sqlSesion.commit();
sqlSesion.close();
for (Student student : stuList) {
System.out.println(student.toString());
}
}
@Test
public void selectStudentById() {
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
Student student = dao.selectStudentById(1);
sqlSesion.commit();
sqlSesion.close();
System.out.println(student.toString());
}
@Test
public void selectStudentLimit() {
Map<String,Integer> map = new HashMap<>();
map.put("start", 1);
map.put("size", 4);
SqlSession sqlSesion = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSesion.getMapper(StudentDao.class);
List<Student> studentList = dao.selectStudentLimit(map);
sqlSesion.commit();
sqlSesion.close();
for (Student student : studentList) {
System.out.println(student.toString());
}
}
}