注意:
1、首先在Mapper接口中完善方法
package com.dao; import com.pojo.Student; import java.util.List; public interface StudentMapper { List<Student> SelectStudent(); Student SelectStudentByid(String s_id); int InsertStudent(Student student); int UpdateStudent(Student student); int DeleteByid(String s_id); }
2、在mapper.xml中完善sql 和相应的参数
<?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.dao.StudentMapper"> <select id="SelectStudent" resultType="com.pojo.Student"> select * from school.student </select> <select id="SelectStudentByid" parameterType="String" resultType="com.pojo.Student"> select * from school.student where s_id = #{s_id} </select> <insert id="InsertStudent" parameterType="com.pojo.Student" > insert into school.student (s_id,s_name,s_sex,s_birth) values (#{s_id},#{s_name},#{s_sex},#{s_birth}) </insert> <delete id="DeleteByid" parameterType="String"> delete from school.student where s_id = #{s_id} </delete> <update id="UpdateStudent" parameterType="com.pojo.Student"> update school.student set s_name = #{s_name},s_sex = #{s_sex} where s_id = #{s_id} </update> </mapper>
3、测试
import com.dao.StudentMapper; import com.pojo.Student; import com.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class Mytest { @Test public void SelectStudent() { // 通过MybatisUtils 获取sqlsession,并通过sqlSession获取mapper // 再调用mapper中的方法查询 SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = mapper.SelectStudent(); for (Student student : students) { System.out.println(student); } sqlSession.close(); } @Test public void SelectStudentByid() { SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = mapper.SelectStudentByid("03"); System.out.println(student); sqlSession.close(); } @Test public void InsertStudent() { SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = new Student("09", "小番茄", "男", "1995-09-14"); int i = mapper.InsertStudent(student); System.out.println(i); sqlSession.commit(); sqlSession.close(); } @Test public void UpdateStudent(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = mapper.SelectStudentByid("02"); student.setS_name("yyf"); student.setS_sex("lv"); int i = mapper.UpdateStudent(student); System.out.println(i); sqlSession.commit(); sqlSession.close(); } @Test public void DeleteByid(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); int i = mapper.DeleteByid("09"); System.out.println(i); sqlSession.commit(); sqlSession.close(); } }
万能的MAP
如果,实体类或者数据库中表的参数或者字段过多,可以使用Map
例:
注意:
//万能的map List<Student> SelectStudentByMap(Map<String,Object> map);
<select id="SelectStudentByMap" parameterType="map" resultType="com.pojo.Student"> select * from student where s_name = #{sname}; </select>
@Test public void SelectStudentByMap(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Map<String , Object> map = new HashMap<>(); map.put("sname","yyf"); List<Student> students = mapper.SelectStudentByMap(map); for(Student student :students){ System.out.println(student); } sqlSession.close(); }
模糊查询
//模糊查询 List<Student> SelectStudentLike(String value);
<!--使用模糊查询 --> <select id="SelectStudentLike" parameterType="String" resultType="com.pojo.Student"> select * from student where s_name like "%"#{value}"%" </select> 或者 <select id="SelectStudentLike" parameterType="String" resultType="com.pojo.Student"> select * from student where s_name like #{value} </select>
@Test public void SelectStudentLike() { SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = mapper.SelectStudentLike("王"); for (Student student : students) { System.out.println(student); } sqlSession.close(); } public void SelectStudentLike() { SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = mapper.SelectStudentLike("%王%"); for (Student student : students) { System.out.println(student); } sqlSession.close(); }
原文:https://www.cnblogs.com/yyyyff/p/14756859.html