博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis 学习记录(1)—— 入门程序
阅读量:2059 次
发布时间:2019-04-29

本文共 5505 字,大约阅读时间需要 18 分钟。

尊重个人劳动成果,转载请注明出处:

0.写在前面

本文仅介绍 mybatis 的入门级使用,即对单个表的增删改查操作。

1. 准备环境

  • IDEA 14.1.7
  • Mysql 5.5
  • maven

2. 数据库

学生表:stu

这里写图片描述

3. 配置文件

  • jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis01?useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=335588

4. 项目结构

这里写图片描述

  • SqlMapConfig.xml
  • stu 映射文件(stu.xml)
INSERT stu(name)VALUES (#{name})
DELETE FROM stu WHERE `name` LIKE '${name}%'
UPDATE stu SET `name`=#{name} WHERE id=#{id}
  • pom.xml
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

5. java 代码

  • StudenDao 接口:
public interface StudentDao {    Student findById(int id);    Student findByName(String name);    void insertStudent(Student student);    void deleteStudent(String name);    void updateStudent(Student student);}
  • mybatis 工作流程:

    1. 通过 Reader 对象读取src目录下的 SqlMapConfig.xml 配置文件(该文本的位置和名字可任意)
    2. 通过 SqlSessionFactoryBuilder 对象创建 SqlSessionFactory 对象
    3. 从当前线程中获取 SqlSession 对象
    4. 通过 SqlSession 对象读取 stu.xml 映射文件中的 id,从而读取 sql 语句
    5. 事务提交,必写
    6. 关闭 SqlSession 对象,让 GC 尽早回收
  • 测试类:

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(); Map
map = 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; }}
你可能感兴趣的文章
Java Guava中的函数式编程讲解
查看>>
Eclipse Memory Analyzer 使用技巧
查看>>
tomcat连接超时
查看>>
谈谈编程思想
查看>>
iOS MapKit导航及地理转码辅助类
查看>>
检测iOS的网络可用性并打开网络设置
查看>>
简单封装FMDB操作sqlite的模板
查看>>
iOS开发中Instruments的用法
查看>>
iOS常用宏定义
查看>>
被废弃的dispatch_get_current_queue
查看>>
什么是ActiveRecord
查看>>
有道词典for mac在Mac OS X 10.9不能取词
查看>>
关于“团队建设”的反思
查看>>
利用jekyll在github中搭建博客
查看>>
Windows7中IIS简单安装与配置(详细图解)
查看>>
linux基本命令
查看>>
BlockQueue 生产消费 不需要判断阻塞唤醒条件
查看>>
ExecutorService 线程池 newFixedThreadPool newSingleThreadExecutor newCachedThreadPool
查看>>
强引用 软引用 弱引用 虚引用
查看>>
数据类型 java转换
查看>>