开始学习另一个ORM 框架:Mybatis,与hibernate类似,感觉比Hibernate轻量,比较小.
这节要完成入门例子.
1.建表mybatis_student
id | name | age |
2.实体类
- package com.skymr.mybatis.model;
- public class Student {
- private int id;
- private String name;
- private int age;
- /**
- * 必须要有无参构造器,有参构造器可有可无(至少我测试时是这样)
- * 如果没有无参构造器,只有有参构造器,会报错
- */
- public Student() {
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = 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;
- }
- public String toString(){
- return "["+name+","+age+"]";
- }
- }
3.mybatis配置文件
jdbc.properties : jdbc配置信息
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://127.0.0.1:3306/website
- jdbc.username=root
- jdbc.password=root
conf.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <!-- 引入jdbc配置信息 -->
- <properties resource="jdbc.properties"></properties>
- <typeAliases>
- <!-- 别名配置,方便书写 -->
- <typeAlias alias="Student" type="com.skymr.mybatis.model.Student"/>
- </typeAliases>
- <!-- 环境配置 -->
- <environments default="development">
- <!-- 开发环境,企业里面可能有多个环境 -->
- <environment id="development">
- <transactionManager type="JDBC" />
- <!-- 配置数据库连接信息 -->
- <dataSource type="POOLED">
- <property name="driver" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!-- 注册StudentMapper.xml文件, -->
- <mapper resource="com/skymr/mybatis/mappers/StudentMapper.xml"/>
- </mappers>
- </configuration>
与Hibernate太像了啊.
4.映射文件与映射接口
映射接口
- package com.skymr.mybatis.mappers;
- import com.skymr.mybatis.model.Student;
- public interface StudentMapper {
- public Student getStudent(int id);
- }
映射文件,类似Hibernate的xml配置的映射
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <!-- namespace:Mapper映射 -->
- <!-- 这个文件感觉就像StudentMapper接口的实现一样,只是从java文件变成了xml文件
- 充当了Dao类的功能
- -->
- <mapper namespace="com.skymr.mybatis.mappers.StudentMapper">
- <select id="getStudent" parameterType="int" resultType="Student">
- <!-- mybatis要自己写sql语句 -->
- select * from mybatis_Student where id=#{id}
- </select>
- </mapper>
这个映射文件感觉就是映射接口的实现类
5.工具类,获取Session
- package com.skymr.mybatis.util;
- import java.io.InputStream;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- /**
- * 工具类,单例
- * @author skymr
- *
- */
- public class MybatisUtil {
- private static SqlSessionFactory factory;
- public static SqlSessionFactory getSessionFactory(){
- if (factory == null){
- InputStream is = MybatisUtil.class.getClassLoader().getResourceAsStream("conf.xml");
- factory = new SqlSessionFactoryBuilder().build(is);
- }
- return factory;
- }
- public static SqlSession openSession(){
- return getSessionFactory().openSession();
- }
- }
6.测试
- package com.skymr.mybatis.service;
- import org.apache.ibatis.session.SqlSession;
- import com.skymr.mybatis.mappers.StudentMapper;
- import com.skymr.mybatis.util.MybatisUtil;
- public class StudentTest {
- public static void main(String[] args){
- // String statement = "com.skymr.mybatis.mappers.StudentMapper.getStudent";//映射sql的标识字符串
- // //执行查询返回一个唯一user对象的sql
- // Student student = MybatisUtil.openSession().selectOne(statement, 1);
- // System.out.println(student);
- SqlSession session = MybatisUtil.openSession();
- StudentMapper mapper = session.getMapper(StudentMapper.class);
- System.out.println(mapper.getStudent(1));
- session.close();
- }
- }
ps:
session需要关闭;
从session里取出映射接口的实例就可调用操作数据库的方法了,底层一定是根据映射配置文件实现了一个映射接口,这个跟hibernate不一样啊.
数据库里添加一行数据,id=1
测试成功.