Mybatis概要
来源:互联网 发布:高拍仪软件通用版 编辑:程序博客网 时间:2024/06/10 02:34
使用Mybatis把握四个要点:
1.Mybatis配置文件mybatis-config.xml
2.Mybatis映射文件MyMapper.xml
3.对应映射文件的接口MyMapper.java
4.实体类My.java
1.mybatis-config.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> <properties resource="properties/mybatis-config.properties"> <property name="username" value="test"/> <property name="password" value="test123"/> </properties> <settings> <!--配置默认的执行器:重用预处理语句--> <setting name="defaultExecutorType" value="REUSE"/> </settings> <typeAliases> <typeAlias alias="Student" type="com.agcro.myapp.model.Student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/agcro/myapp/data/StudentMapper.xml"/> </mappers></configuration>
2.MyMapper.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"><mapper namespace="com.agcro.myapp.data.StudentMapper"> <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="major" column="major"/> <result property="grade" column="grade"/> </resultMap> <select id="getById" parameterType="int" resultMap="studentResultMap"> select * from student where id=#{id} </select></mapper>
3.映射接口文件MyMapper.java
package com.agcro.myapp.data;import com.agcro.myapp.model.Student;/** * Created by x on 13-6-23. */public interface StudentMapper { public Student getById(int id);}
4.实体类My.java
public class Student { private int id; private String name; private String gender; private String major; private String grade; 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; }}
最后是业务测试
import com.agcro.myapp.data.StudentMapper;import com.agcro.myapp.model.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 java.io.IOException;import java.io.Reader;/** * Created by x on 13-6-23. */public class test { public static void main(String[] args) { //mybatis-config.xml中的mapper配置类似,告诉MyBatis //应读取的核心配置文件 String resource = "com/agcro/myapp/data/mybatis-config.xml"; Reader reader = null; try{ reader = Resources.getResourceAsReader(resource); }catch(IOException e) { e.printStackTrace(); } //创建SqlSessionFactory实例。没有指定要用到的 //environment,则使用默认的environment SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); try{ StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = mapper.getById(1); if(student != null) { System.out.println("姓名: "+student.getName() +"\n专业: "+student.getMajor()); } else { System.out.println("没有找到。"); } } finally { sqlSession.close(); } }}
最后补充上面.properties配置文件mybatis-config.properfies
#数据库driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/testusername=testpassword=test123
- Mybatis概要
- mybatis重点概要
- Spring +MyBatis 配置文件概要说明
- 概要
- 概要
- 概要
- 概要
- 中间件概要
- SAML概要
- JSP概要
- IPV6概要
- 中间件概要
- ICE概要
- 概要设计
- 中间件概要
- JavaScript概要
- 概要设计
- Zedgraph概要~
- VS2013体验
- Restricted Boltzmann Machine (RBM)限制波尔兹曼机
- Spring+Struts+Hibernate
- 数据压缩解析及C语言实现介绍
- Java相关博客推荐(不定期更新---2013年8月13号更新 Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning
- Mybatis概要
- 数据的存储
- Recover Binary Search Tree
- 修改Linux的runlevel值
- oracle 创建用户,密码 ,授权,表空间操作,删除等
- struts如何实现国际化
- BUAA-SCSE Training day1
- 2013 whu暑假集训选拔#1
- Mobile phones poj1195 二维线段树