MyBatis 实现基本CRUD操作
在上一篇中已经讲了,基本的配置,那么在这里就不在多说了,直接上源码。
1.myBatis工具类
package com.maya.util; import java.io.IOException; 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; public class SqlSessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getsqlSessionFactory(){ if(sqlSessionFactory==null){ InputStream inputStream=null; try { inputStream=Resources.getResourceAsStream("mybatis-config.xml");//读取配置文件 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } return sqlSessionFactory; } public static SqlSession openSession(){ return getsqlSessionFactory().openSession(); } }
2.Mapper接口
package com.maya.mappers; import java.util.List; import java.util.Map; import com.maya.model.Student; public interface StudentMapper { //添加 public int add(Student student); //修改 public int update(Student student); //删除 public int delete(int id); //查询全部 public List<Student> findAll(); //查询单个 public Student getOneById(int id); //根据条件查询 public List<Student> findWhere(Map<String, Object>param); }
3.映射文件
<?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.maya.mappers.StudentMapper"> <!-- 通常当查询结果是一个集合的话,我们都会在这里定义一个resultMap,用来给下面的方法引用 --> <resultMap type="Student" id="StudentResult"><!-- type就是集合中的类型,id随便你怎么起 --> <id property="id" column="id"></id><!-- property是类的属性名,对应,column是数据库的字段名 --> <result property="name" column="name" /> <result property="age" column="age" /> </resultMap> <insert id="add" parameterType="Student" ><!-- id方法名,parameterType别名,如果不谢别名就要写完成限定名 --> insert into student values(null,#{name},#{age})<!-- 在这里写语句 --> </insert> <update id="update" parameterType="Student"> update student set name=#{name}, age=#{age} where id=#{id} </update> <delete id="delete" parameterType="int"> delete from student where id=#{id} </delete> <select id="getOneById" parameterType="int" resultType="Student"> select * from student where id=#{id} </select> <!-- <select id="findAll" resultType="Student"> select * from student </select> --> <!-- 因为在上面我们定义了一个resultMap所以在这里我们就可以写成下面的方式,当然这两种方式都是可以的 --> <select id="findAll" resultMap="StudentResult"> select * from student </select> <!-- <select id="findWhere" parameterType="Map" resultType="Student"> select * from student where name like #{a1} </select> --> <select id="findWhere" parameterType="Map" resultMap="StudentResult"> select * from student where name like #{a1} and age=#{a2} </select> </mapper>
4.junit测试
package com.maya.service; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.maya.mappers.StudentMapper; import com.maya.model.Student; import com.maya.util.SqlSessionFactoryUtil; public class JunitTest { private static Logger logger=Logger.getLogger(JunitTest.class); private SqlSession sqlSession=null; private StudentMapper studentMapper=null; /** * 测试方法之前调用 * @throws Exception */ @Before public void setUp() throws Exception { sqlSession=SqlSessionFactoryUtil.openSession(); studentMapper=sqlSession.getMapper(StudentMapper.class); } /** * 测试方法之后调用 * @throws Exception */ @After public void tearDown() throws Exception { sqlSession.close(); } @Test public void testAdd() { logger.info("添加学生"); Student s=new Student("哈哈哈哈",16); studentMapper.add(s); sqlSession.commit(); } @Test public void testUpdate() { logger.info("修改学生"); Student s=new Student(7,"哈哈123",19); studentMapper.update(s); sqlSession.commit(); } @Test public void testDelete() { logger.info("删除学生"); studentMapper.delete(4); sqlSession.commit(); } @Test public void testGetOneById() { logger.info("查找单个学生"); Student s=studentMapper.getOneById(6); System.out.println(s.getName()); } @Test public void testFindAll() { logger.info("查找全部学生"); List<Student> list=studentMapper.findAll(); for(Student s:list){ System.out.println(s.getName()); } } @Test public void testFindWhere() { logger.info("根据条件查找学生"); Map<String, Object> param=new HashMap<String,Object>(); param.put("a1", "%哈%"); param.put("a2", 19); List<Student> list=studentMapper.findWhere(param); for(Student s:list){ System.out.println(s.getName()); } } }