mybatis作为一个orm互联网公司基本都在用,今天写个笔记。记录一下mybatis使用
参考官网:
这里使用的mysql, 建了一个数据库db_test,表person,字段id, name, age。插了一条数据,id为1。demo 主要功能从mysql中读取这条记录。
mybatis主要通过读取配置文件xml,初始化得到SqlSessionFactory, 通过facotry拿到session,然后操作数据库。这里具体还要看下源码,后面在做个笔记记录一下。
同时会写下集成spring后的用法
代码:
pom.xml
4.0.0 com.gxf.mybatis demo 1.0-SNAPSHOT org.mybatis mybatis 3.4.6 mysql mysql-connector-java 8.0.11
mybatis-config.xml
Person.java
package com.gxf.mybatis.util;public class Person { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; }}
PersonMapper.java
package com.gxf.mybatis.mapper;import com.gxf.mybatis.util.Person;public interface PersonMapper { Person selectPerson(int id);}
PersonMapper1.java
package com.gxf.mybatis.mapper;import com.gxf.mybatis.util.Person;import org.apache.ibatis.annotations.Select;public interface PersonMapper1 { @Select("SELECT * FROM person WHERE id = #{idss}") Person selectPerson(int idss);}
SqlSessionFactoryHelper.java
package com.gxf.mybatis.util;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;public class SqlSessionFactoryHelper { public static SqlSessionFactory getSqlSessionFactory() { SqlSessionFactory sqlSessionFactory = null; try{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (Exception e){ e.printStackTrace(); } return sqlSessionFactory; }}
测试类mai方法
package com.gxf.mybatis.util;import com.gxf.mybatis.mapper.PersonMapper;import com.gxf.mybatis.mapper.PersonMapper1;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;public class PersonTableOps { private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryHelper.getSqlSessionFactory(); public static void main(String[] args) {// selectOne1();// selectOne2(); selectOne3(); } /** * 第一种方式 * */ public static void selectOne1(){ SqlSession session = sqlSessionFactory.openSession(); try { Person person = session.selectOne( "com.gxf.mybatis.mapper.PersonMapper.selectPerson", 1); System.out.println(person); } finally { session.close(); } } /** * 第二种方式 * */ public static void selectOne2(){ SqlSession session = sqlSessionFactory.openSession(); try { PersonMapper mapper = session.getMapper(PersonMapper.class); Person person = mapper.selectPerson(1); System.out.println(person); } finally { session.close(); } } /** * 第三种方式 * */ public static void selectOne3(){ SqlSession session = sqlSessionFactory.openSession(); try { sqlSessionFactory.getConfiguration().addMapper(PersonMapper1.class); PersonMapper1 mapper = session.getMapper(PersonMapper1.class); Person person = mapper.selectPerson(1); System.out.println(person); } finally { session.close(); } }}