本文共 5505 字,大约阅读时间需要 18 分钟。
尊重个人劳动成果,转载请注明出处:
本文仅介绍 mybatis 的入门级使用,即对单个表的增删改查操作。
学生表:stu
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis01?useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=335588
INSERT stu(name)VALUES (#{name}) DELETE FROM stu WHERE `name` LIKE '${name}%' UPDATE stu SET `name`=#{name} WHERE id=#{id}
4.0.0 com.czd.mybatis_test MybatisTest 1.0-SNAPSHOT log4j log4j 1.2.17 mysql mysql-connector-java 5.1.37 runtime c3p0 c3p0 0.9.1.2 org.mybatis mybatis 3.3.0 org.mybatis mybatis-spring 1.2.3 junit junit 4.12 test
public interface StudentDao { Student findById(int id); Student findByName(String name); void insertStudent(Student student); void deleteStudent(String name); void updateStudent(Student student);}
mybatis 工作流程:
测试类:
package dao;import com.czd.mybatis01.bean.Student;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.Reader;import java.util.HashMap;import java.util.Map;/** * Created by czd on 2017/5/4. */public class StudentDao { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; private static String nameSpace = "com.czd.mybatis01.dao.StudentDao"; static { try { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } @Test public void main() throws Exception { StudentDao studentDao = new StudentDao(); // 增加// Student student = new Student();// student.setName("a");// studentDao.testInsertStudent(student); // 删除// studentDao.testDeleteStudent("a"); // 更改// Student student2 = new Student();// student2.setId(2);// student2.setName("czd2");// studentDao.testUpdateStudent(student2); // 根据id查询 System.out.println(studentDao.testFindById(1)); // 根据name查询 System.out.println(studentDao.testFindByName("czd2")); } public void testInsertStudent(Student student) throws Exception { SqlSession sqlSession = getSession().openSession(); sqlSession.insert(nameSpace + ".insertStudent", student); sqlSession.commit(); sqlSession.close(); } public void testDeleteStudent(String name) throws Exception { SqlSession sqlSession = getSession().openSession(); Mapmap = new HashMap(); map.put("name", name); sqlSession.delete(nameSpace + ".deleteStudent", map); sqlSession.commit(); sqlSession.close(); } public void testUpdateStudent(Student student) throws Exception { SqlSession sqlSession = getSession().openSession(); sqlSession.insert(nameSpace + ".updateStudent", student); sqlSession.commit(); sqlSession.close(); } public Student testFindById(int id) throws Exception { SqlSession sqlSession = getSession().openSession(); Student student = sqlSession.selectOne(nameSpace + ".findById", id); sqlSession.commit(); sqlSession.close(); return student; } public Student testFindByName(String name) throws Exception { SqlSession sqlSession = getSession().openSession(); Student student = sqlSession.selectOne(nameSpace + ".findByName", name); sqlSession.commit(); sqlSession.close(); return student; }}