本文以一个简单的小例子,简述在Java项目开发中MyBatis的基本用法,属于入门级文章,仅供学习分享使用,若有不足之处,还请指正。java
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎全部的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 能够使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。mysql
环境搭建步骤:sql
MyBatis须要的Jar包,共2个,以下所示:数据库
1 //MyBatis包 2 mybatis-3.5.3.jar 3 //MySql数据库链接驱动包 4 mysql-connector-java-5.1.6.jar
在src目录下,新增一个MyBatis的配置文件【mybatis-config.xml】,主要配置数据库链接相关环境信息和Mapper信息,具体内容以下:session
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 加载配置文件db.properties --> 7 <properties resource="db.properties"></properties> 8 <!-- 经过指定environments的default值和 environment的id,指定mybatis运行的数据库环境--> 9 <environments default="development"> 10 <environment id="development"> 11 <transactionManager type="JDBC" /> 12 <!-- dataSource数据源类型: 13 UNPOOLED:不采用链接池,默认采用JDBC的方式,须要没有链接打开关闭 14 POOLED:采用链接池进行数据库链接 15 JNDI:从Tomcat中获一个内置的数据库链接池 16 --> 17 <dataSource type="POOLED"> 18 <property name="driver" value="${jdbc.driver}" /> 19 <property name="url" value="${jdbc.url}" /> 20 <property name="username" value="${jdbc.username}" /> 21 <property name="password" value="${jdbc.password}" /> 22 </dataSource> 23 </environment> 24 </environments> 25 <mappers> 26 <mapper resource="com/hex/mybatis/ProductMapper.xml" /> 27 </mappers> 28 </configuration>
本例中新增长一个Product类,包含三个属性,代码以下:mybatis
1 package com.hex.mybatis; 2 3 /** 4 * 产品类模型 5 * @author Administrator 6 * 7 */ 8 public class Product { 9 10 private String pid; //产品ID 11 private String pname; //产品name 12 private float price; //产品价格 13 14 public Product() { 15 16 } 17 18 public Product(String pid, String pname, float price) { 19 this.pid = pid; 20 this.pname = pname; 21 this.price = price; 22 } 23 24 public String getPid() { 25 return pid; 26 } 27 public void setPid(String pid) { 28 this.pid = pid; 29 } 30 public String getPname() { 31 return pname; 32 } 33 public void setPname(String pname) { 34 this.pname = pname; 35 } 36 public float getPrice() { 37 return price; 38 } 39 public void setPrice(float price) { 40 this.price = price; 41 } 42 43 @Override 44 public String toString() { 45 // TODO Auto-generated method stub 46 return "PID="+this.pid+",PNAME="+this.pname+",PRICE="+this.price; 47 } 48 }
增长ProductMapper.xml文件,用于操做数据库,包含数据表Products的CRUD操做。以下所示:app
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace 是mapper文件的惟一标识符 --> 6 <mapper namespace="com.hex.mybatis.ProductMapper"> 7 <!-- 8 id:惟一标识符 9 parameterType:标识输入参数类型 10 resultType:返回对象的类型 11 --> 12 <select id="queryProductById" resultType="com.hex.mybatis.Product" 13 parameterType="String"> 14 select * from Products where pid = #{id} 15 </select> 16 17 <!-- 查询,注意:返回一个值和多个值,resultType同样 --> 18 <select id="queryProductAll" resultType="com.hex.mybatis.Product"> 19 select * from Products where 1=1 20 </select> 21 22 <!-- 插入:parameterType:输入参数类型,在形式上只能有一个,能够是简单类型,也能够是对象类型。 23 若是是对象类型,以 #{属性名} 格式,不能够乱写 24 --> 25 <insert id="addProduct" parameterType="com.hex.mybatis.Product" > 26 insert into Products(pid,pname,price)values(#{pid},#{pname},#{price}) 27 </insert> 28 29 <!-- 更新 --> 30 <update id="updateProductById" parameterType="com.hex.mybatis.Product"> 31 update products set pname=#{pname},price=#{price} where pid=#{pid} 32 </update> 33 34 <!-- 删除 --> 35 <delete id="deleteProductById" parameterType="String"> 36 delete from products where pid=#{pid} 37 </delete> 38 </mapper>
具体代码,以下所示:框架
1 //以输入流的方式加载配置文件 2 String resource = "mybatis-config.xml"; 3 InputStream inputStream = Resources.getResourceAsStream(resource); 4 //建立SqlSessionFactory对象,build第二个参数指定environment的id,,若是不写,默认配置default. 5 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 6 //建立会话对象 7 SqlSession session = sqlSessionFactory.openSession(); 8 //-------------------查询单个对象--------------------- 9 //执行操做,若是queryProductById有相同的名称,则须要使用彻底限定名,即:namespace+id 10 Product product = session.selectOne("queryProductById", "A-002"); 11 System.out.println(product.toString()); 12 //---------------------End------------------------- 13 //关闭会话对象 14 session.close();
关于数据表的增删改查,以下所示:ide
1 //-------------------新增单个对象--------------------- 2 Product product =new Product("A-006","康师傅绿茶",3.5f); 3 int count = session.insert("addProduct",product); 4 session.commit(); 5 System.out.println("新增长了 "+count+" 个产品!!!"); 6 //---------------------End------------------------- 7 //-------------------修改单个对象--------------------- 8 Product product =new Product("A-004","茉莉花茶",3.2f); 9 int count = session.update("updateProductById", product); 10 session.commit(); 11 System.out.println("修改了 "+count+" 个产品!!!"); 12 //---------------------End------------------------- 13 //-------------------删除单个对象--------------------- 14 int count = session.delete("deleteProductById", "A-002"); 15 session.commit(); 16 System.out.println("删除了 "+count+" 个产品!!!"); 17 //---------------------End------------------------- 18 //-------------------查询个对象--------------------- 19 List<Product> lstProduct = session.selectList("queryProductAll"); 20 for(Product p : lstProduct){ 21 System.out.println(p); 22 } 23 //---------------------End-------------------------
定风波·三月七日 学习
做者:苏轼 (宋)
三月七日,沙湖道中遇雨。雨具先去,同行皆狼狈,余独不觉。已而遂晴,故做此。
莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任生平。
料峭春风吹酒醒,微冷,山头斜照却相迎。回首向来潇瑟处,归去,也无风雨也无晴。