通过Mybatis操作数据库
来源:互联网 发布:淘宝服装平铺拍摄 编辑:程序博客网 时间:2024/05/19 22:51
- 创建普通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); } }
如上所示。就是增删改查的全过程。。。
阅读全文
0 0
- 通过Mybatis操作数据库
- 安装mysql并通过mybatis进行数据库操作
- 通过MyBatis 访问数据库
- myBatis 操作数据库
- MyBatis 的数据库操作
- MyBatis操作数据库步骤
- 通过vs操作数据库
- mybatis集成spring操作数据库
- [笔记]Mybatis连接数据库操作
- javase mybatis的操作数据库
- Mybatis注解方法操作数据库
- 使用MyBatis操作数据库连接
- mybatis的oracle数据库操作
- Mybatis的简单数据库操作
- Mybatis操作数据库的方式
- Mybatis简单的操作数据库
- MYBATIS+MYSQL 批量操作数据库
- 一 mybatis操作oracle数据库
- Win32日记之 动态库 静态库 静态链接
- 剑指offer—矩阵中的路径
- ava构造和解析Json数据的两种方法详解一
- makefile 规则之---make命令-编译,依赖关系
- js的内存泄露
- 通过Mybatis操作数据库
- tensorflow中的逆卷积操作 输出向量的尺寸计算
- Google是如何做负载均衡的?
- 通过MrcroBlaze将配置文件通过BPI烧进FLASH
- 陶哲轩教你学数学 第1章 解题策略 读书笔记
- ActiveMQ 实现点对点的消息模型小Demo
- netty源码分析之-ReferenceCounted详解(12)
- java发送http请求
- AutoCAD .Net 使用扩展字典存储自定义数据