通过Mybatis操作数据库

来源:互联网 发布:淘宝服装平铺拍摄 编辑:程序博客网 时间:2024/05/19 22:51
  1. 创建普通java类 含get set toString 等方法。。
public class Student {    private int id;    private String name;    private double score;    private String hobby;   getter;   setter;   toString;}

2.使用单例形式创建SQLSession对象。为什么使用单例模式,请看我在代码中注释。

public class MyBatisUtil {    /**     * 创建单例对象      * SqlSessionFactory 是一个重量级对象 系统开销很大。 一个应用只需要一个这样的对象就可以了。     * 所以把它设置成为单例。。     */    private static SqlSessionFactory factory;    public static SqlSession getSqlSession() {        try {            if (factory == null) {                  //读取主配置文件,将主配置文件与输入流连上。                InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");                //通过输入流 创建SqlSessionFactory 对象                factory = new SqlSessionFactoryBuilder().build(inputStream);            }        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }        //返回session 对象... 默认是将自动提交关闭了的,所以要手动地写上commit()方法..        return factory.openSession();    }}

3.创建dao接口。 主要是通过接口去执行sql语句。。直接上代码:

package com.evecom.dao;import java.util.List;import java.util.Map;import com.evecom.common.*;public interface IStudentDao {    /**     * 插入学生数据        * @param student     */  void insertStudent(Student student);  /***   * 删除学生数据   * @param id 学生id   */  void deleteStudentById(int id);  /***   * 通过id 更新学生信息   * @param 传入学生对象   */  void upDateStudentInfo(Student student);  /***   * 查询所有数据   * @return   */  List<Student> selectAllStudent();  /***   * 查询   * @return   */  Map<String, Student>selectStudentMap();  /***   * 根据id查询 数据   * @param id   * @return   */  Student selectStudentById(int id);  /***   * 查询数据   * @param map 通过map中的key值。   * @return   */  Student selectStudentByMap(Map<String, Object> map);  /***   * 根据学生名字查询学生信息   * @param name 学生名字   * @return 可能有同名的学生, 所以这里是 list    */  List<Student> selectStudentByName(String name);}

4.创建实现类. 实现接口中方法。完成数据库操作

public class StudentDaoImpl implements IStudentDao {    private SqlSession session;    /*** 数据库中student list */    public List<Student> list = null;    public void insertStudent(Student student) {        // TODO Auto-generated method stub        try {            session = MyBatisUtil.getSqlSession();            session.insert("reyco.insertStu", student);            session.commit();        } finally {            // TODO: handle finally clause            if (session!= null) {                session.close();            }        }    }}

5.测试。。

public static void main(String[] args) {        // TODO Auto-generated method stub         IStudentDao iStudentDao = new StudentDaoImpl();//         List<Student> list = iStudentDao.selectAllStudent();//         //         for (Student student : list) {//          System.out.println(student);//      }//      Map<String, Student> map = iStudentDao.selectStudentMap();////      若key值相同。后存入的值覆盖之前存入的值。。。 是最后一条数据。。//     Student student3 = map.get("wwww");//     System.out.println(student3);//         Student student = iStudentDao.selectStudentById(17);//         System.out.println(student);         List<Student> students = iStudentDao.selectStudentByName("张");         for (Student student : students) {            System.out.println(student);        }    }

如上所示。就是增删改查的全过程。。。

原创粉丝点击