一、简介:什么是MyBatis
1.MyBatis是一款优秀的持久层框架,支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或者注解来配置和映射原生信息,将接口和Java的POJOs映射成数据库中的记录
二、入门
1.安装mybatis-x.x.x.jar将文件置于classpath即可,使用maven来构造项目,则需要将下面的dependency代码放在pom.xml文件中:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
2.创建数据库
create database mybatis;
3.创建users表
create table users(id int primary key auto_increment,name varchar(20),age int)
4.创建必要的包
com.jd.ssm.dao.impl
com.jd.ssm.service.impl
com.jd.ssm.dao.domain
com.jd.ssm.util
com.jd.ssm.ssm.web.controller
5.测试连接
package com.jd.ssm.mybatisdao; 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.InputStream; public class APP { public static void main(String[] args){ try{ //指定配置文件的类路径 String resource ="mybatis-config.xml"; //加载文件,形成输入流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工厂,相当于连接池 SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接了 SqlSession s= sf.openSession(); System.out.println(s); } catch ( Exception e) { e.printStackTrace(); } } }
6.编写mapper文件
a)创建User类,和users对应
package com.jd.ssm.domain; public class User { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer 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; } }
b).创建UserMapper.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="users"> <insert id="insert"> insert into users(name,age) values(#{name},#{age}); </insert> </mapper>
三、复杂应用
1.准备数据
sql.sql
source C:UsersshizeqiDesktopsql.sql
2.创建java类
(1)User类
package com.jd.ssm.domain; public class User { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer 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; } }
(2)Order类
package com.jd.ssm.domain; //创建订单类 public class Order { private Integer id ; private String orderNo; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } //这个地方需要用到uid,直接加上User就可以 private User user; }
(3)Item类
package com.jd.ssm.domain; /* * 订单项 * */ public class Item { private Integer id; private String itemName; private Order order ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
3.创建订单Order映射文件
[resource/OrderMapper.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="orders"> <insert id="insert"> insert into orders(orderno,uid) values(#{orderNo},#{user.id}); </insert> </mapper>