Mybatis初级配置
1. 新建maven项目,导入jar包
2配置核心文件 mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入config约束 --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!-- 给student类路径起个别名,在映射文件中可直接调用 --> <typeAliases> <typeAlias type="com.oracle.pojo.Student" alias="Student"/> </typeAliases>
<!-- 接受数据库元数据 default默认读取一下配置--> <environments default="mysql">
<!-- 针对不同的数据库来配置元数据 id="区分元数据"--> <environment id="mysql"> <!-- 事务管理交由jdbc来管理 --> <transactionManager type="jdbc"/> <!--连接池 --> <dataSource type="pooled"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource>
</environment>
<!-- 配置oracle <environment id="oracle">
<transactionManager type="jdbc"/>
<dataSource type="pooled"> <property name="driver" value="oracle驱动"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521/数据库"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> --> </environments>
<!-- 引入mapping文件 --> <mappers> <mapper resource="com/oracle/pojo/studentMapper.xml"/> </mappers> </configuration> |
3写实体类student.class(注意数据库中必须有一张与之对应的表,且表字段类型相同)
package com.oracle.pojo;
public class Student { private Integer id; private String sname; private Integer sage; private String saddress; private Integer t_id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Integer getSage() { return sage; } public void setSage(Integer sage) { this.sage = sage; } public String getSaddress() { return saddress; } public void setSaddress(String saddress) { this.saddress = saddress; } public Integer getT_id() { return t_id; } public void setT_id(Integer t_id) { this.t_id = t_id; } @Override public String toString() { return "Student [id=" + id + ", sname=" + sname + ", sage=" + sage + ", saddress=" + saddress + ", t_id=" + t_id + "]"; }
}
|
4配置实体映射文件studentMapping.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- 本地mapping约束 --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!-- 给映射空间起名,后面需要调用 --> <mapper namespace="Student"> <!-- 返回结果集 --> <resultMap type="Student" id="studentMap"> <id property="id" column="id"/> <result property="sname" column="sname"/> <result property="sage" column="sage"/> <result property="saddress" column="saddress"/> <result property="t_id" column="t_id"/> </resultMap> <!-- 数据库查询分页 --> <select id="selectByPage" resultMap="studentMap" parameterType="map"> select * from student limit #{start},#{count} </select>
</mapper> |
5 写一个工具用来读取mybatis.xml
MybatisUtil.java
package com.oracle.utils;
import java.io.IOException; import java.io.Reader;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class mybatisUtil { public static ThreadLocal<SqlSession> threadLocal=new ThreadLocal<SqlSession>(); public static SqlSessionFactory sessionFactory; static { try { // 读取mybatis核心配置文件 Reader reader = Resources.getResourceAsReader("mybatis.xml"); //创建会话工厂 sessionFactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} public static SqlSession getSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession==null){ //从会话工厂获取session sqlSession=sessionFactory.openSession(); //绑定会话工厂 threadLocal.set(sqlSession); } return sqlSession; } //关闭session public static void close(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession!=null){ sqlSession.close(); } }
}
|
6. 测试 Test.java
package com.oracle.test;
import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import com.oracle.pojo.Student; import com.oracle.utils.mybatisUtil;
public class Test { public static void main(String[] args) { //从工具中获取session SqlSession session = mybatisUtil.getSession(); //创建map集合,存贮将要传进mybatis.xml工具参数 Map<String, Object> map=new HashMap<String,Object>(); map.put("start", 0); map.put("count", 2); //获得查询结果 List<Student> selectList = session.selectList("Student.selectByPage",map); //输出结果 for (Student student : selectList) { System.out.println(student); }
session.commit();
mybatisUtil.close(); } }
|